def create_document_thumbnail(self, object_id): """ Create thumbnail for a document. """ logger.debug("Generating thumbnail for document #{}.".format(object_id)) try: document = Document.objects.get(id=object_id) except Document.DoesNotExist: logger.error("Document #{} does not exit.".format(object_id)) return image_path = None if document.is_image(): image_path = document.doc_file.path elif document.is_file(): try: image_file = render_document(document.doc_file.path) image_path = image_file.name except ConversionError as e: logger.debug("Could not convert document #{}: {}.".format( object_id, e)) try: if image_path: assert isfile(image_path) and access( image_path, R_OK) and os.stat(image_path).st_size > 0 except (AssertionError, TypeError): image_path = None if not image_path: image_path = document.find_placeholder() if not image_path or not os.path.exists(image_path): logger.debug( "Could not find placeholder for document #{}".format(object_id)) return thumbnail_content = None try: thumbnail_content = generate_thumbnail_content(image_path) except MissingPILError: logger.error('Pillow not installed, could not generate thumbnail.') return if not thumbnail_content: logger.warning("Thumbnail for document #{} empty.".format(object_id)) filename = 'document-{}-thumb.png'.format(document.uuid) document.save_thumbnail(filename, thumbnail_content) logger.debug("Thumbnail for document #{} created.".format(object_id))
def create_document_thumbnail(self, object_id): """ Create thumbnail for a document. """ logger.debug("Generating thumbnail for document #{}.".format(object_id)) try: document = Document.objects.get(id=object_id) except Document.DoesNotExist: logger.error("Document #{} does not exit.".format(object_id)) return image_path = None if document.is_image(): image_path = document.doc_file.path elif document.is_file(): try: image_file = render_document(document.doc_file.path) image_path = image_file.name except ConversionError as e: logger.debug("Could not convert document #{}: {}." .format(object_id, e)) try: if image_path: assert isfile(image_path) and access(image_path, R_OK) and os.stat(image_path).st_size > 0 except (AssertionError, TypeError): image_path = None if not image_path: image_path = document.find_placeholder() if not image_path or not os.path.exists(image_path): logger.debug("Could not find placeholder for document #{}" .format(object_id)) return thumbnail_content = None try: thumbnail_content = generate_thumbnail_content(image_path) except MissingPILError: logger.error('Pillow not installed, could not generate thumbnail.') return if not thumbnail_content: logger.warning("Thumbnail for document #{} empty.".format(object_id)) filename = 'document-{}-thumb.png'.format(document.uuid) document.save_thumbnail(filename, thumbnail_content) logger.debug("Thumbnail for document #{} created.".format(object_id))
def create_document_thumbnail(self, object_id): """ Create thumbnail for a document. """ logger.debug("Generating thumbnail for document #{}.".format(object_id)) try: document = Document.objects.get(id=object_id) except Document.DoesNotExist: logger.error("Document #{} does not exit.".format(object_id)) return image_path = None if document.is_image(): image_path = document.doc_file.path elif document.is_file(): try: image_file = render_document(document.doc_file.path) image_path = image_file.name except ConversionError as e: logger.debug( "Não foi possivel converte o documento #{}: {}.".format( object_id, e)) if not image_path: image_path = document.find_placeholder() if not image_path: logger.debug( "Não foi possivel encontrar o espaco reservado para o documento #{}" .format(object_id)) return thumbnail_content = None try: thumbnail_content = generate_thumbnail_content(image_path) except MissingPILError: logger.error('Pillow not installed, could not generate thumbnail.') return if not thumbnail_content: logger.warning("Thumbnail for document #{} empty.".format(object_id)) filename = 'document-{}-thumb.png'.format(document.uuid) document.save_thumbnail(filename, thumbnail_content) logger.debug("Thumbnail for document #{} created.".format(object_id))
def create_document_thumbnail(object_id): """ Create thumbnail for a document. """ logger.debug("Generating thumbnail for document #{}.".format(object_id)) try: document = Document.objects.get(id=object_id) except Document.DoesNotExist: logger.error("Document #{} does not exit.".format(object_id)) return image_path = None if document.is_image(): image_path = document.doc_file.path elif document.is_file(): try: image_file = render_document(document.doc_file.path) image_path = image_file.name except ConversionError as e: logger.debug("Could not convert document #{}: {}." .format(object_id, e)) if not image_path: image_path = document.find_placeholder() if not image_path: logger.debug("Could not find placeholder for document #{}" .format(object_id)) return try: thumbnail_content = generate_thumbnail_content(image_path) except MissingPILError: logger.error('Pillow not installed, could not generate thumbnail.') return filename = 'document-{}-thumb.png'.format(document.uuid) document.save_thumbnail(filename, thumbnail_content) logger.debug("Thumbnail for document #{} created.".format(object_id))
def create_document_thumbnail(object_id): """ Create thumbnail for a document. """ logger.debug("Generating thumbnail for document #{}.".format(object_id)) try: document = Document.objects.get(id=object_id) except Document.DoesNotExist: logger.error("Document #{} does not exit.".format(object_id)) return image_path = None if document.is_image(): image_path = document.doc_file.path elif document.is_file(): try: image_file = render_document(document.doc_file.path) image_path = image_file.name except ConversionError as e: logger.debug("Could not convert document #{}: {}." .format(object_id, e)) if not image_path: image_path = document.find_placeholder() if not image_path: logger.debug("Could not find placeholder for document #{}" .format(object_id)) return try: thumbnail_content = generate_thumbnail_content(image_path) except MissingPILError: logger.error('Pillow not installed, could not generate thumbnail.') return
def create_document_thumbnail(self, object_id): """ Create thumbnail for a document. """ logger.debug("Generating thumbnail for document #{}.".format(object_id)) try: document = Document.objects.get(id=object_id) except Document.DoesNotExist: logger.error("Document #{} does not exist.".format(object_id)) return image_path = None image_file = None if document.is_image: if not os.path.exists(storage.path(document.doc_file.name)): from shutil import copyfile copyfile(document.doc_file.path, storage.path(document.doc_file.name)) image_file = storage.open(document.doc_file.name, 'rb') elif document.is_video or document.is_audio: image_file = open(document.find_placeholder(), 'rb') elif document.is_file: try: document_location = storage.path(document.doc_file.name) except NotImplementedError as e: logger.debug(e) document_location = storage.url(document.doc_file.name) try: image_path = render_document(document_location) if image_path is not None: try: image_file = open(image_path, 'rb') except Exception as e: logger.debug(e) logger.debug( "Failed to render document #{}".format(object_id)) else: logger.debug("Failed to render document #{}".format(object_id)) except ConversionError as e: logger.debug("Could not convert document #{}: {}.".format( object_id, e)) except NotImplementedError as e: logger.debug("Failed to render document #{}: {}".format( object_id, e)) thumbnail_content = None try: try: thumbnail_content = generate_thumbnail_content(image_file) except Exception as e: logger.error( "Could not generate thumbnail, falling back to 'placeholder': {}" .format(e)) thumbnail_content = generate_thumbnail_content( document.find_placeholder()) except Exception as e: logger.error("Could not generate thumbnail: {}".format(e)) return finally: if image_file is not None: image_file.close() if image_path is not None: os.remove(image_path) if not thumbnail_content: logger.warning("Thumbnail for document #{} empty.".format(object_id)) filename = 'document-{}-thumb.png'.format(document.uuid) document.save_thumbnail(filename, thumbnail_content) logger.debug("Thumbnail for document #{} created.".format(object_id))