Please choose some file(s) to upload.
") - - -- create form to choose files and upload them - body.append ("%N") - - -- put the body to the response - res.put_string (body) - - show_and_store_files (req, res) - - end - - show_and_store_files (req: WSF_REQUEST; res: WSF_RESPONSE) - -- show all uploaded files - local - file_path: PATH - file_name: STRING_8 - stored: BOOLEAN - file_system_handler: WSF_FILE_SYSTEM_HANDLER - file_system_upload_handler: CMS_FILE_UPLOAD_FILE_SYSTEM_HANDLER - uploaded_file: CMS_FILE_UPLOAD_FILE - do - -- if has uploaded files, then store them - if req.has_uploaded_file then - across - req.uploaded_files as uf - loop - file_name := uf.item.safe_filename - - -- check if file is already in folder - if not files_root.has_extension (file_name) then - file_path := files_root.extended (file_name) - - -- move file to path - stored := uf.item.move_to(file_path.name) - end - end - end - - -- create file_system_handler and show the uploaded files - create file_system_handler.make_with_path (document_root) - file_system_handler.enable_index - file_system_handler.process_index ("/uploaded_files", files_root, req, res) - - end - - -feature -- Mapping helper: uri template agent (analogue to the demo-module) - - map_uri_template (a_router: WSF_ROUTER; a_tpl: STRING; h: WSF_URI_TEMPLATE_HANDLER; rqst_methods: detachable WSF_REQUEST_METHODS) - -- Map `h' as handler for `a_tpl', according to `rqst_methods'. - require - a_tpl_attached: a_tpl /= Void - h_attached: h /= Void - do - a_router.map (create {WSF_URI_TEMPLATE_MAPPING}.make (a_tpl, h), rqst_methods) - end - - map_uri_template_agent (a_router: WSF_ROUTER; a_tpl: READABLE_STRING_8; proc: PROCEDURE [TUPLE [req: WSF_REQUEST; res: WSF_RESPONSE]]; rqst_methods: detachable WSF_REQUEST_METHODS) - -- Map `proc' as handler for `a_tpl', according to `rqst_methods'. - require - a_tpl_attached: a_tpl /= Void - proc_attached: proc /= Void - do - map_uri_template (a_router, a_tpl, create {WSF_URI_TEMPLATE_AGENT_HANDLER}.make (proc), rqst_methods) - end -end diff --git a/modules/file_upload/cms_file_upload_file.e b/modules/file_upload/cms_file_upload_file.e index c94ae8a..8ab4288 100644 --- a/modules/file_upload/cms_file_upload_file.e +++ b/modules/file_upload/cms_file_upload_file.e @@ -9,26 +9,35 @@ class inherit WSF_UPLOADED_FILE - redefine - name + rename + make as make_uploaded_file, + name as uploaded_file_name, + change_name as change_uploaded_file_name, + is_empty as is_empty_uploaded_file, + exists as uploaded_file_exists end RAW_FILE - undefine - make, change_name, is_empty, exists + rename + make as make_file end +-- undefine +-- make, change_name, is_empty, exists +-- end create - make_new, - make_with_path + make feature -- Initialization - make_new (a_name: READABLE_STRING_GENERAL; a_filename: READABLE_STRING_GENERAL; a_content_type: like content_type; a_size: like size; a_user: CMS_USER) + make (a_name: READABLE_STRING_GENERAL; a_filename: READABLE_STRING_GENERAL; a_content_type: like content_type; a_size: like size; a_user: CMS_USER) local time: DATE_TIME do - new_name := a_name.as_string_32 + make_uploaded_file (a_name, a_filename, a_content_type, a_size) + make_with_name (a_filename) + + uploaded_file_name := a_name.as_string_32 url_encoded_name := url_encoded_string (a_name) filename := a_filename.as_string_32 content_type := a_content_type diff --git a/modules/file_upload/cms_file_uploader_api.e b/modules/file_upload/cms_file_uploader_api.e new file mode 100644 index 0000000..22c4311 --- /dev/null +++ b/modules/file_upload/cms_file_uploader_api.e @@ -0,0 +1,78 @@ +note + description: "API to manage files." + date: "$Date$" + revision: "$Revision$" + +class + CMS_FILE_UPLOADER_API + +inherit + CMS_MODULE_API + + REFACTORING_HELPER + +create + make + +feature -- Access + + uploads_directory_name: STRING = "uploaded_files" + + uploads_location: PATH + do + Result := cms_api.files_location.extended (uploads_directory_name) + end + + file_link (f: CMS_FILE): CMS_LOCAL_LINK + local + s: STRING + do + s := "files" + across + f.location.components as ic + loop + s.append_character ('/') + s.append (percent_encoded (ic.item.name)) + end + create Result.make (f.filename, s) + end + +feature -- Factory + + new_file (p: PATH): CMS_FILE + do + create Result.make (p, cms_api) + end + + new_uploads_file (p: PATH): CMS_FILE + -- New uploaded path from `p' related to `uploads_location'. + do + create Result.make ((create {PATH}.make_from_string (uploads_directory_name)).extended_path (p), cms_api) + end + +feature -- Storage + + save_uploaded_file (f: CMS_UPLOADED_FILE) + local + p: PATH + ut: FILE_UTILITIES + stored: BOOLEAN + do + p := f.location + if p.is_absolute then + else + p := uploads_location.extended_path (p) + end + if ut.file_path_exists (p) then + -- FIXME: find an alternative name for it, by appending "-" + i.out , with i: INTEGER; + error_handler.add_custom_error (-1, "uploaded file storage failed", "A file with same name already exists!") + else + -- move file to path + stored := f.move_to (p) + if not stored then + error_handler.add_custom_error (-1, "uploaded file storage failed", "Issue occurred when saving uploaded file!") + end + end + end + +end diff --git a/modules/file_upload/cms_file_uploader_module.e b/modules/file_upload/cms_file_uploader_module.e new file mode 100644 index 0000000..23c75fe --- /dev/null +++ b/modules/file_upload/cms_file_uploader_module.e @@ -0,0 +1,332 @@ +note + description: "file_upload application root class" + date: "$Date$" + revision: "$Revision$" + +class + CMS_FILE_UPLOADER_MODULE + +inherit + CMS_MODULE + rename + module_api as file_upload_api + redefine + install, + initialize, + setup_hooks, + permissions, + file_upload_api + end + + CMS_HOOK_MENU_SYSTEM_ALTER + + SHARED_EXECUTION_ENVIRONMENT + +create + make + +feature {NONE} -- Initialization + + make + do + name := "file_uploader" + version := "1.0" + description := "Service to upload files, and manage them." + package := "file" + end + +feature -- Access + + name: STRING + + permissions: LIST [READABLE_STRING_8] + -- List of permission ids, used by this module, and declared. + do + Result := Precursor + Result.force ("admin uploaded files") + Result.force ("upload files") + end + +feature {CMS_API} -- Module Initialization + + initialize (api: CMS_API) + --Open the media ") + body.append (api.html_encoded (f.filename)) + body.append (".
%N") + + if attached f.location.extension as ext then + if + ext.is_case_insensitive_equal_general ("png") + or ext.is_case_insensitive_equal_general ("jpg") + then + body.append ("Please choose some file(s) to upload.
") + + -- create form to choose files and upload them + body.append ("%N") + + if req.is_post_request_method then + process_uploaded_files (req, api, body) + end + + -- Build the response. + create {GENERIC_VIEW_CMS_RESPONSE} r.make (req, res, api) + append_uploaded_file_album_to (req, api, body) + r.set_main_content (body) + else + create {BAD_REQUEST_ERROR_CMS_RESPONSE} r.make (req, res, api) + end + r.execute + end + + process_uploaded_files (req: WSF_REQUEST; api: CMS_API; a_output: STRING) + -- show all uploaded files + local +-- stored: BOOLEAN +-- file_system_handler: WSF_FILE_SYSTEM_HANDLER +-- file_system_upload_handler: CMS_FILE_UPLOAD_FILE_SYSTEM_HANDLER + l_uploaded_file: CMS_UPLOADED_FILE + uf: WSF_UPLOADED_FILE +-- ut: FILE_UTILITIES +-- files_root: PATH + do + if attached file_upload_api as l_file_upload_api then + -- if has uploaded files, then store them + if req.has_uploaded_file then + a_output.append ("