Browse Source

WIP preview_info

Guénaël Muller 5 years ago
parent
commit
f345b7b4f4
2 changed files with 40 additions and 12 deletions
  1. 1 0
      setup.py
  2. 39 12
      tracim/views/contents_api/file_controller.py

+ 1 - 0
setup.py View File

@@ -35,6 +35,7 @@ requires = [
35 35
     'filedepot',
36 36
     'babel',
37 37
     'python-slugify',
38
+    'preview-generator',
38 39
     # mail-notifier
39 40
     'mako',
40 41
     'lxml',

+ 39 - 12
tracim/views/contents_api/file_controller.py View File

@@ -32,6 +32,7 @@ from tracim.models.context_models import RevisionInContext
32 32
 from tracim.models.contents import ContentTypeLegacy as ContentType
33 33
 from tracim.models.contents import file_type
34 34
 from tracim.models.revision_protection import new_revision
35
+from preview_generator.manager import PreviewManager
35 36
 
36 37
 FILE_ENDPOINTS_TAG = 'Files'
37 38
 
@@ -44,7 +45,7 @@ class FileController(Controller):
44 45
     @require_content_types([file_type])
45 46
     @hapic.input_path(WorkspaceAndContentIdPathSchema())
46 47
     #@hapic.input_files()
47
-    @hapic.output_file([])
48
+    @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT)  # nopep8
48 49
     def upload_file(self, context, request: TracimRequest, hapic_data=None):
49 50
         # TODO - G.M - 2018-07-05 - Do this endpoint
50 51
         app_config = request.registry.settings['CFG']
@@ -69,14 +70,11 @@ class FileController(Controller):
69 70
                 new_mimetype=file.type,
70 71
                 new_content=file.file,
71 72
             )
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
73
+
74
+        return
77 75
 
78 76
     @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
79
-    @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
77
+    @require_workspace_role(UserRoleInWorkspace.READER)
80 78
     @require_content_types([file_type])
81 79
     @hapic.input_path(WorkspaceAndContentIdPathSchema())
82 80
     @hapic.output_file([])
@@ -100,10 +98,31 @@ class FileController(Controller):
100 98
 
101 99
 
102 100
     # Previews
103
-    # def get_file_preview(self):
104
-    #     # TODO - G.M - 2018-07-05 - Do this endpoint
105
-    #     pass
106
-    
101
+    @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
102
+    @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
103
+    @require_content_types([file_type])
104
+    @hapic.input_path(WorkspaceAndContentIdPathSchema())
105
+    #@hapic.output_file([])
106
+    def get_file_preview_info(self, context, request: TracimRequest, hapic_data=None):  # nopep8
107
+        # TODO - G.M - 2018-07-05 - Do this endpoint
108
+        app_config = request.registry.settings['CFG']
109
+        preview_manager = PreviewManager(app_config.PREVIEW_CACHE_DIR, create_folder=True)  # nopep8
110
+        api = ContentApi(
111
+            current_user=request.current_user,
112
+            session=request.dbsession,
113
+            config=app_config,
114
+        )
115
+        content = api.get_one(
116
+            hapic_data.path.content_id,
117
+            content_type=ContentType.Any
118
+        )
119
+        file_path = api.get_one_revision_filepath(content.revision_id)
120
+        return {
121
+            'nb_pages': preview_manager.get_page_nb(file_path),
122
+            'pdf_preview': preview_manager.has_pdf_preview(file_path),
123
+            'mimetype':  preview_manager.get_mimetype(file_path),
124
+        }
125
+
107 126
     # File infos
108 127
     @hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
109 128
     @require_workspace_role(UserRoleInWorkspace.READER)
@@ -264,10 +283,18 @@ class FileController(Controller):
264 283
         )
265 284
         configurator.add_view(self.get_file_revisions, route_name='file_revisions')  # nopep8
266 285
 
267
-        # get file revisions
286
+        # get file status
268 287
         configurator.add_route(
269 288
             'set_file_status',
270 289
             '/workspaces/{workspace_id}/files/{content_id}/status',  # nopep8
271 290
             request_method='PUT'
272 291
         )
273 292
         configurator.add_view(self.set_file_status, route_name='set_file_status')  # nopep8
293
+
294
+        # get preview info
295
+        configurator.add_route(
296
+            'preview_info',
297
+            '/workspaces/{workspace_id}/files/{content_id}/preview/info',  # nopep8
298
+            request_method='GET'
299
+        )
300
+        configurator.add_view(self.get_file_preview_info, route_name='preview_info')  # nopep8