def get_image(self, transformations=None): cache_filename = self.cache_filename logger.debug('Page cache filename: %s', cache_filename) if not setting_disable_base_image_cache.value and storage_documentimagecache.exists(cache_filename): logger.debug('Page cache file "%s" found', cache_filename) with storage_documentimagecache.open(cache_filename) as file_object: converter = get_converter_class()( file_object=file_object ) converter.seek_page(page_number=0) # This code is also repeated below to allow using a context # manager with storage_documentimagecache.open and close it # automatically. for transformation in transformations: converter.transform(transformation=transformation) return converter.get_page() else: logger.debug('Page cache file "%s" not found', cache_filename) try: with self.document_version.get_intermediate_file() as file_object: converter = get_converter_class()( file_object=file_object ) converter.seek_page(page_number=self.page_number - 1) page_image = converter.get_page() # Since open "wb+" doesn't create files, check if the file # exists, if not then create it if not storage_documentimagecache.exists(cache_filename): storage_documentimagecache.save(name=cache_filename, content=ContentFile(content='')) with storage_documentimagecache.open(cache_filename, 'wb+') as file_object: file_object.write(page_image.getvalue()) for transformation in transformations: converter.transform(transformation=transformation) return converter.get_page() except Exception as exception: # Cleanup in case of error logger.error( 'Error creating page cache file "%s"; %s', cache_filename, exception ) storage_documentimagecache.delete(cache_filename) raise
def get_image(self, transformations=None): cache_filename = 'base_image' logger.debug('Page cache filename: %s', cache_filename) cache_file = self.cache_partition.get_file(filename=cache_filename) if not setting_disable_base_image_cache.value and cache_file: logger.debug('Page cache file "%s" found', cache_filename) with cache_file.open() as file_object: converter = get_converter_class()( file_object=file_object ) converter.seek_page(page_number=0) # This code is also repeated below to allow using a context # manager with cache_file.open and close it automatically. # Apply runtime transformations for transformation in transformations: converter.transform(transformation=transformation) return converter.get_page() else: logger.debug('Page cache file "%s" not found', cache_filename) try: with self.document_version.get_intermediate_file() as file_object: converter = get_converter_class()( file_object=file_object ) converter.seek_page(page_number=self.page_number - 1) page_image = converter.get_page() # Since open "wb+" doesn't create files, create it explicitly with self.cache_partition.create_file(filename=cache_filename) as file_object: file_object.write(page_image.getvalue()) # Apply runtime transformations for transformation in transformations: converter.transform(transformation=transformation) return converter.get_page() except Exception as exception: # Cleanup in case of error logger.error( 'Error creating page cache file "%s"; %s', cache_filename, exception ) raise
def update_page_count(self, save=True): try: with self.open() as file_object: converter = get_converter_class()( file_object=file_object, mime_type=self.mimetype ) detected_pages = converter.get_page_count() except PageCountError: # If converter backend doesn't understand the format, # use 1 as the total page count pass else: DocumentPage = apps.get_model( app_label='documents', model_name='DocumentPage' ) with transaction.atomic(): self.pages.all().delete() for page_number in range(detected_pages): DocumentPage.objects.create( document_version=self, page_number=page_number + 1 ) if save: self.save() return detected_pages
def get_image(self, transformations=None): cache_filename = self.cache_filename file_object = None try: file_object = open(self.get_full_path(), mode='rb') converter = get_converter_class()(file_object=file_object) page_image = converter.get_page() # Since open "wb+" doesn't create files, check if the file # exists, if not then create it if not storage_staging_file_image_cache.exists(cache_filename): storage_staging_file_image_cache.save( name=cache_filename, content=ContentFile(content='')) with storage_staging_file_image_cache.open(cache_filename, 'wb+') as file_object: file_object.write(page_image.getvalue()) except Exception as exception: # Cleanup in case of error logger.error('Error creating staging file cache "%s"; %s', cache_filename, exception) storage_staging_file_image_cache.delete(cache_filename) if file_object: file_object.close() raise for transformation in transformations: converter.transform(transformation=transformation) result = converter.get_page() file_object.close() return result
def get_intermediate_file(self): cache_filename = 'intermediate_file' cache_file = self.cache_partition.get_file(filename=cache_filename) if cache_file: logger.debug('Intermidiate file found.') return cache_file.open() else: logger.debug('Intermidiate file not found.') try: with self.open() as version_file_object: converter = get_converter_class()( file_object=version_file_object) with converter.to_pdf() as pdf_file_object: with self.cache_partition.create_file( filename=cache_filename) as file_object: shutil.copyfileobj(fsrc=pdf_file_object, fdst=file_object) return self.cache_partition.get_file( filename=cache_filename).open() except InvalidOfficeFormat: return self.open() except Exception as exception: logger.error('Error creating intermediate file "%s"; %s.', cache_filename, exception) cache_file = self.cache_partition.get_file( filename=cache_filename) if cache_file: cache_file.delete() raise
def detect_orientation(self): with self.document_version.open() as file_object: converter = get_converter_class()( file_object=file_object, mime_type=self.document_version.mimetype ) return converter.detect_orientation( page_number=self.page_number )
def execute(self, file_object, language=None, transformations=None): self.language = language if not transformations: transformations = [] self.converter = get_converter_class()(file_object=file_object) for transformation in transformations: self.converter.transform(transformation=transformation)
def get_intermediate_file(self): cache_filename = self.cache_filename logger.debug('Intermidiate filename: %s', cache_filename) if storage_documentimagecache.exists(cache_filename): logger.debug('Intermidiate file "%s" found.', cache_filename) return storage_documentimagecache.open(cache_filename) else: logger.debug('Intermidiate file "%s" not found.', cache_filename) try: with self.open() as version_file_object: converter = get_converter_class()(file_object=version_file_object) with converter.to_pdf() as pdf_file_object: # Since open "wb+" doesn't create files, check if the file # exists, if not then create it if not storage_documentimagecache.exists(cache_filename): storage_documentimagecache.save( name=cache_filename, content=ContentFile(content='') ) with storage_documentimagecache.open(cache_filename, mode='wb+') as file_object: shutil.copyfileobj( fsrc=pdf_file_object, fdst=file_object ) return storage_documentimagecache.open(cache_filename) except InvalidOfficeFormat: return self.open() except Exception as exception: # Cleanup in case of error logger.error( 'Error creating intermediate file "%s"; %s.', cache_filename, exception ) storage_documentimagecache.delete(cache_filename) raise