Remove the MIMETYPE_FILE_READ_SIZE setting

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2019-04-21 03:59:05 -04:00
parent d7a5db711b
commit 64cd0c232d
5 changed files with 24 additions and 36 deletions

View File

@@ -94,6 +94,7 @@
management app.
* Move the purge permission logic to the StorePermission
manager.
* Remove the MIMETYPE_FILE_READ_SIZE setting.
3.1.11 (2019-04-XX)
===================

View File

@@ -126,6 +126,7 @@ Other changes
management app.
* Move the purge permission logic to the StorePermission
manager.
* Remove the MIMETYPE_FILE_READ_SIZE setting.
Removals
--------

View File

@@ -1,8 +1,10 @@
from __future__ import unicode_literals
from shutil import copyfileobj
import magic
from .settings import setting_file_read_size
from mayan.apps.storage.utils import NamedTemporaryFile
def get_mimetype(file_object, mimetype_only=False):
@@ -13,22 +15,27 @@ def get_mimetype(file_object, mimetype_only=False):
file_mimetype = None
file_mime_encoding = None
read_size = setting_file_read_size.value
if read_size == 0:
# If the setting value is 0 that means disable read limit. To disable
# the read limit passing None won't work, we pass -1 instead as per
# the Python documentation.
# https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects
read_size = -1
mime = magic.Magic(mime=True)
file_mimetype = mime.from_buffer(file_object.read(read_size))
temporary_file_object = NamedTemporaryFile()
file_object.seek(0)
copyfileobj(fsrc=file_object, fdst=temporary_file_object)
file_object.seek(0)
temporary_file_object.seek(0)
kwargs = {'mime': True}
if not mimetype_only:
file_object.seek(0)
mime_encoding = magic.Magic(mime_encoding=True)
file_mime_encoding = mime_encoding.from_buffer(file_object.read(read_size))
file_object.seek(0)
kwargs['mime_encoding'] = True
try:
mime = magic.Magic(**kwargs)
if mimetype_only:
file_mimetype = mime.from_file(filename=temporary_file_object.name)
else:
file_mimetype, file_mime_encoding = mime.from_file(
filename=temporary_file_object.name
).split('; charset=')
finally:
temporary_file_object.close()
return file_mimetype, file_mime_encoding

View File

@@ -1,3 +0,0 @@
from __future__ import unicode_literals
DEFAULT_MIMETYPE_FILE_READ_SIZE = 1024

View File

@@ -1,18 +0,0 @@
from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _
from mayan.apps.smart_settings import Namespace
from .literals import DEFAULT_MIMETYPE_FILE_READ_SIZE
namespace = Namespace(label=_('MIME type'), name='mimetype')
setting_file_read_size = namespace.add_setting(
default=DEFAULT_MIMETYPE_FILE_READ_SIZE,
global_name='MIMETYPE_FILE_READ_SIZE', help_text=_(
'Amount of bytes to read from a document to determine its MIME type. '
'Setting it to 0 disables the feature and attempts to read the entire '
'document file into memory.'
)
)