A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
better error messages for file uploads. adresses #91
jonaswinklereede5595e9b6
diff --git a/src-ui/src/app/components/dashboard/widgets/upload-file-widget/upload-file-widget.component.ts b/src-ui/src/app/components/dashboard/widgets/upload-file-widget/upload-file-widget.component.tsindex a95d5f4db..1003f31db 100644--- a/src-ui/src/app/components/dashboard/widgets/upload-file-widget/upload-file-widget.component.ts+++ b/src-ui/src/app/components/dashboard/widgets/upload-file-widget/upload-file-widget.component.ts@@ -16,26 +16,31 @@ export class UploadFileWidgetComponent implements OnInit {}public fileOver(event){- console.log(event);}-+public fileLeave(event){- console.log(event);}-+public dropped(files: NgxFileDropEntry[]) {for (const droppedFile of files) {if (droppedFile.fileEntry.isFile) {const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;- console.log(fileEntry)fileEntry.file((file: File) => {- console.log(file)const formData = new FormData()formData.append('document', file, file.name)this.documentService.uploadDocument(formData).subscribe(result => {this.toastService.showToast(Toast.make("Information", "The document has been uploaded and will be processed by the consumer shortly."))}, error => {- this.toastService.showToast(Toast.makeError("An error has occured while uploading the document. Sorry!"))+ switch (error.status) {+ case 400: {+ this.toastService.showToast(Toast.makeError(`There was an error while uploading the document: ${error.error.document}`))+ break;+ }+ default: {+ this.toastService.showToast(Toast.makeError("An error has occured while uploading the document. Sorry!"))+ break;+ }+ }})});}diff --git a/src/documents/serialisers.py b/src/documents/serialisers.pyindex c988b2137..95f32094f 100644--- a/src/documents/serialisers.py+++ b/src/documents/serialisers.py@@ -150,8 +150,7 @@ class PostDocumentSerializer(serializers.Serializer):required=False,)- def validate(self, attrs):- document = attrs.get('document')+ def validate_document(self, document):try:validate_filename(document.name)@@ -163,32 +162,31 @@ class PostDocumentSerializer(serializers.Serializer):if not is_mime_type_supported(mime_type):raise serializers.ValidationError(- "This mime type is not supported.")+ "This file type is not supported.")- attrs['document_data'] = document_data+ return document.name, document_data- title = attrs.get('title')-- if not title:- attrs['title'] = None+ def validate_title(self, title):+ if title:+ return title+ else:+ # do not return empty strings.+ return None- correspondent = attrs.get('correspondent')+ def validate_correspondent(self, correspondent):if correspondent:- attrs['correspondent_id'] = correspondent.id+ return correspondent.idelse:- attrs['correspondent_id'] = None+ return None- document_type = attrs.get('document_type')+ def validate_document_type(self, document_type):if document_type:- attrs['document_type_id'] = document_type.id+ return document_type.idelse:- attrs['document_type_id'] = None+ return None- tags = attrs.get('tags')+ def validate_tags(self, tags):if tags:- tag_ids = [tag.id for tag in tags]- attrs['tag_ids'] = tag_ids+ return [tag.id for tag in tags]else:- attrs['tag_ids'] = None-- return attrs+ return Nonediff --git a/src/documents/views.py b/src/documents/views.pyindex c6b4d4b35..7d587ed3f 100755--- a/src/documents/views.py+++ b/src/documents/views.py@@ -235,12 +235,11 @@ class PostDocumentView(APIView):serializer = self.get_serializer(data=request.data)serializer.is_valid(raise_exception=True)- document = serializer.validated_data['document']- document_data = serializer.validated_data['document_data']- correspondent_id = serializer.validated_data['correspondent_id']- document_type_id = serializer.validated_data['document_type_id']- tag_ids = serializer.validated_data['tag_ids']- title = serializer.validated_data['title']+ doc_name, doc_data = serializer.validated_data.get('document')+ correspondent_id = serializer.validated_data.get('correspondent')+ document_type_id = serializer.validated_data.get('document_type')+ tag_ids = serializer.validated_data.get('tags')+ title = serializer.validated_data.get('title')t = int(mktime(datetime.now().timetuple()))@@ -249,17 +248,17 @@ class PostDocumentView(APIView):with tempfile.NamedTemporaryFile(prefix="paperless-upload-",dir=settings.SCRATCH_DIR,delete=False) as f:- f.write(document_data)+ f.write(doc_data)os.utime(f.name, times=(t, t))async_task("documents.tasks.consume_file",f.name,- override_filename=document.name,+ override_filename=doc_name,override_title=title,override_correspondent_id=correspondent_id,override_document_type_id=document_type_id,override_tag_ids=tag_ids,- task_name=os.path.basename(document.name)[:100])+ task_name=os.path.basename(doc_name)[:100])return Response("OK")