note description: "Summary description for {IMAGE_UPLOADER_EXECUTION}." date: "$Date$" revision: "$Revision$" class IMAGE_UPLOADER_EXECUTION inherit WSF_ROUTED_SKELETON_EXECUTION undefine requires_proxy end WSF_NO_PROXY_POLICY WSF_ROUTED_URI_TEMPLATE_HELPER SHARED_EXECUTION_ENVIRONMENT create make feature {NONE} -- Initialization setup_router -- Setup router local www: WSF_FILE_SYSTEM_HANDLER do map_uri_template_agent ("/upload/{name}{?nb}", agent execute_upload_put, router.methods_put) map_uri_template_agent ("/upload{?nb}", agent execute_upload, Void) create www.make_with_path (document_root) www.set_directory_index (<<"index.html">>) www.set_not_found_handler (agent execute_not_found) router.handle ("", www, router.methods_GET) end feature -- Configuration document_root: PATH -- Document root to look for files or directories once Result := execution_environment.current_working_path.extended ("htdocs") end files_root: PATH -- Uploaded files will be stored in `files_root' folder once Result := document_root.extended ("files") end feature -- Execution execute_not_found (uri: READABLE_STRING_8; req: WSF_REQUEST; res: WSF_RESPONSE) -- `uri' is not found, redirect to default page do res.redirect_now_with_content (req.script_url ("/"), uri + ": not found.%NRedirection to " + req.script_url ("/"), "text/html") end execute_upload (req: WSF_REQUEST; res: WSF_RESPONSE) -- Upload page is requested, either GET or POST -- On GET display the web form to upload file, by passing ?nb=5 you can upload 5 images -- On POST display the uploaded files local l_body: STRING_8 l_safe_filename: STRING_8 fn: PATH page: WSF_HTML_PAGE_RESPONSE n: INTEGER do if req.is_request_method ("GET") or else not req.has_uploaded_file then create page.make page.set_title ("EWF: Upload file") page.add_style (req.script_url ("style.css"), "all") create l_body.make_empty page.set_body (l_body) l_body.append ("

EWF: Upload image file

%N") l_body.append ("
%N") if attached {WSF_STRING} req.query_parameter ("nb") as p_nb and then p_nb.is_integer then n := p_nb.integer_value else n := 1 end if attached {WSF_STRING} req.query_parameter ("demo") as p_demo then fn := document_root.extended (p_demo.value) l_body.append ("File:
%N") end from until n = 0 loop l_body.append ("File:
%N") n := n - 1 end l_body.append (" %N
") res.send (page) else create l_body.make (255) l_body.append ("

EWF: Uploaded files

%N") l_body.append ("") create page.make page.set_title ("EWF: uploaded image") page.add_style ("../style.css", "all") page.set_body (l_body) res.send (page) end end execute_upload_put (req: WSF_REQUEST; res: WSF_RESPONSE) -- Upload page is requested, PUT require is_put_request_method: req.is_put_request_method local l_body: STRING_8 l_safe_filename: detachable READABLE_STRING_32 fn: PATH page: WSF_HTML_PAGE_RESPONSE n: INTEGER do create l_body.make (255) l_body.append ("

EWF: Uploaded files

%N") l_body.append ("") create page.make page.set_title ("EWF: uploaded image") page.add_style ("../style.css", "all") page.set_body (l_body) res.send (page) end feature {NONE} -- Encoder new_temporary_output_file (n: detachable READABLE_STRING_8): detachable FILE local ut: WSF_FILE_UTILITIES [RAW_FILE] bp: detachable PATH d: DIRECTORY do create bp.make_current create d.make_with_path (bp) if not d.exists then d.recursive_create_dir end create ut if n /= Void then Result := ut.new_temporary_file (d, Void, n) else Result := ut.new_temporary_file (d, "tmp", Void) end ensure Result /= Void implies Result.is_open_write end url_encode (s: READABLE_STRING_32): STRING_8 -- URL Encode `s' as Result do Result := url_encoder.encoded_string (s) end url_encoder: URL_ENCODER once create Result end html_encode (s: READABLE_STRING_32): STRING_8 -- HTML Encode `s' as Result do Result := html_encoder.encoded_string (s) end html_encoder: HTML_ENCODER once create Result end end