Skip to content
Newer
Older
100644 185 lines (161 sloc) 5.42 KB
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
1 /*
64801b4 @Aqua-Ye [enhance] bootstrap: improved the import system again, with fixes for…
Aqua-Ye authored Mar 12, 2012
2 Copyright © 2011, 2012 MLstate
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 */
a21882c @akoprow [enhance] Upload: Starting to re-factor the Upload module.
akoprow authored Jun 13, 2011
18
19 package stdlib.upload
20
21 /**
22 * @author Quentin Bourgerie
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
23 * @author Adam Koprowski (re-packaging, docs, re-implementation)
a21882c @akoprow [enhance] Upload: Starting to re-factor the Upload module.
akoprow authored Jun 13, 2011
24 *
25 *
26 * {1 About this module}
27 *
28 * This module provides functionality to create a form containing
29 * a file upload field.
30 **/
31
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
32 /**
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
33 * Representation of an uploaded file.
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
34 */
35 type Upload.file = {
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
36
37 /** Name of the input field corresponding to the uploaded file. */
59463c9 @akoprow [enhance] WFormBuilder: Big refactoring.
akoprow authored Jun 14, 2011
38 field_name : string
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
39
40 /** Name of the uploaded file. */
59463c9 @akoprow [enhance] WFormBuilder: Big refactoring.
akoprow authored Jun 14, 2011
41 filename : string
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
42
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
43 /** The mimetype of the file */
44 mimetype : string
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
45
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
46 /** Content of the file. */
47 content : binary
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
48 }
49
50 /**
59463c9 @akoprow [enhance] WFormBuilder: Big refactoring.
akoprow authored Jun 14, 2011
51 * A data of the form created with this module
52 */
53 type Upload.form_data = {
54 /** data of the uploaded file (if missing then something went wrong
55 with form submission) */
434495f @akoprow [enhance] WFormBuilder: Finished refactoring of FormBuilder.
akoprow authored Jun 15, 2011
56 uploaded_files : stringmap(Upload.file)
59463c9 @akoprow [enhance] WFormBuilder: Big refactoring.
akoprow authored Jun 14, 2011
57
58 /** data of other fields (map from field id to field value) */
59 form_fields : stringmap(string)
60 }
61
62 /**
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
63 * Configuration of the file uploader.
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
64 */
59463c9 @akoprow [enhance] WFormBuilder: Big refactoring.
akoprow authored Jun 14, 2011
65 type Upload.config = {
66
67 /** id of the form */
68 form_id : string
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
69
70 /** Parameters for the dynamic url to be created by the upload manager. */
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
71 url_parameters : DynamicResource.parameters
72
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
73 /** The content of the form.
74 * It can contain many fields, among which there should be one field
75 * for file upload:
76 * {[
77 * <input type="file" id="..." name="..." />
78 * ]}
79 */
a1a7a8b @akoprow [enhance] WFormBuilder: Another big refactorization.
akoprow authored Jun 21, 2011
80 form_body : xhtml
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
81
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
82 /** A function that will be invoked upon uploading the file */
59463c9 @akoprow [enhance] WFormBuilder: Big refactoring.
akoprow authored Jun 14, 2011
83 process : Upload.form_data -> void
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
84 }
85
86 /**
87 * This module provides one main [make] function which create an xhtml
88 * that allows to upload file(s) from client to server.
89 */
90 Upload = {{
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
91
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
92 /**
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
93 * The default configuration of a file upload widget.
94 * It creates a form with only one file upload field and
95 * does nothing upon retrieving the file.
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
96 */
59463c9 @akoprow [enhance] WFormBuilder: Big refactoring.
akoprow authored Jun 14, 2011
97 default_config() : Upload.config = {
98 form_id = Dom.fresh_id()
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
99 url_parameters = {
100 expiration={none}
101 consumption={unlimited}
102 visibility={current_context}
103 }
a1a7a8b @akoprow [enhance] WFormBuilder: Another big refactorization.
akoprow authored Jun 21, 2011
104 form_body =
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
105 <input type="file" name="filename"/>
106 <input type="submit" value="Upload"/>
107 process(_) = void
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
108 }
109
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
110 /* Folding on the data of the multipart form data */
111 @private
434495f @akoprow [enhance] WFormBuilder: Finished refactoring of FormBuilder.
akoprow authored Jun 15, 2011
112 multifold(part, fold_headers, form_data) =
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
113 match part with
114 // fileupload field -- process
115 | ~{filename name content} ->
116 find_mime(header, value, acc) =
117 match header with
118 | "Content_Type" -> value
119 | _ -> acc
449ad7a @akoprow [feature] WFormBuilder: Adding file upload to the FormBuilder.
akoprow authored Jun 14, 2011
120 /* A hack for forall, TODO make it proprely */
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
121 mimetype = @unsafe_cast(fold_headers)("text/plain", find_mime)
122 field_name = name
123 full_content =
124 rec aux() =
125 match content() with
126 | {partial=_} ->
127 do Scheduler.wait(500)
128 aux()
129 | ~{content} -> content
130 aux()
131 file = ~{filename field_name mimetype content=full_content}
434495f @akoprow [enhance] WFormBuilder: Finished refactoring of FormBuilder.
akoprow authored Jun 15, 2011
132 { form_data with
133 uploaded_files=Map.add(name, file, form_data.uploaded_files) }
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
134 // "regular" field -- ignore
59463c9 @akoprow [enhance] WFormBuilder: Big refactoring.
akoprow authored Jun 14, 2011
135 | ~{name value} ->
434495f @akoprow [enhance] WFormBuilder: Finished refactoring of FormBuilder.
akoprow authored Jun 15, 2011
136 { form_data with
137 form_fields=Map.add(name, value, form_data.form_fields) }
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
138
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
139 /**
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
140 * Creates a form with file upload capabilities.
141 *
142 * @param config Configuration of the file-upload widget.
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
143 */
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
144 html(config : Upload.config) : xhtml =
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
145 /* Save creation page context because iframe is another page. */
146 reset_context =
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
147 match ThreadContext.get({current}) with
51f92b4 [feature] adding: a no_client_calls directive
Hugo Heuzard authored Sep 9, 2011
148 | {key=~{client} request=_ details=_ constraint=_} ->
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
149 key = ~{client}
150 ( -> { ThreadContext.get({current}) with ~key })
151 | _ ->
152 ( -> ThreadContext.get({current}))
153 /* The dynamic resource used for the upload. */
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
154 dynamic(request) =
155 @with_thread_context(reset_context(),
156 match HttpRequest.Generic.get_multipart(request) with
157 | {none} ->
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
158 Resource.error_page("Upload failed", <h1>Unexpected load request</h1>,
159 {forbidden})
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
160 | {some = multipart} ->
434495f @akoprow [enhance] WFormBuilder: Finished refactoring of FormBuilder.
akoprow authored Jun 15, 2011
161 empty_form_data = { form_fields=Map.empty uploaded_files=Map.empty }
162 form_data = HttpRequest.Generic.fold_multipart(multipart,
163 empty_form_data, multifold)
164 do Scheduler.push( -> config.process(form_data))
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
165 Resource.source("Upload successful", "text/plain")
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
166 )
167 resource = Resource.dynamic(dynamic)
64801b4 @Aqua-Ye [enhance] bootstrap: improved the import system again, with fixes for…
Aqua-Ye authored Mar 12, 2012
168 upload_url = DynamicResource.publish("", resource, config.url_parameters)
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
169 frame_style = css {
170 width: 0px;
171 height: 0px;
172 display: none;
173 border: 0px solid white
174 }
449ad7a @akoprow [feature] WFormBuilder: Adding file upload to the FormBuilder.
akoprow authored Jun 14, 2011
175 frameId = Dom.fresh_id()
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
176 <>
6256b85 [fix] stdlib: remove dummy_resources and fake_urls
Hugo Heuzard authored Sep 12, 2011
177 <iframe name={frameId} id={frameId} src="{Resource.get_uri_of_null}" style={frame_style} />
59463c9 @akoprow [enhance] WFormBuilder: Big refactoring.
akoprow authored Jun 14, 2011
178 <form id={config.form_id} action="{upload_url}" target={frameId}
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
179 method="post" enctype="multipart/form-data">
a1a7a8b @akoprow [enhance] WFormBuilder: Another big refactorization.
akoprow authored Jun 21, 2011
180 {config.form_body}
f116151 @akoprow [enhance] Upload: Simplified API of the upload widget.
akoprow authored Jun 13, 2011
181 </form>
182 </>
4f8969f [fix] stdlib: upload
Hugo Heuzard authored Jun 23, 2011
183
fccc685 Initial open-source release
MLstate authored Jun 21, 2011
184 }}
Something went wrong with that request. Please try again.