Use copyfileobj to save documents to files
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -205,6 +205,7 @@
|
|||||||
* Enable list link icons.
|
* Enable list link icons.
|
||||||
* Add outline links CSS for facets.
|
* Add outline links CSS for facets.
|
||||||
* Add a bottom margin to list links.
|
* Add a bottom margin to list links.
|
||||||
|
* Use copyfileobj to save documents to files
|
||||||
|
|
||||||
3.1.11 (2019-04-XX)
|
3.1.11 (2019-04-XX)
|
||||||
===================
|
===================
|
||||||
|
|||||||
@@ -237,6 +237,7 @@ Other changes
|
|||||||
* Enable list link icons.
|
* Enable list link icons.
|
||||||
* Add outline links CSS for facets.
|
* Add outline links CSS for facets.
|
||||||
* Add a bottom margin to list links.
|
* Add a bottom margin to list links.
|
||||||
|
* Use copyfileobj to save documents to files
|
||||||
|
|
||||||
Removals
|
Removals
|
||||||
--------
|
--------
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from __future__ import absolute_import, unicode_literals
|
|||||||
import hashlib
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
@@ -310,23 +311,13 @@ class DocumentVersion(models.Model):
|
|||||||
sender=Document, instance=self.document
|
sender=Document, instance=self.document
|
||||||
)
|
)
|
||||||
|
|
||||||
def save_to_file(self, filepath, buffer_size=1024 * 1024):
|
def save_to_file(self, file_object):
|
||||||
"""
|
"""
|
||||||
Save a copy of the document from the document storage backend
|
Save a copy of the document from the document storage backend
|
||||||
to the local filesystem
|
to the local filesystem
|
||||||
"""
|
"""
|
||||||
input_descriptor = self.open()
|
input_file_object = self.open()
|
||||||
output_descriptor = open(filepath, 'wb')
|
shutil.copyfileobj(fsrc=input_file_object, fdst=file_object)
|
||||||
while True:
|
|
||||||
copy_buffer = input_descriptor.read(buffer_size)
|
|
||||||
if copy_buffer:
|
|
||||||
output_descriptor.write(copy_buffer)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
output_descriptor.close()
|
|
||||||
input_descriptor.close()
|
|
||||||
return filepath
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def size(self):
|
def size(self):
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ except ImportError:
|
|||||||
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from mayan.apps.storage.utils import fs_cleanup, mkstemp
|
from mayan.apps.storage.utils import NamedTemporaryFile
|
||||||
|
|
||||||
from ..literals import DEFAULT_EXIF_PATH
|
from ..literals import DEFAULT_EXIF_PATH
|
||||||
from ..classes import FileMetadataDriver
|
from ..classes import FileMetadataDriver
|
||||||
@@ -44,15 +44,15 @@ class EXIFToolDriver(FileMetadataDriver):
|
|||||||
|
|
||||||
def _process(self, document_version):
|
def _process(self, document_version):
|
||||||
if self.command_exiftool:
|
if self.command_exiftool:
|
||||||
new_file_object, temp_filename = mkstemp()
|
temporary_fileobject = NamedTemporaryFile()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
document_version.save_to_file(filepath=temp_filename)
|
document_version.save_to_file(file_object=temporary_fileobject)
|
||||||
result = self.command_exiftool(temp_filename)
|
result = self.command_exiftool(temporary_fileobject.name)
|
||||||
|
|
||||||
return json.loads(s=result.stdout)[0]
|
return json.loads(s=result.stdout)[0]
|
||||||
finally:
|
finally:
|
||||||
fs_cleanup(filename=temp_filename)
|
temporary_fileobject.close()
|
||||||
else:
|
else:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
'EXIFTool binary not found, not processing document '
|
'EXIFTool binary not found, not processing document '
|
||||||
|
|||||||
Reference in New Issue
Block a user