From 0c3b85bb370c9996d3844db3aa1bc356a9e7e23a Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Mon, 26 Sep 2016 10:37:41 +0200 Subject: [PATCH] Set the uploaded file path to site/temp by default. Can be set via configuration with "temp-dir" variable. (This is important, otherwise it uses the current working directory, but for fcgi, this is not always obvious where it is.) Allow advanced upload, and also basic html upload, in case the dropzone.js has trouble. Split get and post handling for files module upload request. --- modules/files/cms_files_module.e | 81 ++++++++++++++++++++++---------- src/configuration/cms_setup.e | 15 +++++- src/service/cms_api.e | 10 +++- src/service/cms_execution.e | 18 ++++++- 4 files changed, 95 insertions(+), 29 deletions(-) diff --git a/modules/files/cms_files_module.e b/modules/files/cms_files_module.e index 89d8f21..8f64d5d 100644 --- a/modules/files/cms_files_module.e +++ b/modules/files/cms_files_module.e @@ -230,45 +230,78 @@ feature -- Handler local body: STRING_8 r: CMS_RESPONSE + i: INTEGER do - if req.is_get_head_request_method or req.is_post_request_method then + if req.is_get_head_request_method then + create {GENERIC_VIEW_CMS_RESPONSE} r.make (req, res, api) create body.make_empty body.append ("

Upload files

%N") - create {GENERIC_VIEW_CMS_RESPONSE} r.make (req, res, api) - -- set style - r.add_style (r.url ("/module/" + name + "/files/css/files.css", void), void) - - -- add JS for dropzone - r.add_javascript_url (r.url ("/module/" + name + "/files/js/dropzone.js", void)) - r.add_style (r.url ("/module/" + name + "/files/js/dropzone.css", void), void) + r.add_style (r.url ("/module/" + name + "/files/css/files.css", Void), Void) if api.has_permission (upload_files_permission) then - -- create body - body.append ("

Please choose some file(s) to upload.

") - + body.append ("

Please choose file(s) to upload.

") -- create form to choose files and upload them body.append ("
") - body.append ("
") - body.append ("
%N") - body.append ("") - body.append ("
") - if req.is_post_request_method then - process_uploaded_files (req, api, body) + + if attached {WSF_STRING} req.item ("basic_upload") as p and then p.is_case_insensitive_equal ("yes") then + -- No dropzone + body.append ("
") + body.append ("%N") + from + i := 1 + until + i > 5 + loop + body.append ("
%N") + i := i + 1 + end + body.append ("
") + body.append ("
%N") + body.append ("Use advanced file uploading.%N") + else + -- add JS for dropzone + r.add_javascript_url (r.url ("/module/" + name + "/files/js/dropzone.js", Void)) + r.add_style (r.url ("/module/" + name + "/files/js/dropzone.css", Void), Void) + + -- create form to choose files and upload them + body.append ("
") + body.append ("
%N") + + body.append ("Use basic file uploading.%N") end - else - create {FORBIDDEN_ERROR_CMS_RESPONSE} r.make (req, res, api) + body.append ("") end - -- Build the response. - if r.has_permission (browse_files_permission) then - append_uploaded_file_album_to (req, api, body) - else - r.add_warning_message ("You are not allowed to browse files!") + if req.is_get_head_request_method then + -- Build the response. + if r.has_permission (browse_files_permission) then + body.append ("
Refresh uploaded
") + + append_uploaded_file_album_to (req, api, body) + else + r.add_warning_message ("You are not allowed to browse files!") + end end r.set_main_content (body) + elseif req.is_post_request_method then + if api.has_permission (upload_files_permission) then + create {GENERIC_VIEW_CMS_RESPONSE} r.make (req, res, api) + create body.make_empty + body.append ("

Upload files

%N") + process_uploaded_files (req, api, body) + if attached {WSF_STRING} req.item ("basic_upload") as p and then p.is_case_insensitive_equal ("yes") then + r.set_redirection (r.url (uploads_location, Void) + "?basic_upload=yes") + else + r.set_redirection (r.url (uploads_location, Void)) + end + r.set_main_content (body) + else + create {FORBIDDEN_ERROR_CMS_RESPONSE} r.make (req, res, api) + r.set_main_content ("You are not allowed to upload file!") + end else create {BAD_REQUEST_ERROR_CMS_RESPONSE} r.make (req, res, api) end diff --git a/src/configuration/cms_setup.e b/src/configuration/cms_setup.e index 02fc1c8..176d4ea 100644 --- a/src/configuration/cms_setup.e +++ b/src/configuration/cms_setup.e @@ -2,8 +2,8 @@ note description: "[ Class that enable to set basic configuration, application environment, core modules and themes. ]" - date: "$Date: 2015-02-13 13:08:13 +0100 (ven., 13 févr. 2015) $" - revision: "$Revision: 96616 $" + date: "$Date$" + revision: "$Revision$" deferred class CMS_SETUP @@ -70,6 +70,13 @@ feature {NONE} -- Initialization files_location := site_location.extended ("files") end + -- Location for temp files + if attached text_item ("temp-dir") as l_temp_dir then + create temp_location.make_from_string (l_temp_dir) + else + temp_location := site_location.extended ("temp") + end + -- Location for modules folders. if attached text_item ("modules-dir") as l_modules_dir then create modules_location.make_from_string (l_modules_dir) @@ -314,6 +321,10 @@ feature -- Access: Theme site_location: PATH -- Path to CMS site root dir. + temp_location: PATH + -- Path to folder used as temporary dir. + -- (Mainly for uploaded file). + files_location: PATH -- Path to public "files" dir. diff --git a/src/service/cms_api.e b/src/service/cms_api.e index 446eaab..c97cefd 100644 --- a/src/service/cms_api.e +++ b/src/service/cms_api.e @@ -1,7 +1,7 @@ note description: "API for a CMS" - date: "$Date: 2015-02-13 13:08:13 +0100 (ven., 13 févr. 2015) $" - revision: "$Revision: 96616 $" + date: "$Date$" + revision: "$Revision$" class CMS_API @@ -628,6 +628,12 @@ feature -- Environment/ theme Result := setup.site_location end + temp_location: PATH + -- CMS temp folder location. + do + Result := setup.temp_location + end + files_location: PATH -- CMS public files location. do diff --git a/src/service/cms_execution.e b/src/service/cms_execution.e index 518bb28..47083f8 100644 --- a/src/service/cms_execution.e +++ b/src/service/cms_execution.e @@ -14,6 +14,7 @@ inherit undefine requires_proxy redefine + execute, create_router, router, execute_default, filter_execute, @@ -26,6 +27,8 @@ inherit WSF_ROUTED_URI_TEMPLATE_HELPER + WSF_REQUEST_EXPORTER + REFACTORING_HELPER SHARED_LOGGER @@ -193,7 +196,20 @@ feature -- Settings: router a_router.handle ("/", fhdl, router.methods_GET) end -feature -- Execute Filter +feature -- Request execution + + initialize_execution + -- Initialize CMS execution. + do + request.set_uploaded_file_path (api.temp_location) + end + + execute + -- . + do + initialize_execution + Precursor + end filter_execute (req: WSF_REQUEST; res: WSF_RESPONSE) -- Execute the filter.