Skip to content

Commit

Permalink
feat: avoid uploading duplicate files and get fullpath
Browse files Browse the repository at this point in the history
  • Loading branch information
g-saracca committed Mar 7, 2025
1 parent 41b0d8a commit c10bd18
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
29 changes: 25 additions & 4 deletions src/sections/shared/file-uploader/FileUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type FileStorageConfiguration = 'S3'
const limit = 6
const semaphore = new Semaphore(limit)

// TODO:ME - Check semaphore working ok and not missing somewhere
// TODO:ME - Check the fix validity endpoint to know the hashing algorithm to use

export const FileUploader = ({
Expand Down Expand Up @@ -101,7 +100,7 @@ export const FileUploader = ({

const uploadOneFile = async (file: File) => {
if (FileUploaderHelper.isDS_StoreFile(file)) {
toast.info(`We did not upload the file ${file.name} as it is a .DS_Store file`)
toast.info(`File ${file.name} dismissed as it is a .DS_Store file.`)
return
}

Expand All @@ -118,6 +117,14 @@ export const FileUploader = ({
}
}

// Check if file is already being uploaded or has already been uploaded
if (getFileByKey(FileUploaderHelper.getFileKey(file))) {
const fileInfo = getFileByKey(FileUploaderHelper.getFileKey(file)) as FileUploadState
toast.info(`File ${fileInfo.key} dismissed as it is or has already being uploaded.`)

return
}

await semaphore.acquire(1)

// TODO:ME - There was a sanity check here, needed? or leave it?
Expand Down Expand Up @@ -167,7 +174,17 @@ export const FileUploader = ({
if (entry.isFile) {
const fse = entry as FileSystemFileEntry
fse.file((file) => {
void uploadOneFile(file)
const fileWithPath = new File([file], file.name, {
type: file.type,
lastModified: file.lastModified
})

Object.defineProperty(fileWithPath, 'webkitRelativePath', {
value: entry.fullPath,
writable: true
})

void uploadOneFile(fileWithPath)
})
} else if (entry.isDirectory) {
addFromDir(entry as FileSystemDirectoryEntry)
Expand Down Expand Up @@ -307,7 +324,11 @@ export const FileUploader = ({
key={file.key}>
<div className={styles.info_progress_wrapper}>
<p className={styles.info}>
<span>{file.fileName}</span>
<span>
{file.fileDir
? `${file.fileDir}/${file.fileName}`
: file.fileName}
</span>
<small>{file.fileSizeString}</small>
</p>

Expand Down
4 changes: 2 additions & 2 deletions src/sections/shared/file-uploader/fileUploaderReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const fileUploaderReducer = (state: FileUploaderState, action: Action): FileUplo
done: false,
removed: false,
fileName: file.name,
fileDir: toDir(file.webkitRelativePath),
fileDir: file.webkitRelativePath ? toDir(file.webkitRelativePath) : undefined,
fileType: file.type,
tags: [],
restricted: false
Expand Down Expand Up @@ -134,7 +134,7 @@ export const useFileUploader = () => {
)
const removeFile = useCallback((key: string) => dispatch({ type: 'REMOVE_FILE', key }), [])

const getFileByKey = (key: string) => state[key]
const getFileByKey = (key: string): FileUploadState | undefined => state[key]

return { state, addFiles, addFile, updateFile, removeFile, getFileByKey }
}
Expand Down

0 comments on commit c10bd18

Please sign in to comment.