added some exception handling around some file io operations

This commit is contained in:
freewayml 2023-10-10 00:27:16 +00:00
parent b18b070622
commit 692a4fe38a

View File

@ -345,10 +345,14 @@ class DocumentViewSet(
return []
def get_filesize(self, filename):
try:
if os.path.isfile(filename):
return os.stat(filename).st_size
else:
return None
except (FileNotFoundError, PermissionError) as e:
logger.debug(f'Error: {e}')
return None
@action(methods=["get"], detail=True)
def metadata(self, request, pk=None):
@ -692,8 +696,11 @@ class LogViewSet(ViewSet):
if not os.path.isfile(filename):
raise Http404
try:
with open(filename) as f:
lines = [line.rstrip() for line in f.readlines()]
except FileNotFoundError:
raise Http404
return Response(lines)
@ -784,9 +791,12 @@ class PostDocumentView(GenericAPIView):
pathvalidate.sanitize_filename(doc_name),
)
try:
temp_file_path.write_bytes(doc_data)
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(
source=DocumentSource.ApiUpload,
@ -1009,6 +1019,7 @@ class BulkDownloadView(GenericAPIView):
content = serializer.validated_data.get("content")
follow_filename_format = serializer.validated_data.get("follow_formatting")
try:
os.makedirs(settings.SCRATCH_DIR, exist_ok=True)
temp = tempfile.NamedTemporaryFile(
dir=settings.SCRATCH_DIR,
@ -1037,6 +1048,8 @@ class BulkDownloadView(GenericAPIView):
)
return response
except (FileNotFoundError, PermissionError) as e:
return HttpResponse(f"File IO error: {str(e)}", status=500)
class RemoteVersionView(GenericAPIView):