better error messages for file uploads. adresses #91

This commit is contained in:
jonaswinkler 2020-12-06 22:30:04 +01:00
parent c1fc8b2dac
commit eede5595e9
3 changed files with 38 additions and 36 deletions

View File

@ -16,26 +16,31 @@ export class UploadFileWidgetComponent implements OnInit {
} }
public fileOver(event){ public fileOver(event){
console.log(event);
} }
public fileLeave(event){ public fileLeave(event){
console.log(event);
} }
public dropped(files: NgxFileDropEntry[]) { public dropped(files: NgxFileDropEntry[]) {
for (const droppedFile of files) { for (const droppedFile of files) {
if (droppedFile.fileEntry.isFile) { if (droppedFile.fileEntry.isFile) {
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry; const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
console.log(fileEntry)
fileEntry.file((file: File) => { fileEntry.file((file: File) => {
console.log(file)
const formData = new FormData() const formData = new FormData()
formData.append('document', file, file.name) formData.append('document', file, file.name)
this.documentService.uploadDocument(formData).subscribe(result => { 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.")) this.toastService.showToast(Toast.make("Information", "The document has been uploaded and will be processed by the consumer shortly."))
}, error => { }, 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;
}
}
}) })
}); });
} }

View File

@ -150,8 +150,7 @@ class PostDocumentSerializer(serializers.Serializer):
required=False, required=False,
) )
def validate(self, attrs): def validate_document(self, document):
document = attrs.get('document')
try: try:
validate_filename(document.name) validate_filename(document.name)
@ -163,32 +162,31 @@ class PostDocumentSerializer(serializers.Serializer):
if not is_mime_type_supported(mime_type): if not is_mime_type_supported(mime_type):
raise serializers.ValidationError( 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') def validate_title(self, title):
if title:
return title
else:
# do not return empty strings.
return None
if not title: def validate_correspondent(self, correspondent):
attrs['title'] = None
correspondent = attrs.get('correspondent')
if correspondent: if correspondent:
attrs['correspondent_id'] = correspondent.id return correspondent.id
else: else:
attrs['correspondent_id'] = None return None
document_type = attrs.get('document_type') def validate_document_type(self, document_type):
if document_type: if document_type:
attrs['document_type_id'] = document_type.id return document_type.id
else: else:
attrs['document_type_id'] = None return None
tags = attrs.get('tags') def validate_tags(self, tags):
if tags: if tags:
tag_ids = [tag.id for tag in tags] return [tag.id for tag in tags]
attrs['tag_ids'] = tag_ids
else: else:
attrs['tag_ids'] = None return None
return attrs

View File

@ -235,12 +235,11 @@ class PostDocumentView(APIView):
serializer = self.get_serializer(data=request.data) serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
document = serializer.validated_data['document'] doc_name, doc_data = serializer.validated_data.get('document')
document_data = serializer.validated_data['document_data'] correspondent_id = serializer.validated_data.get('correspondent')
correspondent_id = serializer.validated_data['correspondent_id'] document_type_id = serializer.validated_data.get('document_type')
document_type_id = serializer.validated_data['document_type_id'] tag_ids = serializer.validated_data.get('tags')
tag_ids = serializer.validated_data['tag_ids'] title = serializer.validated_data.get('title')
title = serializer.validated_data['title']
t = int(mktime(datetime.now().timetuple())) t = int(mktime(datetime.now().timetuple()))
@ -249,17 +248,17 @@ class PostDocumentView(APIView):
with tempfile.NamedTemporaryFile(prefix="paperless-upload-", with tempfile.NamedTemporaryFile(prefix="paperless-upload-",
dir=settings.SCRATCH_DIR, dir=settings.SCRATCH_DIR,
delete=False) as f: delete=False) as f:
f.write(document_data) f.write(doc_data)
os.utime(f.name, times=(t, t)) os.utime(f.name, times=(t, t))
async_task("documents.tasks.consume_file", async_task("documents.tasks.consume_file",
f.name, f.name,
override_filename=document.name, override_filename=doc_name,
override_title=title, override_title=title,
override_correspondent_id=correspondent_id, override_correspondent_id=correspondent_id,
override_document_type_id=document_type_id, override_document_type_id=document_type_id,
override_tag_ids=tag_ids, override_tag_ids=tag_ids,
task_name=os.path.basename(document.name)[:100]) task_name=os.path.basename(doc_name)[:100])
return Response("OK") return Response("OK")