#!/usr/bin/env ruby #!ruby # Simple Uploader for YUI Editor # This CGI script handles an input form named "yui_image", save the # file into DATA_DIR, and answers JSON status string for An Image # Upload Extension for YUI Rich Text Editor # http://allmybrain.com/2007/10/16/an-image-upload-extension-for-yui-rich-text-editor/ # If "yui_image" is not included in the request, it shows input form # by itself for debugging purpose. The file name is encoded in URL # escape. The content type should be image/* but there is no security # regard, so it is not recommended to use in serious application. require "cgi" DATA_DIR = "projects" # data directory PREVIEW = < Upload test

{{PREVIEW}}

EOT YUI_OUT = < e output = "{status: '#{e}'}" else output = template.sub('{{PREVIEW}}', preview(fileName, type)) output = output.sub('{{URL}}', url(fileName)) end print "Content-type: text/html\n\n" print output end def url fileName DATA_DIR + '/' + CGI.escape(fileName) end # Save form data, and answer file name and type def upload value if /^image\// !~ value.content_type raise "File type #{value.content_type} is not supported." end contents = value.read fileName = CGI.escape(value.original_filename) localName = DATA_DIR + '/' + fileName File.open(localName, "w") {|f| f.print contents } return [fileName, value.content_type] end def preview fileName, type return "type: #{type}
name: #{fileName}
" + "#{url fileName}" end main