Code Duplication    Length = 42-46 lines in 2 locations

backend/tracim_backend/views/contents_api/file_controller.py 2 locations

@@ 460-505 (lines=46) @@
457
            as_attachment=hapic_data.query.force_download
458
        )
459
460
    @hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
461
    @require_workspace_role(UserRoleInWorkspace.READER)
462
    @require_content_types([FILE_TYPE])
463
    @hapic.handle_exception(UnavailablePreview, HTTPStatus.BAD_REQUEST)
464
    @hapic.handle_exception(PageOfPreviewNotFound, HTTPStatus.BAD_REQUEST)
465
    @hapic.handle_exception(PreviewDimNotAllowed, HTTPStatus.BAD_REQUEST)
466
    @hapic.input_path(RevisionPreviewSizedPathSchema())
467
    @hapic.input_query(PageQuerySchema())
468
    @hapic.output_file([])
469
    def sized_preview_jpg_revision(self, context, request: TracimRequest, hapic_data=None):  # nopep8
470
        """
471
        Obtain resized jpg preview of a specific revision of content.
472
        """
473
        app_config = request.registry.settings['CFG']
474
        api = ContentApi(
475
            show_archived=True,
476
            show_deleted=True,
477
            current_user=request.current_user,
478
            session=request.dbsession,
479
            config=app_config,
480
        )
481
        content = api.get_one(
482
            hapic_data.path.content_id,
483
            content_type=CONTENT_TYPES.Any_SLUG
484
        )
485
        revision = api.get_one_revision(
486
            revision_id=hapic_data.path.revision_id,
487
            content=content
488
        )
489
        jpg_preview_path = api.get_jpg_preview_path(
490
            content_id=content.content_id,
491
            revision_id=revision.revision_id,
492
            page_number=hapic_data.query.page,
493
            height=hapic_data.path.height,
494
            width=hapic_data.path.width,
495
        )
496
        filename = "{label}_page_{page_number}_{width}x{height}.jpg".format(
497
            label=revision.label,
498
            page_number=hapic_data.query.page,
499
            width=hapic_data.path.width,
500
            height=hapic_data.path.height
501
        )
502
        return HapicFile(
503
            file_path=jpg_preview_path,
504
            filename=filename,
505
            as_attachment=hapic_data.query.force_download
506
        )
507
508
    @hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
@@ 416-457 (lines=42) @@
413
            as_attachment=hapic_data.query.force_download
414
        )
415
416
    @hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
417
    @require_workspace_role(UserRoleInWorkspace.READER)
418
    @require_content_types([FILE_TYPE])
419
    @hapic.handle_exception(UnavailablePreview, HTTPStatus.BAD_REQUEST)
420
    @hapic.handle_exception(PageOfPreviewNotFound, HTTPStatus.BAD_REQUEST)
421
    @hapic.handle_exception(PreviewDimNotAllowed, HTTPStatus.BAD_REQUEST)
422
    @hapic.input_query(PageQuerySchema())
423
    @hapic.input_path(ContentPreviewSizedPathSchema())
424
    @hapic.output_file([])
425
    def sized_preview_jpg(self, context, request: TracimRequest, hapic_data=None):  # nopep8
426
        """
427
        Obtain resized jpg preview of last revision of content.
428
        """
429
        app_config = request.registry.settings['CFG']
430
        api = ContentApi(
431
            show_archived=True,
432
            show_deleted=True,
433
            current_user=request.current_user,
434
            session=request.dbsession,
435
            config=app_config,
436
        )
437
        content = api.get_one(
438
            hapic_data.path.content_id,
439
            content_type=CONTENT_TYPES.Any_SLUG
440
        )
441
        jpg_preview_path = api.get_jpg_preview_path(
442
            content_id=content.content_id,
443
            revision_id=content.revision_id,
444
            page_number=hapic_data.query.page,
445
            height=hapic_data.path.height,
446
            width=hapic_data.path.width,
447
        )
448
        filename = "{label}_page_{page_number}_{width}x{height}.jpg".format(
449
            label=content.label,
450
            page_number=hapic_data.query.page,
451
            width=hapic_data.path.width,
452
            height=hapic_data.path.height
453
        )
454
        return HapicFile(
455
            file_path=jpg_preview_path,
456
            filename=filename,
457
            as_attachment=hapic_data.query.force_download
458
        )
459
460
    @hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])