Get storage paths working

This commit is contained in:
Martin Tan 2023-06-13 00:07:08 +08:00
parent 3e5886584f
commit 1c042d4aaf
9 changed files with 2985 additions and 2959 deletions

View File

@ -1,7 +1,7 @@
import { SettingsService } from './services/settings.service'
import { SETTINGS_KEYS } from './data/paperless-uisettings'
import { Component, OnDestroy, OnInit, Renderer2 } from '@angular/core'
import { Router } from '@angular/router'
import { ActivatedRoute, Router } from '@angular/router'
import { Subscription } from 'rxjs'
import { ConsumerStatusService } from './services/consumer-status.service'
import { ToastService } from './services/toast.service'
@ -34,6 +34,7 @@ export class AppComponent implements OnInit, OnDestroy {
private consumerStatusService: ConsumerStatusService,
private toastService: ToastService,
private router: Router,
private route: ActivatedRoute,
private uploadDocumentsService: UploadDocumentsService,
private tasksService: TasksService,
public tourService: TourService,
@ -265,7 +266,9 @@ export class AppComponent implements OnInit, OnDestroy {
public dropped(files: NgxFileDropEntry[]) {
this.fileLeave(true)
this.uploadDocumentsService.uploadFiles(files)
let storagePathId = parseInt(this.route.snapshot.queryParams['spid'])
storagePathId = !isNaN(storagePathId) ? storagePathId : undefined
this.uploadDocumentsService.uploadFiles(files, storagePathId)
this.toastService.showInfo($localize`Initiating upload...`, 3000)
}
}

View File

@ -157,7 +157,6 @@ export class ExplorerComponent
this.route.queryParamMap
.pipe(takeUntil(this.unsubscribeNotifier))
.subscribe((queryParams) => {
console.log('query params updated:', queryParams)
this.list.loadFromQueryParams(queryParams)
this.unmodifiedFilterRules = []
})

View File

@ -181,7 +181,6 @@ export class StoragePathListViewService {
)
.subscribe({
next: (result) => {
console.log('result:', result)
this.initialized = true
this.isReloading = false
activeListViewState.collectionSize = result.count

View File

@ -21,13 +21,18 @@ export class UploadDocumentsService {
private settings: SettingsService
) {}
uploadFiles(files: NgxFileDropEntry[]) {
uploadFiles(files: NgxFileDropEntry[], storagePathId?: number) {
for (const droppedFile of files) {
if (droppedFile.fileEntry.isFile) {
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry
fileEntry.file((file: File) => {
let formData = new FormData()
formData.append('document', file, file.name)
if (storagePathId) {
formData.append('storage_path_id', storagePathId.toString())
}
let status = this.consumerStatusService.newFileUpload(file.name)
status.message = $localize`Connecting...`

View File

@ -25,7 +25,7 @@ from .classifier import load_classifier
from .file_handling import create_source_path_directory
from .file_handling import generate_unique_filename
from .loggers import LoggingMixin
from .models import Correspondent
from .models import Correspondent, StoragePath
from .models import Document
from .models import DocumentType
from .models import FileInfo
@ -294,6 +294,7 @@ class Consumer(LoggingMixin):
override_created=None,
override_asn=None,
override_owner_id=None,
override_storage_path_id=None,
) -> Document:
"""
Return the document object if it was successfully created.
@ -309,6 +310,7 @@ class Consumer(LoggingMixin):
self.override_created = override_created
self.override_asn = override_asn
self.override_owner_id = override_owner_id
self.override_storage_path_id = override_storage_path_id
self._send_progress(0, 100, "STARTING", MESSAGE_NEW_FILE)
@ -566,7 +568,7 @@ class Consumer(LoggingMixin):
return document
def apply_overrides(self, document):
def apply_overrides(self, document: Document):
if self.override_correspondent_id:
document.correspondent = Correspondent.objects.get(
pk=self.override_correspondent_id,
@ -589,6 +591,11 @@ class Consumer(LoggingMixin):
pk=self.override_owner_id,
)
if self.override_storage_path_id:
document.storage_path = StoragePath.objects.get(
id=self.override_storage_path_id,
)
def _write(self, storage_type, source, target):
with open(source, "rb") as read_file, open(target, "wb") as write_file:
write_file.write(read_file.read())

View File

@ -24,6 +24,7 @@ class DocumentMetadataOverrides:
created: Optional[datetime.datetime] = None
asn: Optional[int] = None
owner_id: Optional[int] = None
storage_path_id: Optional[int] = None
class DocumentSource(enum.IntEnum):

View File

@ -725,6 +725,13 @@ class PostDocumentSerializer(serializers.Serializer):
max_value=Document.ARCHIVE_SERIAL_NUMBER_MAX,
)
storage_path_id = serializers.IntegerField(
label="Storage path ID",
allow_null=True,
write_only=True,
required=False,
)
def validate_document(self, document):
document_data = document.file.read()
mime_type = magic.from_buffer(document_data, mime=True)

View File

@ -200,6 +200,7 @@ def consume_file(
override_created=overrides.created,
override_asn=overrides.asn,
override_owner_id=overrides.owner_id,
override_storage_path_id=overrides.storage_path_id
)
if document:

View File

@ -678,6 +678,9 @@ class PostDocumentView(GenericAPIView):
title = serializer.validated_data.get("title")
created = serializer.validated_data.get("created")
archive_serial_number = serializer.validated_data.get("archive_serial_number")
storage_path_id = serializer.validated_data.get("storage_path_id")
logger.debug(f"storage_path_id: {storage_path_id}")
t = int(mktime(datetime.now().timetuple()))
@ -704,6 +707,7 @@ class PostDocumentView(GenericAPIView):
created=created,
asn=archive_serial_number,
owner_id=request.user.id,
storage_path_id=storage_path_id,
)
async_task = consume_file.delay(