|
@@ -20,6 +20,9 @@ from tracim.extensions import hapic
|
20
|
20
|
from tracim.lib.core.content import ContentApi
|
21
|
21
|
from tracim.views.controllers import Controller
|
22
|
22
|
from tracim.views.core_api.schemas import FileContentSchema
|
|
23
|
+from tracim.views.core_api.schemas import ContentPreviewSizedPathSchema
|
|
24
|
+from tracim.views.core_api.schemas import RevisionPreviewSizedPathSchema
|
|
25
|
+from tracim.views.core_api.schemas import PageQuerySchema
|
23
|
26
|
from tracim.views.core_api.schemas import WorkspaceAndContentRevisionIdPathSchema # nopep8
|
24
|
27
|
from tracim.views.core_api.schemas import FileRevisionSchema
|
25
|
28
|
from tracim.views.core_api.schemas import SetContentStatusSchema
|
|
@@ -126,45 +129,177 @@ class FileController(Controller):
|
126
|
129
|
@hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
|
127
|
130
|
@require_workspace_role(UserRoleInWorkspace.READER)
|
128
|
131
|
@require_content_types([file_type])
|
|
132
|
+ @hapic.input_query(PageQuerySchema())
|
|
133
|
+ @hapic.input_path(WorkspaceAndContentIdPathSchema())
|
129
|
134
|
@hapic.output_file([])
|
130
|
135
|
def preview_pdf(self, context, request: TracimRequest, hapic_data=None):
|
131
|
|
- raise NotImplemented()
|
|
136
|
+ app_config = request.registry.settings['CFG']
|
|
137
|
+ preview_manager = PreviewManager(app_config.PREVIEW_CACHE_DIR, create_folder=True) # nopep8
|
|
138
|
+ api = ContentApi(
|
|
139
|
+ current_user=request.current_user,
|
|
140
|
+ session=request.dbsession,
|
|
141
|
+ config=app_config,
|
|
142
|
+ )
|
|
143
|
+ content = api.get_one(
|
|
144
|
+ hapic_data.path.content_id,
|
|
145
|
+ content_type=ContentType.Any
|
|
146
|
+ )
|
|
147
|
+ file_path = api.get_one_revision_filepath(content.revision_id)
|
|
148
|
+ if hapic_data.query.page >= preview_manager.get_page_nb(file_path):
|
|
149
|
+ raise Exception('page {page} of content {content_id} does not exist'.format(
|
|
150
|
+ page=hapic_data.query.page,
|
|
151
|
+ content_id=content.content_id),
|
|
152
|
+ )
|
|
153
|
+ pdf_preview_path = preview_manager.get_pdf_preview(file_path, page=hapic_data.query.page) # nopep8
|
|
154
|
+ return FileResponse(pdf_preview_path)
|
132
|
155
|
|
133
|
156
|
@hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
|
134
|
157
|
@require_workspace_role(UserRoleInWorkspace.READER)
|
135
|
158
|
@require_content_types([file_type])
|
|
159
|
+ @hapic.input_path(WorkspaceAndContentIdPathSchema())
|
136
|
160
|
@hapic.output_file([])
|
137
|
161
|
def preview_pdf_full(self, context, request: TracimRequest, hapic_data=None):
|
138
|
|
- raise NotImplemented()
|
|
162
|
+ app_config = request.registry.settings['CFG']
|
|
163
|
+ preview_manager = PreviewManager(app_config.PREVIEW_CACHE_DIR, create_folder=True) # nopep8
|
|
164
|
+ api = ContentApi(
|
|
165
|
+ current_user=request.current_user,
|
|
166
|
+ session=request.dbsession,
|
|
167
|
+ config=app_config,
|
|
168
|
+ )
|
|
169
|
+ content = api.get_one(
|
|
170
|
+ hapic_data.path.content_id,
|
|
171
|
+ content_type=ContentType.Any
|
|
172
|
+ )
|
|
173
|
+ file_path = api.get_one_revision_filepath(content.revision_id)
|
|
174
|
+ pdf_preview_path = preview_manager.get_pdf_preview(file_path)
|
|
175
|
+ return FileResponse(pdf_preview_path)
|
139
|
176
|
|
140
|
177
|
@hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
|
141
|
178
|
@require_workspace_role(UserRoleInWorkspace.READER)
|
142
|
179
|
@require_content_types([file_type])
|
|
180
|
+ @hapic.input_path(WorkspaceAndContentRevisionIdPathSchema())
|
|
181
|
+ @hapic.input_query(PageQuerySchema())
|
143
|
182
|
@hapic.output_file([])
|
144
|
183
|
def preview_pdf_revision(self, context, request: TracimRequest, hapic_data=None):
|
145
|
|
- raise NotImplemented()
|
|
184
|
+ app_config = request.registry.settings['CFG']
|
|
185
|
+ preview_manager = PreviewManager(app_config.PREVIEW_CACHE_DIR, create_folder=True) # nopep8
|
|
186
|
+ api = ContentApi(
|
|
187
|
+ current_user=request.current_user,
|
|
188
|
+ session=request.dbsession,
|
|
189
|
+ config=app_config,
|
|
190
|
+ )
|
|
191
|
+ content = api.get_one(
|
|
192
|
+ hapic_data.path.content_id,
|
|
193
|
+ content_type=ContentType.Any
|
|
194
|
+ )
|
|
195
|
+ revision = api.get_one_revision(
|
|
196
|
+ revision_id=hapic_data.path.revision_id,
|
|
197
|
+ content=content
|
|
198
|
+ )
|
|
199
|
+ file_path = api.get_one_revision_filepath(revision.revision_id)
|
|
200
|
+ if hapic_data.query.page >= preview_manager.get_page_nb(file_path):
|
|
201
|
+ raise Exception('page {page} of content {content_id} does not exist'.format(
|
|
202
|
+ page=hapic_data.query.page,
|
|
203
|
+ content_id=content.content_id),
|
|
204
|
+ )
|
|
205
|
+ pdf_preview_path = preview_manager.get_pdf_preview(file_path, page=hapic_data.query.page) # nopep8
|
|
206
|
+ return FileResponse(pdf_preview_path)
|
146
|
207
|
|
147
|
208
|
# jpg
|
148
|
209
|
@hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
|
149
|
210
|
@require_workspace_role(UserRoleInWorkspace.READER)
|
150
|
211
|
@require_content_types([file_type])
|
|
212
|
+ @hapic.input_path(WorkspaceAndContentIdPathSchema())
|
|
213
|
+ @hapic.input_query(PageQuerySchema())
|
151
|
214
|
@hapic.output_file([])
|
152
|
215
|
def preview_jpg(self, context, request: TracimRequest, hapic_data=None):
|
153
|
|
- raise NotImplemented()
|
|
216
|
+ app_config = request.registry.settings['CFG']
|
|
217
|
+ preview_manager = PreviewManager(app_config.PREVIEW_CACHE_DIR, create_folder=True) # nopep8
|
|
218
|
+ api = ContentApi(
|
|
219
|
+ current_user=request.current_user,
|
|
220
|
+ session=request.dbsession,
|
|
221
|
+ config=app_config,
|
|
222
|
+ )
|
|
223
|
+ content = api.get_one(
|
|
224
|
+ hapic_data.path.content_id,
|
|
225
|
+ content_type=ContentType.Any
|
|
226
|
+ )
|
|
227
|
+ file_path = api.get_one_revision_filepath(content.revision_id)
|
|
228
|
+ if hapic_data.query.page >= preview_manager.get_page_nb(file_path):
|
|
229
|
+ raise Exception('page {page} of content {content_id} does not exist'.format(
|
|
230
|
+ page=hapic_data.query.page,
|
|
231
|
+ content_id=content.content_id),
|
|
232
|
+ )
|
|
233
|
+ jpg_preview_path = preview_manager.get_jpeg_preview(file_path, page=hapic_data.query.page) # nopep8
|
|
234
|
+ return FileResponse(jpg_preview_path)
|
154
|
235
|
|
155
|
236
|
@hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
|
156
|
237
|
@require_workspace_role(UserRoleInWorkspace.READER)
|
157
|
238
|
@require_content_types([file_type])
|
|
239
|
+ @hapic.input_query(PageQuerySchema())
|
|
240
|
+ @hapic.input_path(ContentPreviewSizedPathSchema())
|
158
|
241
|
@hapic.output_file([])
|
159
|
242
|
def sized_preview_jpg(self, context, request: TracimRequest, hapic_data=None):
|
160
|
|
- raise NotImplemented()
|
|
243
|
+ app_config = request.registry.settings['CFG']
|
|
244
|
+ preview_manager = PreviewManager(app_config.PREVIEW_CACHE_DIR, create_folder=True) # nopep8
|
|
245
|
+ api = ContentApi(
|
|
246
|
+ current_user=request.current_user,
|
|
247
|
+ session=request.dbsession,
|
|
248
|
+ config=app_config,
|
|
249
|
+ )
|
|
250
|
+ content = api.get_one(
|
|
251
|
+ hapic_data.path.content_id,
|
|
252
|
+ content_type=ContentType.Any
|
|
253
|
+ )
|
|
254
|
+ file_path = api.get_one_revision_filepath(content.revision_id)
|
|
255
|
+ if hapic_data.query.page >= preview_manager.get_page_nb(file_path):
|
|
256
|
+ raise Exception('page {page} of content {content_id} does not exist'.format(
|
|
257
|
+ page=hapic_data.query.page,
|
|
258
|
+ content_id=content.content_id),
|
|
259
|
+ )
|
|
260
|
+ jpg_preview_path = preview_manager.get_jpeg_preview(
|
|
261
|
+ file_path,
|
|
262
|
+ page=hapic_data.query.page,
|
|
263
|
+ width=hapic_data.path.width,
|
|
264
|
+ height=hapic_data.path.height,
|
|
265
|
+ )
|
|
266
|
+ return FileResponse(jpg_preview_path)
|
161
|
267
|
|
162
|
268
|
@hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
|
163
|
269
|
@require_workspace_role(UserRoleInWorkspace.READER)
|
164
|
270
|
@require_content_types([file_type])
|
|
271
|
+ @hapic.input_path(RevisionPreviewSizedPathSchema())
|
|
272
|
+ @hapic.input_query(PageQuerySchema())
|
165
|
273
|
@hapic.output_file([])
|
166
|
274
|
def sized_preview_jpg_revision(self, context, request: TracimRequest, hapic_data=None):
|
167
|
|
- raise NotImplemented()
|
|
275
|
+ app_config = request.registry.settings['CFG']
|
|
276
|
+ preview_manager = PreviewManager(app_config.PREVIEW_CACHE_DIR, create_folder=True) # nopep8
|
|
277
|
+ api = ContentApi(
|
|
278
|
+ current_user=request.current_user,
|
|
279
|
+ session=request.dbsession,
|
|
280
|
+ config=app_config,
|
|
281
|
+ )
|
|
282
|
+ content = api.get_one(
|
|
283
|
+ hapic_data.path.content_id,
|
|
284
|
+ content_type=ContentType.Any
|
|
285
|
+ )
|
|
286
|
+ revision = api.get_one_revision(
|
|
287
|
+ revision_id=hapic_data.path.revision_id,
|
|
288
|
+ content=content
|
|
289
|
+ )
|
|
290
|
+ file_path = api.get_one_revision_filepath(revision.revision_id)
|
|
291
|
+ if hapic_data.query.page >= preview_manager.get_page_nb(file_path):
|
|
292
|
+ raise Exception('page {page} of content {content_id} does not exist'.format(
|
|
293
|
+ page=hapic_data.query.page,
|
|
294
|
+ content_id=content.content_id),
|
|
295
|
+ )
|
|
296
|
+ jpg_preview_path = preview_manager.get_jpeg_preview(
|
|
297
|
+ file_path,
|
|
298
|
+ page=hapic_data.query.page,
|
|
299
|
+ width=hapic_data.path.width,
|
|
300
|
+ height=hapic_data.path.height,
|
|
301
|
+ )
|
|
302
|
+ return FileResponse(jpg_preview_path)
|
168
|
303
|
|
169
|
304
|
@hapic.with_api_doc(tags=[FILE_ENDPOINTS_TAG])
|
170
|
305
|
@require_workspace_role(UserRoleInWorkspace.READER)
|