14 # details. |
14 # details. |
15 # |
15 # |
16 # You should have received a copy of the GNU Lesser General Public License along |
16 # You should have received a copy of the GNU Lesser General Public License along |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 |
18 |
|
19 import hashlib |
|
20 |
19 from cubicweb.predicates import has_related_entities |
21 from cubicweb.predicates import has_related_entities |
20 from cubicweb.web.views.ajaxcontroller import ajaxfunc |
22 from cubicweb.web.views.ajaxcontroller import ajaxfunc |
21 from cubicweb.web.views.ibreadcrumbs import IBreadCrumbsAdapter |
23 from cubicweb.web.views.ibreadcrumbs import IBreadCrumbsAdapter |
22 |
24 |
23 def _recursive_replace_stream_by_content(tree): |
25 def _recursive_replace_stream_by_md5(tree): |
24 """ Search for streams (i.e. object that have a 'read' method) in a tree |
26 """ Search for streams (i.e. object that have a 'read' method) in a tree |
25 (which branches are lists or tuples), and substitute them by their content, |
27 (whose branches are lists or tuples), and substitute them by their md5 hash, |
26 leaving other leafs identical. A copy of the tree with only lists as |
28 leaving other leafs identical. A copy of the tree with only lists as |
27 branches is returned. |
29 branches is returned. |
28 """ |
30 """ |
29 if not isinstance(tree, (list, tuple)): |
31 if not isinstance(tree, (list, tuple)): |
30 if hasattr(tree, 'read'): |
32 if hasattr(tree, 'read'): |
31 return tree.read() |
33 return hashlib.md5(tree.read()).hexdigest() |
32 return tree |
34 return tree |
33 else: |
35 else: |
34 return [_recursive_replace_stream_by_content(value) |
36 return [_recursive_replace_stream_by_md5(value) |
35 for value in tree] |
37 for value in tree] |
36 |
38 |
37 |
39 |
38 @ajaxfunc(output_type='json') |
40 @ajaxfunc(output_type='json') |
39 def fileupload(self): |
41 def fileupload(self): |
40 """ Return a json copy of the web request form in which uploaded files |
42 """ Return a json copy of the web request form in which uploaded files |
41 are read and the received streams are replaced by their content. |
43 are read and the received streams are replaced by their md5 hash. |
42 """ |
44 """ |
43 try: |
45 try: |
44 result_dict = {} |
46 result_dict = {} |
45 for key, value in self._cw.form.items(): |
47 for key, value in self._cw.form.items(): |
46 result_dict[key] = _recursive_replace_stream_by_content(value) |
48 result_dict[key] = _recursive_replace_stream_by_md5(value) |
47 return result_dict |
49 return result_dict |
48 except Exception as ex: |
50 except Exception as ex: |
49 import traceback as tb |
51 import traceback as tb |
50 tb.print_exc(ex) |
52 tb.print_exc(ex) |
51 |
53 |