Added new mimetype app to handle mimetype guessing and provide mimetype related icons when a preview is not available

This commit is contained in:
Roberto Rosario
2011-07-22 03:37:26 -04:00
parent 4060a39a4f
commit f589da84fd
124 changed files with 171 additions and 80 deletions

View File

@@ -13,15 +13,6 @@ from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User from django.contrib.auth.models import User
try:
from python_magic import magic
USE_PYTHON_MAGIC = True
except:
import mimetypes
mimetypes.init()
USE_PYTHON_MAGIC = False
def urlquote(link=None, get=None): def urlquote(link=None, get=None):
u''' u'''
This method does both: urlquote() and urlencode() This method does both: urlquote() and urlencode()
@@ -349,34 +340,6 @@ def return_diff(old_obj, new_obj, attrib_list=None):
return diff_dict return diff_dict
def get_mimetype(filepath):
"""
Determine a file's mimetype by calling the system's libmagic
library via python-magic or fallback to use python's mimetypes
library
"""
file_mimetype = u''
file_mime_encoding = u''
if USE_PYTHON_MAGIC:
if os.path.exists(filepath):
try:
source = open(filepath, 'r')
mime = magic.Magic(mime=True)
file_mimetype = mime.from_buffer(source.read())
source.seek(0)
mime_encoding = magic.Magic(mime_encoding=True)
file_mime_encoding = mime_encoding.from_buffer(source.read())
finally:
if source:
source.close()
else:
path, filename = os.path.split(filepath)
file_mimetype, file_mime_encoding = mimetypes.guess_type(filename)
return file_mimetype, file_mime_encoding
def validate_path(path): def validate_path(path):
if os.path.exists(path) != True: if os.path.exists(path) != True:
# If doesn't exist try to create it # If doesn't exist try to create it

View File

@@ -12,7 +12,7 @@ except RuntimeError:
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from common.utils import get_mimetype from mimetype.api import get_mimetype
from converter.exceptions import ConvertError, UnknownFormat, IdentifyError from converter.exceptions import ConvertError, UnknownFormat, IdentifyError
from converter.backends import ConverterBase from converter.backends import ConverterBase

View File

@@ -15,12 +15,14 @@ from converter.api import get_page_count
from converter.api import get_available_transformations_choices from converter.api import get_available_transformations_choices
from converter.api import convert from converter.api import convert
from converter.exceptions import UnknownFormat, UnkownConvertError from converter.exceptions import UnknownFormat, UnkownConvertError
from mimetype.api import get_document_mimetype, get_icon_file_path, \
get_error_icon_file_path
from documents.utils import get_document_mimetype
from documents.conf.settings import CHECKSUM_FUNCTION from documents.conf.settings import CHECKSUM_FUNCTION
from documents.conf.settings import UUID_FUNCTION from documents.conf.settings import UUID_FUNCTION
from documents.conf.settings import STORAGE_BACKEND from documents.conf.settings import STORAGE_BACKEND
from documents.conf.settings import PREVIEW_SIZE from documents.conf.settings import PREVIEW_SIZE
from documents.conf.settings import DISPLAY_SIZE
from documents.conf.settings import CACHE_PATH from documents.conf.settings import CACHE_PATH
from documents.managers import RecentDocumentManager, \ from documents.managers import RecentDocumentManager, \
@@ -234,17 +236,16 @@ class Document(models.Model):
document_file = document_save_to_temp_dir(self, self.checksum) document_file = document_save_to_temp_dir(self, self.checksum)
return convert(document_file, output_filepath=cache_file_path, page=page, transformations=transformations) return convert(document_file, output_filepath=cache_file_path, page=page, transformations=transformations)
def get_image(self, size=PREVIEW_SIZE, page=DEFAULT_PAGE_NUMBER, zoom=DEFAULT_ZOOM_LEVEL, rotation=DEFAULT_ROTATION): def get_image(self, size=DISPLAY_SIZE, page=DEFAULT_PAGE_NUMBER, zoom=DEFAULT_ZOOM_LEVEL, rotation=DEFAULT_ROTATION):
try: try:
image_cache_name = self.get_image_cache_name(page=page) image_cache_name = self.get_image_cache_name(page=page)
output_file = convert(image_cache_name, cleanup_files=False, size=size, zoom=zoom, rotation=rotation) return convert(image_cache_name, cleanup_files=False, size=size, zoom=zoom, rotation=rotation)
except UnknownFormat: except UnknownFormat:
output_file = os.path.join(settings.MEDIA_ROOT, u'images', PICTURE_UNKNOWN_SMALL) return get_icon_file_path(self.file_mimetype)
except UnkownConvertError: except UnkownConvertError:
output_file = os.path.join(settings.MEDIA_ROOT, u'images', PICTURE_ERROR_SMALL) return get_error_icon_file_path()
except: except:
output_file = os.path.join(settings.MEDIA_ROOT, u'images', PICTURE_ERROR_SMALL) return get_error_icon_file_path()
return output_file
class DocumentTypeFilename(models.Model): class DocumentTypeFilename(models.Model):

View File

@@ -2,14 +2,6 @@ import os
from common.conf.settings import TEMPORARY_DIRECTORY from common.conf.settings import TEMPORARY_DIRECTORY
try:
from python_magic import magic
USE_PYTHON_MAGIC = True
except:
import mimetypes
mimetypes.init()
USE_PYTHON_MAGIC = False
#http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python #http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python
def copyfile(source, dest, buffer_size=1024 * 1024): def copyfile(source, dest, buffer_size=1024 * 1024):
@@ -37,30 +29,3 @@ def copyfile(source, dest, buffer_size=1024 * 1024):
def document_save_to_temp_dir(document, filename, buffer_size=1024 * 1024): def document_save_to_temp_dir(document, filename, buffer_size=1024 * 1024):
temporary_path = os.path.join(TEMPORARY_DIRECTORY, filename) temporary_path = os.path.join(TEMPORARY_DIRECTORY, filename)
return document.save_to_file(temporary_path, buffer_size) return document.save_to_file(temporary_path, buffer_size)
def get_document_mimetype(document):
"""
Determine a documents mimetype by calling the system's libmagic
library via python-magic or fallback to use python's mimetypes
library
"""
file_mimetype = u''
file_mime_encoding = u''
if USE_PYTHON_MAGIC:
if document.exists():
try:
source = document.open()
mime = magic.Magic(mime=True)
file_mimetype = mime.from_buffer(source.read())
source.seek(0)
mime_encoding = magic.Magic(mime_encoding=True)
file_mime_encoding = mime_encoding.from_buffer(source.read())
finally:
if source:
source.close()
else:
file_mimetype, file_mime_encoding = mimetypes.guess_type(document.get_fullname())
return file_mimetype, file_mime_encoding

View File

134
apps/mimetype/api.py Normal file
View File

@@ -0,0 +1,134 @@
import os
from django.conf import settings
try:
import magic
USE_PYTHON_MAGIC = True
except:
import mimetypes
mimetypes.init()
USE_PYTHON_MAGIC = False
MIMETYPE_ICONS_DIRECTORY_NAME = os.path.join('images', 'mimetypes')
UNKNWON_TYPE_FILE_NAME = 'unknown.png'
ERROR_FILE_NAME = 'error.png'
mimetype_icons = {
'application/pdf' : 'file_extension_pdf.png',
'application/zip' : 'file_extension_zip.png',
'application/ogg' : 'file_extension_ogg.png',
'application/postscript' : 'file_extension_ps.png',
'application/x-gzip' : 'file_extension_gz.png',
'application/x-rar-compressed' : 'file_extension_rar.png',
'application/x-troff-msvideo' : 'file_extension_avi.png',
'application/acad' : 'file_extension_dwg.png',
'application/octet-stream' : 'file_extension_exe.png',
'application/vnd.oasis.opendocument.text': 'ODF_textdocument_32x32.png',
'application/vnd.oasis.opendocument.spreadsheet': 'ODF_spreadsheet_32x32.png',
'application/vnd.oasis.opendocument.presentation': 'ODF_presentation_32x32.png',
'application/vnd.oasis.opendocument.graphics': 'ODF_drawing_32x32.png',
'application/vnd.ms-excel': 'file_extension_xls.png',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'file_extension_xls.png',
'application/msword': 'file_extension_doc.png',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'file_extension_doc.png',
'application/mspowerpoint': 'file_extension_pps.png',
'application/vnd.ms-powerpoint': 'file_extension_pps.png',
'application/wav' : 'file_extension_wav.png',
'application/x-wav' : 'file_extension_wav.png',
'image/jpeg' : 'file_extension_jpeg.png',
'image/png' : 'file_extension_png.png',
'image/x-png' : 'file_extension_png.png',
'image/tiff' : 'file_extension_tif.png',
'image/x-tiff' : 'file_extension_tif.png',
'image/bmp' : 'file_extension_bmp.png',
'image/gif' : 'file_extension_gif.png',
'image/vnd.dwg' : 'file_extension_dwg.png',
'image/x-dwg' : 'file_extension_dwg.png',
'audio/mpeg' : 'file_extension_mp3.png',
'audio/mid' : 'file_extension_mid.png',
'audio/x-wav' : 'file_extension_wav.png',
'audio/vnd.wav' : 'file_extension_wav.png',
'audio/x-pn-realaudio' : 'file_extension_ram.png',
'audio/mp4' : 'file_extension_mp4.png',
'audio/x-ms-wma' : 'file_extension_wma.png',
'video/avi' : 'file_extension_avi.png',
'video/mpeg' : 'file_extension_mpeg.png',
'video/quicktime' : 'file_extension_mov.png',
'video/x-ms-asf' : 'file_extension_asf.png',
'video/x-ms-wmv' : 'file_extension_wmv.png',
'text/html' : 'file_extension_html.png',
'text/plain' : 'file_extension_txt.png',
}
def get_icon_file_path(mimetype):
file_name = mimetype_icons.get(mimetype, UNKNWON_TYPE_FILE_NAME)
return os.path.join(settings.MEDIA_ROOT, MIMETYPE_ICONS_DIRECTORY_NAME, file_name)
def get_error_icon_file_path():
return os.path.join(settings.MEDIA_ROOT, MIMETYPE_ICONS_DIRECTORY_NAME, ERROR_FILE_NAME)
def get_mimetype(filepath):
"""
Determine a file's mimetype by calling the system's libmagic
library via python-magic or fallback to use python's mimetypes
library
"""
file_mimetype = u''
file_mime_encoding = u''
source = None
if USE_PYTHON_MAGIC:
if os.path.exists(filepath):
try:
source = open(filepath, 'r')
mime = magic.Magic(mime=True)
file_mimetype = mime.from_buffer(source.read())
source.seek(0)
mime_encoding = magic.Magic(mime_encoding=True)
file_mime_encoding = mime_encoding.from_buffer(source.read())
finally:
if source:
source.close()
else:
path, filename = os.path.split(filepath)
file_mimetype, file_mime_encoding = mimetypes.guess_type(filename)
return file_mimetype, file_mime_encoding
def get_document_mimetype(document):
"""
Determine a documents mimetype by calling the system's libmagic
library via python-magic or fallback to use python's mimetypes
library
"""
file_mimetype = u''
file_mime_encoding = u''
source = None
if USE_PYTHON_MAGIC:
if document.exists():
try:
source = document.open()
mime = magic.Magic(mime=True)
file_mimetype = mime.from_buffer(source.read())
source.seek(0)
mime_encoding = magic.Magic(mime_encoding=True)
file_mime_encoding = mime_encoding.from_buffer(source.read())
finally:
if source:
source.close()
else:
file_mimetype, file_mime_encoding = mimetypes.guess_type(document.get_fullname())
return file_mimetype, file_mime_encoding

3
apps/mimetype/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

23
apps/mimetype/tests.py Normal file
View File

@@ -0,0 +1,23 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

1
apps/mimetype/views.py Normal file
View File

@@ -0,0 +1 @@
# Create your views here.

View File

@@ -150,6 +150,7 @@ INSTALLED_APPS = (
'mptt', 'mptt',
'document_indexing', 'document_indexing',
'sources', 'sources',
'mimetype',
) )
TEMPLATE_CONTEXT_PROCESSORS = ( TEMPLATE_CONTEXT_PROCESSORS = (

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 950 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 966 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 917 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Some files were not shown because too many files have changed in this diff Show More