fix(upload): Fix uploading / dropping a folder

Issues:
- the `storage_path` being dropped to wasn't being considered when computing new document's `storage_path`
- include `original_filename` when computing filename
- add try/catch in `get_file_from_gcs`
- support dropping folders anywhere in the app
This commit is contained in:
Martin Tan 2023-10-12 12:24:53 +08:00
parent 892b033cdd
commit f8d16dcf4b
4 changed files with 287 additions and 262 deletions

View File

@ -268,7 +268,8 @@ export class AppComponent implements OnInit, OnDestroy {
this.fileLeave(true)
let storagePathId = parseInt(this.route.snapshot.queryParams['spid'])
storagePathId = !isNaN(storagePathId) ? storagePathId : undefined
this.uploadDocumentsService.uploadFiles(files, { storagePathId })
const isUploadWithFolders = files.every(f => 'fullPath' in f.fileEntry && typeof f.fileEntry.fullPath === 'string' && (f.fileEntry.fullPath as string).split('/').filter(s => !!s).length > 1)
this.uploadDocumentsService.uploadFiles(files, { storagePathId, isUploadWithFolders })
this.toastService.showInfo($localize`Initiating upload...`, 3000)
}
}

View File

@ -607,8 +607,22 @@ class Consumer(LoggingMixin):
)
if self.full_path:
# e.g. full_path: "/CDV#3500648756/OR#1161.pdf"
# e.g. ['CDV#3500648756']
folders = self.full_path.split('/')[:-1]
# remove empty values from splitting the leading slash
folders = [i for i in folders if i]
# e.g. user dropped the file in storage path id 26
# which is "folder_test" or "test/test2/test3"
if document.storage_path:
# e.g. ['test', 'test2', 'test3']
parent_folders = document.storage_path.path.split('/')
# just double check that there are no empty values from leading slashes
parent_folders = [i for i in parent_folders if i]
# e.g. "test/test2/test3/CDV#3500648756"
folders = parent_folders + folders
folder_path = '/'.join(folders)
print(f'folder_path: {folder_path}')

View File

@ -2,6 +2,7 @@ import logging
import os
from collections import defaultdict
from pathlib import PurePath
# import uuid
import pathvalidate
from django.conf import settings
@ -124,6 +125,9 @@ def generate_unique_filename(doc, archive_filename=False):
counter += 1
else:
return new_filename
# new_filename = str(uuid.uuid4()) + ".pdf"
# if not os.path.exists(os.path.join(root, new_filename)):
# return new_filename
def generate_filename(
@ -141,7 +145,7 @@ def generate_filename(
f"Document has storage_path {doc.storage_path.id} "
f"({doc.storage_path.path}) set",
)
filename_format = doc.storage_path.path
filename_format = doc.storage_path.path + '/' + doc.original_filename
if filename_format is not None:
tags = defaultdictNoStr(

View File

@ -30,6 +30,7 @@ def upload_file(source, target):
blob.upload_from_file(read_file_2)
def get_file_from_gcs(bucket_path):
try:
if (not client) or (not bucket):
raise Exception("Google Cloud Storage is not initialized.")
@ -47,3 +48,8 @@ def get_file_from_gcs(bucket_path):
# print("Returning downloaded file to caller")
return byte_stream
except:
return None
def exists():
return False