From 695ac88eac5222ed13ba54c9098cf5a524af5c81 Mon Sep 17 00:00:00 2001 From: Tobias Goerke Date: Wed, 26 Jul 2023 14:19:00 +0200 Subject: [PATCH] fix: clear uploads if file doesn't exist --- frontend/src/api/tus.js | 2 -- http/tus_store.go | 23 ++++++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/frontend/src/api/tus.js b/frontend/src/api/tus.js index 3a699b3a..bf7c6811 100644 --- a/frontend/src/api/tus.js +++ b/frontend/src/api/tus.js @@ -14,8 +14,6 @@ export async function upload(url, content = "", overwrite = false, onupload) { return new Promise((resolve, reject) => { const metadata = { - filename: content.name, - filetype: content.type, overwrite: overwrite.toString(), // url is URI encoded and needs to be decoded for metadata first destination: decodeURIComponent(removePrefix(url)), diff --git a/http/tus_store.go b/http/tus_store.go index 09229afa..843e956f 100644 --- a/http/tus_store.go +++ b/http/tus_store.go @@ -54,9 +54,19 @@ func (store *InPlaceDataStore) UseIn(composer *tusd.StoreComposer) { composer.UseConcater(store) } -func prepareFile(filePath string, uploadSize int64, mutex *sync.Mutex) (int64, error) { - mutex.Lock() - defer mutex.Unlock() +func (store *InPlaceDataStore) prepareFile(filePath string, info *tusd.FileInfo) (int64, error) { + store.mutex.Lock() + defer store.mutex.Unlock() + + // If the file doesn't exist, remove all upload references. + // This way we can eliminate inconsistencies for failed uploads. + if _, err := os.Stat(filePath); os.IsNotExist(err) { + for id, upload := range store.uploads { + if upload.filePath == filePath { + delete(store.uploads, id) + } + } + } // Create the file if it doesn't exist. file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, filePerm) @@ -71,7 +81,7 @@ func prepareFile(filePath string, uploadSize int64, mutex *sync.Mutex) (int64, e return 0, err } // Enlarge the file by the upload's size. - _, err = file.Write(make([]byte, uploadSize)) + _, err = file.Write(make([]byte, info.Size)) if err != nil { return 0, err } @@ -97,9 +107,8 @@ func (store *InPlaceDataStore) NewUpload(ctx context.Context, info tusd.FileInfo // Tus creates a POST request for the final concatenation. // In that case, we don't need to create a new upload. actualOffset := info.Size - if info.IsPartial { - actualOffset, err = prepareFile(filePath, info.Size, store.mutex) - if err != nil { + if !info.IsFinal { + if actualOffset, err = store.prepareFile(filePath, &info); err != nil { return nil, err } }