Remove the MIMETYPE_FILE_READ_SIZE setting
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -94,6 +94,7 @@
|
|||||||
management app.
|
management app.
|
||||||
* Move the purge permission logic to the StorePermission
|
* Move the purge permission logic to the StorePermission
|
||||||
manager.
|
manager.
|
||||||
|
* Remove the MIMETYPE_FILE_READ_SIZE setting.
|
||||||
|
|
||||||
3.1.11 (2019-04-XX)
|
3.1.11 (2019-04-XX)
|
||||||
===================
|
===================
|
||||||
|
|||||||
@@ -126,6 +126,7 @@ Other changes
|
|||||||
management app.
|
management app.
|
||||||
* Move the purge permission logic to the StorePermission
|
* Move the purge permission logic to the StorePermission
|
||||||
manager.
|
manager.
|
||||||
|
* Remove the MIMETYPE_FILE_READ_SIZE setting.
|
||||||
|
|
||||||
Removals
|
Removals
|
||||||
--------
|
--------
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from shutil import copyfileobj
|
||||||
|
|
||||||
import magic
|
import magic
|
||||||
|
|
||||||
from .settings import setting_file_read_size
|
from mayan.apps.storage.utils import NamedTemporaryFile
|
||||||
|
|
||||||
|
|
||||||
def get_mimetype(file_object, mimetype_only=False):
|
def get_mimetype(file_object, mimetype_only=False):
|
||||||
@@ -13,22 +15,27 @@ def get_mimetype(file_object, mimetype_only=False):
|
|||||||
file_mimetype = None
|
file_mimetype = None
|
||||||
file_mime_encoding = None
|
file_mime_encoding = None
|
||||||
|
|
||||||
read_size = setting_file_read_size.value
|
temporary_file_object = NamedTemporaryFile()
|
||||||
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))
|
|
||||||
file_object.seek(0)
|
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:
|
if not mimetype_only:
|
||||||
file_object.seek(0)
|
kwargs['mime_encoding'] = True
|
||||||
mime_encoding = magic.Magic(mime_encoding=True)
|
|
||||||
file_mime_encoding = mime_encoding.from_buffer(file_object.read(read_size))
|
try:
|
||||||
file_object.seek(0)
|
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
|
return file_mimetype, file_mime_encoding
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
DEFAULT_MIMETYPE_FILE_READ_SIZE = 1024
|
|
||||||
@@ -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.'
|
|
||||||
)
|
|
||||||
)
|
|
||||||
Reference in New Issue
Block a user