First setup of file_upload module analogous to the upload_image example

This commit is contained in:
fmurer
2016-01-04 21:11:16 +01:00
parent e1ae9f9eec
commit 7e9c29f277
6 changed files with 205 additions and 233 deletions

View File

@@ -1,5 +1,5 @@
note
description: "cms_file_upload application root class"
description: "file_upload application root class"
date: "$Date$"
revision: "$Revision$"
@@ -7,44 +7,221 @@ class
CMS_FILE_UPLOAD
inherit
CMS_NODE
CMS_MODULE
redefine
make_empty
install,
initialize,
setup_hooks
end
CMS_HOOK_BLOCK
CMS_HOOK_MENU_SYSTEM_ALTER
WSF_ROUTED_URI_TEMPLATE_HELPER
SHARED_EXECUTION_ENVIRONMENT
create
make_empty,
make
feature {NONE} -- Initialisation
feature {NONE} -- Initialization
make_empty
make
do
Precursor
name := "file_uploader"
version := "1.0"
description := "Service to upload some files"
package := "file upload"
end
feature -- Access
content_type: READABLE_STRING_8
name: STRING
feature -- Access: node
feature {CMS_API} -- Module Initialization
summary: detachable READABLE_STRING_32
-- A short summary of the node.
content: detachable READABLE_STRING_32
-- Content of the node.
format: detachable READABLE_STRING_8
-- Format associated with `content' and `summary'.
-- For example: text, mediawiki, html, etc
feature -- Element change: node
set_content (a_content: like content; a_summary: like summary; a_format: like format)
initialize (api: CMS_API)
-- <Precursor>
do
content := a_content
summary := a_summary
format := a_format
Precursor (api)
end
feature -- Module management
install (api: CMS_API)
-- install the module
local
sql: STRING
do
-- create a database table
if attached {CMS_STORAGE_SQL_I} api.storage as l_sql_storage then
if not l_sql_storage.sql_table_exists ("file_upload_table") then
sql := "[
CREATE TABLE file_upload_table(
`id` INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL CHECK("id">=0),
`name` VARCHAR(100) NOT NULL,
`uploaded_date` DATE,
`size` INTEGER
);
]"
l_sql_storage.sql_execute_script (sql, Void)
if l_sql_storage.has_error then
api.logger.put_error ("Could not initialize database for file uploader module", generating_type)
end
end
Precursor {CMS_MODULE}(api)
end
end
feature -- Access: router
setup_router (a_router: WSF_ROUTER; a_api: CMS_API)
-- <Precursor>
local
www: WSF_FILE_SYSTEM_HANDLER
do
map_uri_template_agent ("/upload{?nb}", agent execute_upload_handler, void)
create www.make_with_path (document_root)
www.set_directory_index (<<"index.html">>)
www.set_not_found_handler (agent execute_not_found)
a_router.handle("", www, a_router.methods_get)
end
feature -- Hooks
setup_hooks (a_hooks: CMS_HOOK_CORE_MANAGER)
do
end
block_list: ITERABLE [like {CMS_BLOCK}.name]
do
end
get_block_view (a_block_id: READABLE_STRING_8; a_response: CMS_RESPONSE)
do
end
menu_system_alter (a_menu_system: CMS_MENU_SYSTEM; a_response: CMS_RESPONSE)
do
end
feature -- Configuration
document_root: PATH
-- Document root to look for files or directories
once
Result := execution_environment.current_working_path.extended ("docs")
end
files_root: PATH
-- Uploaded files will be stored in `files_root' folder
once
Result := document_root.extended ("files")
end
feature -- Handler
execute_not_found_handler (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. %N Redirectioin to" + req.script_url ("/"), "text/html")
end
execute_upload_handler(req: WSF_REQUEST; res: WSF_RESPONSE; a_api: CMS_API)
local
body: STRING_8
safe_filename: STRING_8
path: 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 body.make_empty
page.set_body (body)
-- create the body
body.append ("<h1> EWF: Upload files </h1>%N")
body.append ("<form action=%"" + req.script_url ("/upload") + "%" method=%"POST%" enctype=%"multipart/form-data%">%N")
-- tetermine how many files to upload by a query parameter ?nb=number_of_files
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
-- llist for the number of wanted files a upload button
from
until
n = 0
loop
body.append ("<input type=%"file%" name=%"uploaded_file[]%" size=%"60%"></br> %N")
n := n-1
end
-- set the submit button
body.append ("<button type=%"submit%">UPLOAD</button>%N")
res.send (page)
else
create body.make_empty
body.append ("<h1>EWF: Uploaded files</h1>%N")
body.append ("<ul>%N")
n := 0
across
req.uploaded_files as u_file
loop
body.append ("<li>%N")
body.append ("<div>" + u_file.item.name + "=" + html_encode (u_file.item.filename) + " size=" + u_file.item.size.out + " type" + u_file.item.content_type + "</div> %N")
safe_filename := u_file.item.safe_filename
path := files_root.extended (safe_filename)
-- TODO: list dhe uploaded items
body.append ("</li> %N")
end
body.append ("</ul> %N")
-- create page
create page.make
page.add_style ("../style.css", "all")
page.set_body (body)
res.send (page)
end
end
feature {NONE} -- Encoder
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

View File

@@ -1,46 +0,0 @@
note
description: "Summary description for {CMS_FILE_UPLOAD_API}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
CMS_FILE_UPLOAD_API
inherit
CMS_MODULE_API
rename
make as make_with_cms_api
redefine
initialize
end
create
make
feature {NONE} -- Initialization
make (a_api: CMS_API; a_node_api: CMS_NODE_API)
-- (from CMS_MODULE_API)
do
node_api := a_node_api
make_with_cms_api(a_api)
end
initialize
do
Precursor
-- create the storage of type file
if attached storage.as_sql_storage as l_storage_sql then
create {CMS_FILE_UPLOAD_STORAGE_SQL} file_storage.make (l_storage_sql)
end
end
feature -- Access
nod_api: CMS_NODE_API
file_storage: CMS_FILE_UPLOAD_STORAGE_I
end

View File

@@ -1,101 +0,0 @@
note
description: "Summary description for {CMS_FILE_UPLOAD_MODULE}."
author: "fmurer"
date: "$Date$"
revision: "$Revision$"
class
CMS_FILE_UPLOAD_MODULE
inherit
CMS_MODULE
rename
module_api as file_upload_api
redefine
install,
register_hooks
end
CMS_HOOK_BLOCK
CMS_HOOK_MENU_SYSTEM_ALTER
create
make
feature {NONE} -- Initialisation
make
do
version := "1.0"
description := "Service to upload a file"
package := "file_upload"
add_dependency({CMS_NODE_MODULE})
end
feature -- Access
name: STRING = "file_uploader"
feature -- Access: router
setup_router(a_router: WSF_ROUTER; a_api: CMS_API)
-- <Precurser>
do
end
feature -- Module Management
install (api: CMS_API)
-- install the module
local
sql_query: STRING
do
-- create the database
if attached api.storage.as_sql_storage as l_sql_storage then
if not l_sql_storage.sql_table_exists ("file_nodes") then
sql := "[
CREATE TABLE file_nodes(
`nid` INTEGER NOT NULL CHECK("nid">=0),
`type` VARCHAR(255) NOT NULL,
CONSTRAINT unique_id PRIMARY KEY nid
);
]"
l_sql_storage.sql_execute_script (sql, void)
if l_sql_storage.has_error then
api.logger.put_error ("Could not initialize database for file_uploader module", generating_type)
end
end
Precursor (api)
end
end
feature -- Hooks
register_hooks (a_response: CMS_RESPONSE)
-- register the hooks
do
a_response.subscribe_to_menu_system_alter_block(Current)
a_response.subscribe_to_block_hook(Current)
end
block_list: ITERABLE [like {CMS_BLOCK}.name]
do
-- List of block names, managed by current object.
end
get_block_view (a_block_id: READABLE_STRING_8; a_response: CMS_RESPONSE)
do
-- Get block object identified by `a_block_id' and associate with `a_response'.
end
menu_system_alter (a_menu_system: CMS_MENU_SYSTEM; a_response: CMS_RESPONSE)
local
link: CMS_LOCAL_LINK
do
create link.make ("Files", "/uploaded_files")
a_menu_system.primary_menu.extend (link)
end
end

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-15-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-15-0 http://www.eiffel.com/developers/xml/configuration-1-15-0.xsd" name="cms_file_upload" uuid="1059EB41-2A5D-4B40-8452-EFE313DA18AD">
<target name="cms_file_upload">
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-15-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-15-0 http://www.eiffel.com/developers/xml/configuration-1-15-0.xsd" name="file_uploader" uuid="795C88E5-9218-4F35-A985-5501340E2D9D">
<target name="file_uploader">
<root class="CMS_FILE_UPLOAD" feature="make"/>
<option warning="true">
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
@@ -8,20 +8,14 @@
<setting name="console_application" value="true"/>
<precompile name="base_pre" location="$ISE_PRECOMP\base-safe.ecf"/>
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
<library name="base_extension" location="$ISE_LIBRARY\library\base_extension\base_extension-safe.ecf"/>
<library name="cms" location="\home\fmurer\Documents\EWF_ROC\ROC\cms-safe.ecf"/>
<library name="cms_model" location="\home\fmurer\Documents\EWF_ROC\ROC\library\model\cms_model-safe.ecf"/>
<library name="error" location="$ISE_LIBRARY\contrib\library\utility\general\error\error-safe.ecf"/>
<library name="http" location="$ISE_LIBRARY\contrib\library\network\protocol\http\http-safe.ecf"/>
<library name="json" location="$ISE_LIBRARY\contrib\library\text\parser\json\library\json-safe.ecf"/>
<library name="node" location="\home\fmurer\Documents\EWF_ROC\ROC\modules\node\node-safe.ecf"/>
<library name="text_filter" location="$ISE_LIBRARY\unstable\library\text\text_filter\text_filter-safe.ecf"/>
<library name="time" location="$ISE_LIBRARY\library\time\time-safe.ecf"/>
<library name="wsf" location="$ISE_LIBRARY\contrib\library\web\framework\ewf\wsf\wsf-safe.ecf"/>
<library name="wsf_encoder" location="\home\fmurer\Documents\EWF_ROC\eiffelstudio-src\contrib\library\web\framework\ewf\text\encoder\encoder-safe.ecf"/>
<library name="wsf_extension" location="$ISE_LIBRARY\contrib\library\web\framework\ewf\wsf\wsf_extension-safe.ecf"/>
<library name="wsf_html" location="$ISE_LIBRARY\contrib\library\web\framework\ewf\wsf_html\wsf_html-safe.ecf"/>
<cluster name="cms_file_upload" location=".\" recursive="true">
<cluster name="file_uploader" location=".\" recursive="true">
<file_rule>
<exclude>/.svn$</exclude>
<exclude>/CVS$</exclude>

View File

@@ -1,22 +0,0 @@
note
description: "Summary description for {CMS_FILE_UPLOAD_STORAGE_I}."
author: "fmurer"
date: "$Date$"
revision: "$Revision$"
deferred class
CMS_FILE_UPLOAD_STORAGE_I
feature -- Access
files_count: INTEGER_64
-- count of files
deferred
end
files: LIST [CMS_NODE]
-- List of files
deferred
end
end

View File

@@ -1,30 +0,0 @@
note
description: "Summary description for {CMS_FILE_UPLOAD_STORAGE_SQL}."
author: "fmurer"
date: "$Date$"
revision: "$Revision$"
class
CMS_FILE_UPLOAD_STORAGE_SQL
inherit
CMS_NODE_STORAGE_SQL
CMS_FILE_UPLOAD_STORAGE_I
create
make
feature
files_count: INTEGER_64
-- Precursor
do
end
files: LIST [CMS_NODE]
do
end
end