示例#1
0
 def get_one(self,
             page_id: str='-1',
             revision_id: str=None,
             size: int=300,
             *args, **kwargs):
     file_id = int(tg.request.controller_state.routing_args.get('file_id'))
     page = int(page_id)
     revision_id = int(revision_id) if revision_id != 'latest' else None
     cache_path = CFG.get_instance().PREVIEW_CACHE_DIR
     preview_manager = PreviewManager(cache_path, create_folder=True)
     user = tmpl_context.current_user
     content_api = ContentApi(user,
                              show_archived=True,
                              show_deleted=True)
     if revision_id:
         file_path = content_api.get_one_revision_filepath(revision_id)
     else:
         file = content_api.get_one(file_id, self._item_type)
         file_path = content_api.get_one_revision_filepath(file.revision_id)
     try:
         path = preview_manager.get_jpeg_preview(file_path=file_path,
                                                 page=page,
                                                 height=size,
                                                 width=size)
         with open(path, 'rb') as large:
             result = large.read()
     except PreviewGeneratorException:
         result = None
     return result
示例#2
0
 def download_pdf_one(self,
                      page_id: str,
                      revision_id: str=None,
                      *args, **kwargs):
     file_id = int(tg.request.controller_state.routing_args.get('file_id'))
     revision_id = int(revision_id) if revision_id != 'latest' else None
     page = int(page_id)
     cache_path = CFG.get_instance().PREVIEW_CACHE_DIR
     preview_manager = PreviewManager(cache_path, create_folder=True)
     user = tmpl_context.current_user
     content_api = ContentApi(user,
                              show_archived=True,
                              show_deleted=True)
     file = content_api.get_one(file_id, self._item_type)
     if revision_id:
         file_path = content_api.get_one_revision_filepath(revision_id)
     else:
         file = content_api.get_one(file_id, self._item_type)
         file_path = content_api.get_one_revision_filepath(file.revision_id)
     path = preview_manager.get_pdf_preview(file_path=file_path,
                                            page=page)
     file_suffix = ''
     if page > -1:
         file_suffix = '.page-{}'.format(page + 1)
     tg.response.headers['Content-Disposition'] = \
         'attachment; filename="{}{}.pdf"'.format(
             file.label,
             file_suffix,
         )
     with open(path, 'rb') as pdf:
         return pdf.read()
示例#3
0
def test_algorithm4() -> None:
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_jpeg_preview(file_path=PDF_FILE_PATH__A4) is True
    path_to_file = manager.get_jpeg_preview(file_path=PDF_FILE_PATH__A4,
                                            force=True)
    with Image.open(path_to_file) as jpeg:
        assert jpeg.height == 256
        assert jpeg.width in range(180, 182)
示例#4
0
def test_get_nb_page() -> None:
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    nb_page = manager.get_page_nb(file_path=PDF_FILE_PATH)
    assert nb_page == 2
    nb_page = manager.get_page_nb(file_path=PDF_FILE_PATH__ENCRYPTED)
    assert nb_page == 2
    nb_page = manager.get_page_nb(file_path=PDF_FILE_PATH__A4)
    assert nb_page == 2
示例#5
0
def test_text_to_jpeg() -> None:
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    image_file_path = IMAGE_FILE_PATH
    assert manager.has_jpeg_preview(file_path=image_file_path) is True
    path_to_file = manager.get_jpeg_preview(file_path=image_file_path,
                                            force=True)
    assert os.path.exists(path_to_file) is True
    assert os.path.getsize(path_to_file) > 0
示例#6
0
def test_to_pdf_full_export():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    path_to_file = manager.get_pdf_preview(file_path=os.path.join(
        CURRENT_DIR, 'the_odt.odt'),
                                           page=-1,
                                           force=True)
    assert os.path.exists(path_to_file) == True
    assert os.path.getsize(path_to_file) > 0
    assert path_to_file == '/tmp/preview-generator-tests/cache/565e100b2c2337222cf1a551f36c17e7.pdf'  # nopep8
示例#7
0
def test_to_jpeg__default_size():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    path_to_file = manager.get_jpeg_preview(file_path=IMAGE_FILE_PATH, )
    assert os.path.exists(path_to_file) == True
    assert os.path.getsize(path_to_file) > 0
    assert path_to_file == '/tmp/preview-generator-tests/cache/f910f2af6cda4fff79f21456e19e021c-256x256.jpeg'  # nopep8
    with Image.open(path_to_file) as jpeg:
        assert jpeg.height in range(229, 231)
        assert jpeg.width == 256
示例#8
0
def test_to_jpeg__default_size():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    path_to_file = manager.get_jpeg_preview(file_path=IMAGE_FILE_PATH,
                                            force=True)
    assert os.path.exists(path_to_file) == True
    assert os.path.getsize(path_to_file) > 0
    with Image.open(path_to_file) as jpeg:
        assert jpeg.height in range(226, 228)
        assert jpeg.width == 256
示例#9
0
def test_to_pdf_full_export() -> None:
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_pdf_preview(file_path=IMAGE_FILE_PATH) is True
    path_to_file = manager.get_pdf_preview(file_path=IMAGE_FILE_PATH,
                                           page=-1,
                                           force=True)
    assert os.path.exists(path_to_file) is True
    assert os.path.getsize(path_to_file) > 0
    assert re.match(test_utils.CACHE_FILE_PATH_PATTERN__PDF, path_to_file)
def test_to_pdf_no_extension_extension_forced():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_pdf_preview(
        file_path=IMAGE_FILE_PATH_NO_EXTENSION,
        file_ext=".xlsx",
    ) is True
    path_to_file = manager.get_pdf_preview(
        file_path=IMAGE_FILE_PATH_NO_EXTENSION, force=True, file_ext=".xlsx")
    assert os.path.exists(path_to_file) is True
示例#11
0
 def __init__(self, settings, index, id, routing):
     self.settings = settings
     self.index = index
     self.id = id
     self.routing = routing
     self.source = {}
     self.setup_target_directory()
     self.manager = PreviewManager(self.thumbnail_directory,
                                   create_folder=True)
示例#12
0
def test_get_file_hash_with_size_and_page():
    pm = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    from preview_generator.utils import ImgDims
    hash = pm._get_file_hash(
        '/tmp/image.jpeg',
        ImgDims(width=512, height=256),
        page=3
    )
    assert hash == '7f8df7223d8be60a7ac8a9bf7bd1df2a-512x256-page3'
示例#13
0
def test_to_jpeg__default_size():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    path_to_file = manager.get_jpeg_preview(file_path=IMAGE_FILE_PATH, )
    assert os.path.exists(path_to_file) == True
    assert os.path.getsize(path_to_file) > 0
    assert path_to_file == '/tmp/preview-generator-tests/cache/4ebf3400b8b74282dfbf61e784777928-256x256.jpeg'  # nopep8
    with Image.open(path_to_file) as jpeg:
        assert jpeg.height in range(254, 256)
        assert jpeg.width == 256
def test_to_json():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_json_preview(
        file_path=IMAGE_FILE_PATH
    ) is True
    path_to_file = manager.get_json_preview(
        file_path=IMAGE_FILE_PATH,
        force=True
    )
示例#15
0
def test_get_preview_name_with_size_and_page() -> None:
    pm = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    from preview_generator.utils import ImgDims

    filehash = pm.get_preview_context("/tmp/image.jpeg", file_ext=".jpeg").hash
    hash = pm._get_preview_name(filehash,
                                ImgDims(width=512, height=256),
                                page=3)
    assert hash == "7f8df7223d8be60a7ac8a9bf7bd1df2a-512x256-page3"
示例#16
0
def test_to_pdf_full_export() -> None:
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_pdf_preview(file_path=IMAGE_FILE_PATH) is True
    path_to_file = manager.get_pdf_preview(file_path=IMAGE_FILE_PATH,
                                           page=-1,
                                           force=True)
    assert os.path.exists(path_to_file) is True
    assert os.path.getsize(path_to_file) > 0
    assert path_to_file == (os.path.join(CACHE_DIR, PDF_FILE_HASH + ".pdf"))
示例#17
0
def test_to_jpeg_no_extension_extension_forced() -> None:
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    image_file_path = HTML_FILE_PATH_NO_EXTENSION
    assert manager.has_jpeg_preview(file_path=image_file_path,
                                    file_ext=".html") is True
    path_to_file = manager.get_jpeg_preview(file_path=image_file_path,
                                            force=True,
                                            file_ext=".html")
    assert os.path.exists(path_to_file) is True
示例#18
0
def create_thumb(sender, instance):
    #Se il file è un audio gli assegno una thumbnail prefatta
    if 'audio' in instance.tipoFile:
        return '/thumb/audio.png'
    else:
        filepath = instance.fileUrl.file.name
        cache_path = settings.MEDIA_ROOT + '/thumb'
        manager = PreviewManager(cache_path, create_folder=True)
        FullPathToimage = manager.get_jpeg_preview(filepath)
        return '/thumb/' + os.path.basename(FullPathToimage)
示例#19
0
def test_to_pdf_no_page() -> None:
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_pdf_preview(file_path=ODP_FILE_PATH) is True
    path_to_file = manager.get_pdf_preview(file_path=ODP_FILE_PATH, force=True)
    assert os.path.exists(path_to_file) is True
    assert os.path.getsize(path_to_file) > 0
    assert re.match(test_utils.CACHE_FILE_PATH_PATTERN__PDF, path_to_file)
    assert (PdfPreviewBuilderPopplerUtils().get_page_number(
        cache_path=CACHE_DIR, preview_name="test",
        file_path=path_to_file) == 5)
示例#20
0
def pdf_to_image(cach_dir_path, pdf_file_path, font_path=None):
    """Convert text file to a grayscale image with black characters on a white background.

    arguments:
    text_path - the content of this file will be converted to an image
    font_path - path to a font file (for example impact.ttf)
    """
    preview_manager = PreviewManager(cach_dir_path, create_folder= True)
    preview = preview_manager.get_jpeg_preview(pdf_file_path, height=Height,width=WIDTH)
    return preview
示例#21
0
def test_to_text():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_text_preview(
        file_path=IMAGE_FILE_PATH
    ) is False
    with pytest.raises(UnavailablePreviewType):
        path_to_file = manager.get_text_preview(
            file_path=IMAGE_FILE_PATH,
            force=True
        )
示例#22
0
def test_to_pdf_no_page():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_pdf_preview(file_path=IMAGE_FILE_PATH) is True
    path_to_file = manager.get_pdf_preview(file_path=IMAGE_FILE_PATH, force=True)
    assert os.path.exists(path_to_file) is True
    assert os.path.getsize(path_to_file) > 0
    assert re.match(test_utils.CACHE_FILE_PATH_PATTERN__PDF, path_to_file)

    pdf = PdfFileReader(open(path_to_file, "rb"))
    assert pdf.getNumPages() == 2
示例#23
0
def main() -> None:
    args = parse_args()
    logging.basicConfig(level=logging.ERROR - 10 * args.v)  # In logging, levels are 40, 30, 20, 10.
    if args.check_dependencies:
        check_dependencies()
    if args.input_files:
        manager = PreviewManager("./")
        for input_file in args.input_files:
            path_to_preview_image = manager.get_jpeg_preview(input_file)
            print(input_file, "→", path_to_preview_image)
示例#24
0
def test_to_jpeg_no_size():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    path_to_file = manager.get_jpeg_preview(file_path=os.path.join(
        CURRENT_DIR, 'the_pdf.pdf'),
                                            force=True)
    assert os.path.exists(path_to_file) == True
    assert os.path.getsize(path_to_file) > 0
    with Image.open(path_to_file) as jpeg:
        assert jpeg.height == 256
        assert jpeg.width in range(180, 182)
示例#25
0
def test_to_pdf_no_page():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    path_to_file = manager.get_pdf_preview(file_path=os.path.join(
        CURRENT_DIR, 'the_odt.odt'),
                                           force=True)
    assert os.path.exists(path_to_file) == True
    assert os.path.getsize(path_to_file) > 0
    assert path_to_file == '/tmp/preview-generator-tests/cache/565e100b2c2337222cf1a551f36c17e7.pdf'  # nopep8
    with WandImage(filename=path_to_file) as pdf:
        assert len(pdf.sequence) == 2
示例#26
0
def test_to_jpeg__default_size():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    path_to_file = manager.get_jpeg_preview(file_path=IMAGE_FILE_PATH,
                                            force=True)
    assert os.path.exists(path_to_file) == True
    assert os.path.getsize(path_to_file) > 0
    assert path_to_file == '/tmp/preview-generator-tests/cache/243918a3cda4aa11bfe7603b627a587f-256x256.jpeg'  # nopep8
    with Image.open(path_to_file) as jpeg:
        assert jpeg.height in range(100, 102)
        assert jpeg.width == 256
def test_to_pdf():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    image_file_path = IMAGE_FILE_PATH
    assert manager.has_jpeg_preview(
        file_path=image_file_path,
    ) is True
    path_to_file = manager.get_pdf_preview(
        file_path=image_file_path,
        force=True
    )
    assert os.path.exists(path_to_file) is True
示例#28
0
def test_to_jpeg(file_path):
    manager = PreviewManager(
        cache_folder_path=CACHE_DIR,
        create_folder=True
    )
    with pytest.raises(UnsupportedMimeType):
        manager.get_jpeg_preview(
            file_path=file_path,
            height=256,
            width=512
        )
    def create_preview(self):
        full_path = self.upload.path
        cache_path = os.path.dirname(full_path)
        manager = PreviewManager(cache_path, create_folder=True)
        path_to_preview_image = manager.get_jpeg_preview(full_path,
                                                         width=200,
                                                         height=200)

        self.preview.name = path_to_preview_image.replace(MEDIA_ROOT, '')
        #raise Exception(self.preview.name,path_to_preview_image)
        self.save()
示例#30
0
def test_to_jpeg__default_size() -> None:
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_jpeg_preview(file_path=IMAGE_FILE_PATH) is True
    path_to_file = manager.get_jpeg_preview(file_path=IMAGE_FILE_PATH)
    assert os.path.exists(path_to_file) is True
    assert os.path.getsize(path_to_file) > 0
    assert re.match(test_utils.CACHE_FILE_PATH_PATTERN__JPEG, path_to_file)

    with Image.open(path_to_file) as jpeg:
        assert jpeg.height in range(229, 231)
        assert jpeg.width == 256
示例#31
0
def test_to_jpeg_no_size_no_page():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_jpeg_preview(file_path=IMAGE_FILE_PATH) is True
    path_to_file = manager.get_jpeg_preview(file_path=IMAGE_FILE_PATH, force=True)
    assert os.path.exists(path_to_file) is True
    assert os.path.getsize(path_to_file) > 0
    assert re.match(test_utils.CACHE_FILE_PATH_PATTERN__JPEG, path_to_file)

    with Image.open(path_to_file) as jpeg:
        assert jpeg.height == 256
        assert jpeg.width in range(180, 182)
def test_to_jpeg_no_extension():
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    image_file_path = IMAGE_FILE_PATH_NO_EXTENSION
    assert manager.has_jpeg_preview(
        file_path=image_file_path,
    ) is True
    path_to_file = manager.get_jpeg_preview(
        file_path=image_file_path,
        force=True
    )
    assert os.path.exists(path_to_file) is True