Initial commit to modernize staging file handling including preview generation
This commit is contained in:
60
mayan/apps/sources/classes.py
Normal file
60
mayan/apps/sources/classes.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import base64
|
||||
import errno
|
||||
import os
|
||||
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.core.files import File
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
from django.utils.encoding import smart_str
|
||||
from django.utils.translation import ugettext
|
||||
|
||||
from converter.api import convert, cache_cleanup
|
||||
from converter.literals import (DEFAULT_ZOOM_LEVEL, DEFAULT_ROTATION,
|
||||
DEFAULT_PAGE_NUMBER, DEFAULT_FILE_FORMAT_MIMETYPE)
|
||||
from documents.conf.settings import DISPLAY_SIZE, THUMBNAIL_SIZE
|
||||
from mimetype.api import get_mimetype
|
||||
|
||||
|
||||
class StagingFile(object):
|
||||
"""
|
||||
Simple class to extend the File class to add preview capabilities
|
||||
files in a directory on a storage
|
||||
"""
|
||||
def __init__(self, staging_folder, filename):
|
||||
self.staging_folder = staging_folder
|
||||
self.filename = filename
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.filename)
|
||||
|
||||
def open(self):
|
||||
return open(self.get_full_path(), mode='rb')
|
||||
#return File(file=descriptor, name=filename)
|
||||
|
||||
def get_full_path(self):
|
||||
return os.path.join(self.staging_folder.folder_path, self.filename)
|
||||
|
||||
def get_image(self, size, page, zoom, rotation, as_base64=True):
|
||||
#return self.get_valid_image(size=size, transformations=transformations)
|
||||
converted_file_path = convert(self.get_full_path(), size=size)#, cleanup_files=True)#, transformations=transformations)
|
||||
|
||||
if as_base64:
|
||||
mimetype = get_mimetype(open(converted_file_path, 'r'), converted_file_path, mimetype_only=True)[0]
|
||||
image = open(converted_file_path, 'r')
|
||||
base64_data = base64.b64encode(image.read())
|
||||
image.close()
|
||||
return u'data:%s;base64,%s' % (mimetype, base64_data)
|
||||
else:
|
||||
return file_path
|
||||
|
||||
#def delete(self, preview_size, transformations):
|
||||
# cache_cleanup(self.filepath, size=preview_size, transformations=transformations)
|
||||
# try:
|
||||
# os.unlink(self.filepath)
|
||||
# except OSError, exc:
|
||||
# if exc.errno == errno.ENOENT:
|
||||
# pass
|
||||
# else:
|
||||
# raise Exception(ugettext(u'Unable to delete staging file: %s') % exc)
|
||||
Reference in New Issue
Block a user