Browse Source

WIP upload/download

Guénaël Muller 6 years ago
parent
commit
d24a7538eb
1 changed files with 78 additions and 61 deletions
  1. 78 61
      tracim/views/contents_api/file_controller.py

+ 78 - 61
tracim/views/contents_api/file_controller.py View File

2
 import typing
2
 import typing
3
 
3
 
4
 import transaction
4
 import transaction
5
+from depot.io.local import LocalStoredFile
6
+from depot.manager import DepotManager
5
 from pyramid.config import Configurator
7
 from pyramid.config import Configurator
8
+from pyramid.response import FileResponse, FileIter
6
 
9
 
7
 from tracim.exceptions import EmptyLabelNotAllowed
10
 from tracim.exceptions import EmptyLabelNotAllowed
8
 from tracim.models.data import UserRoleInWorkspace
11
 from tracim.models.data import UserRoleInWorkspace
35
 
38
 
36
 class FileController(Controller):
39
 class FileController(Controller):
37
 
40
 
38
-    # # File data
39
-    # @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
40
-    # @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
41
-    # @require_content_types([file_type])
42
-    # @hapic.input_path(WorkspaceAndContentIdPathSchema())
43
-    # #@hapic.input_files()
44
-    # @hapic.output_file([])
45
-    # def upload_file(self, context, request: TracimRequest, hapic_data=None):
46
-    #     # TODO - G.M - 2018-07-05 - Do this endpoint
47
-    #     app_config = request.registry.settings['CFG']
48
-    #     api = ContentApi(
49
-    #         current_user=request.current_user,
50
-    #         session=request.dbsession,
51
-    #         config=app_config,
52
-    #     )
53
-    #     content = api.get_one(
54
-    #         hapic_data.path.content_id,
55
-    #         content_type=ContentType.Any
56
-    #     )
57
-    #     file = request.POST['files']
58
-    #     api.update_file_data(
59
-    #         content,
60
-    #         new_filename=file.filename,
61
-    #         new_mimetype=file.type,
62
-    #         new_content=file.file,
63
-    #     )
64
-    #     return content.depot_file
65
-    #
66
-    # @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
67
-    # @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
68
-    # @require_content_types([file_type])
69
-    # @hapic.input_path(WorkspaceAndContentIdPathSchema())
70
-    # @hapic.output_file([])
71
-    # def download_file(self, context, request: TracimRequest, hapic_data=None):
72
-    #     # TODO - G.M - 2018-07-05 - Do this endpoint
73
-    #     app_config = request.registry.settings['CFG']
74
-    #     api = ContentApi(
75
-    #         current_user=request.current_user,
76
-    #         session=request.dbsession,
77
-    #         config=app_config,
78
-    #     )
79
-    #     content = api.get_one(
80
-    #         hapic_data.path.content_id,
81
-    #         content_type=ContentType.Any
82
-    #     )
83
-    #     return content.depot_file
41
+    # File data
42
+    @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
43
+    @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
44
+    @require_content_types([file_type])
45
+    @hapic.input_path(WorkspaceAndContentIdPathSchema())
46
+    #@hapic.input_files()
47
+    @hapic.output_file([])
48
+    def upload_file(self, context, request: TracimRequest, hapic_data=None):
49
+        # TODO - G.M - 2018-07-05 - Do this endpoint
50
+        app_config = request.registry.settings['CFG']
51
+        api = ContentApi(
52
+            current_user=request.current_user,
53
+            session=request.dbsession,
54
+            config=app_config,
55
+        )
56
+        content = api.get_one(
57
+            hapic_data.path.content_id,
58
+            content_type=ContentType.Any
59
+        )
60
+        file = request.POST['files']
61
+        with new_revision(
62
+                session=request.dbsession,
63
+                tm=transaction.manager,
64
+                content=content
65
+        ):
66
+            api.update_file_data(
67
+                content,
68
+                new_filename=file.filename,
69
+                new_mimetype=file.type,
70
+                new_content=file.file,
71
+            )
72
+        file = DepotManager.get().get(content.depot_file)
73
+        response = request.response
74
+        response.content_type = file.content_type
75
+        response.app_iter = FileIter(file)
76
+        return response
77
+
78
+    @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
79
+    @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
80
+    @require_content_types([file_type])
81
+    @hapic.input_path(WorkspaceAndContentIdPathSchema())
82
+    @hapic.output_file([])
83
+    def download_file(self, context, request: TracimRequest, hapic_data=None):
84
+        # TODO - G.M - 2018-07-05 - Do this endpoint
85
+        app_config = request.registry.settings['CFG']
86
+        api = ContentApi(
87
+            current_user=request.current_user,
88
+            session=request.dbsession,
89
+            config=app_config,
90
+        )
91
+        content = api.get_one(
92
+            hapic_data.path.content_id,
93
+            content_type=ContentType.Any
94
+        )
95
+        file = DepotManager.get().get(content.depot_file)
96
+        response = request.response
97
+        response.content_type = file.content_type
98
+        response.app_iter = FileIter(file)
99
+        return response
100
+
84
 
101
 
85
     # Previews
102
     # Previews
86
     # def get_file_preview(self):
103
     # def get_file_preview(self):
224
         )  # nopep8
241
         )  # nopep8
225
         configurator.add_view(self.update_file_info, route_name='update_file_info')  # nopep8
242
         configurator.add_view(self.update_file_info, route_name='update_file_info')  # nopep8
226
 
243
 
227
-        # # upload new file data
228
-        # configurator.add_route(
229
-        #     'upload_file',
230
-        #     '/workspaces/{workspace_id}/files/{content_id}/file_data',  # nopep8
231
-        #     request_method='PUT'
232
-        # )
233
-        # configurator.add_view(self.upload_file, route_name='upload_file')  # nopep8
234
-        #
235
-        # # download file data
236
-        # configurator.add_route(
237
-        #     'download_file',
238
-        #     '/workspaces/{workspace_id}/files/{content_id}/file_data',  # nopep8
239
-        #     request_method='GET'
240
-        # )
241
-        # configurator.add_view(self.download_file, route_name='download_file')  # nopep8
244
+        # upload new file data
245
+        configurator.add_route(
246
+            'upload_file',
247
+            '/workspaces/{workspace_id}/files/{content_id}/file_data',  # nopep8
248
+            request_method='PUT'
249
+        )
250
+        configurator.add_view(self.upload_file, route_name='upload_file')  # nopep8
251
+
252
+        # download file data
253
+        configurator.add_route(
254
+            'download_file',
255
+            '/workspaces/{workspace_id}/files/{content_id}/file_data',  # nopep8
256
+            request_method='GET'
257
+        )
258
+        configurator.add_view(self.download_file, route_name='download_file')  # nopep8
242
         # get file revisions
259
         # get file revisions
243
         configurator.add_route(
260
         configurator.add_route(
244
             'file_revisions',
261
             'file_revisions',