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
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()
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)
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
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
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
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
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
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
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)
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'
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 )
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"
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"))
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
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)
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)
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
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 )
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
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)
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)
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
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
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()
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
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