added some exception handling around some file io operations
This commit is contained in:
parent
b18b070622
commit
692a4fe38a
@ -345,9 +345,13 @@ class DocumentViewSet(
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
def get_filesize(self, filename):
|
def get_filesize(self, filename):
|
||||||
if os.path.isfile(filename):
|
try:
|
||||||
return os.stat(filename).st_size
|
if os.path.isfile(filename):
|
||||||
else:
|
return os.stat(filename).st_size
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
except (FileNotFoundError, PermissionError) as e:
|
||||||
|
logger.debug(f'Error: {e}')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@action(methods=["get"], detail=True)
|
@action(methods=["get"], detail=True)
|
||||||
@ -692,8 +696,11 @@ class LogViewSet(ViewSet):
|
|||||||
if not os.path.isfile(filename):
|
if not os.path.isfile(filename):
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
with open(filename) as f:
|
try:
|
||||||
lines = [line.rstrip() for line in f.readlines()]
|
with open(filename) as f:
|
||||||
|
lines = [line.rstrip() for line in f.readlines()]
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise Http404
|
||||||
|
|
||||||
return Response(lines)
|
return Response(lines)
|
||||||
|
|
||||||
@ -784,9 +791,12 @@ class PostDocumentView(GenericAPIView):
|
|||||||
pathvalidate.sanitize_filename(doc_name),
|
pathvalidate.sanitize_filename(doc_name),
|
||||||
)
|
)
|
||||||
|
|
||||||
temp_file_path.write_bytes(doc_data)
|
try:
|
||||||
|
temp_file_path.write_bytes(doc_data)
|
||||||
os.utime(temp_file_path, times=(t, t))
|
os.utime(temp_file_path, times=(t, t))
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"Error occurred during file processing: {e}")
|
||||||
|
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
input_doc = ConsumableDocument(
|
input_doc = ConsumableDocument(
|
||||||
source=DocumentSource.ApiUpload,
|
source=DocumentSource.ApiUpload,
|
||||||
@ -1009,34 +1019,37 @@ class BulkDownloadView(GenericAPIView):
|
|||||||
content = serializer.validated_data.get("content")
|
content = serializer.validated_data.get("content")
|
||||||
follow_filename_format = serializer.validated_data.get("follow_formatting")
|
follow_filename_format = serializer.validated_data.get("follow_formatting")
|
||||||
|
|
||||||
os.makedirs(settings.SCRATCH_DIR, exist_ok=True)
|
try:
|
||||||
temp = tempfile.NamedTemporaryFile(
|
os.makedirs(settings.SCRATCH_DIR, exist_ok=True)
|
||||||
dir=settings.SCRATCH_DIR,
|
temp = tempfile.NamedTemporaryFile(
|
||||||
suffix="-compressed-archive",
|
dir=settings.SCRATCH_DIR,
|
||||||
delete=False,
|
suffix="-compressed-archive",
|
||||||
)
|
delete=False,
|
||||||
|
|
||||||
if content == "both":
|
|
||||||
strategy_class = OriginalAndArchiveStrategy
|
|
||||||
elif content == "originals":
|
|
||||||
strategy_class = OriginalsOnlyStrategy
|
|
||||||
else:
|
|
||||||
strategy_class = ArchiveOnlyStrategy
|
|
||||||
|
|
||||||
with zipfile.ZipFile(temp.name, "w", compression) as zipf:
|
|
||||||
strategy = strategy_class(zipf, follow_filename_format)
|
|
||||||
for id in ids:
|
|
||||||
doc = Document.objects.get(id=id)
|
|
||||||
strategy.add_document(doc)
|
|
||||||
|
|
||||||
with open(temp.name, "rb") as f:
|
|
||||||
response = HttpResponse(f, content_type="application/zip")
|
|
||||||
response["Content-Disposition"] = '{}; filename="{}"'.format(
|
|
||||||
"attachment",
|
|
||||||
"documents.zip",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return response
|
if content == "both":
|
||||||
|
strategy_class = OriginalAndArchiveStrategy
|
||||||
|
elif content == "originals":
|
||||||
|
strategy_class = OriginalsOnlyStrategy
|
||||||
|
else:
|
||||||
|
strategy_class = ArchiveOnlyStrategy
|
||||||
|
|
||||||
|
with zipfile.ZipFile(temp.name, "w", compression) as zipf:
|
||||||
|
strategy = strategy_class(zipf, follow_filename_format)
|
||||||
|
for id in ids:
|
||||||
|
doc = Document.objects.get(id=id)
|
||||||
|
strategy.add_document(doc)
|
||||||
|
|
||||||
|
with open(temp.name, "rb") as f:
|
||||||
|
response = HttpResponse(f, content_type="application/zip")
|
||||||
|
response["Content-Disposition"] = '{}; filename="{}"'.format(
|
||||||
|
"attachment",
|
||||||
|
"documents.zip",
|
||||||
|
)
|
||||||
|
|
||||||
|
return response
|
||||||
|
except (FileNotFoundError, PermissionError) as e:
|
||||||
|
return HttpResponse(f"File IO error: {str(e)}", status=500)
|
||||||
|
|
||||||
|
|
||||||
class RemoteVersionView(GenericAPIView):
|
class RemoteVersionView(GenericAPIView):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user