From cb26003b9974ba3823fd1748d52eb1cfca7fd86d Mon Sep 17 00:00:00 2001 From: Hexxa Date: Mon, 21 Dec 2020 23:20:06 +0800 Subject: [PATCH] fix(uploader, files/handlers): fix incorrect unlock, catch and check error after calling api (#26) * fix(uploader, files/handlers): fix incorrect unlock, catch and check after calling api * fix(uploader): fix uploader test --- src/client/web/src/client/uploader.ts | 31 +++++++++++++++++++++------ src/handlers/fileshdr/handlers.go | 8 +++++-- src/server/server_files_test.go | 4 ++-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/client/web/src/client/uploader.ts b/src/client/web/src/client/uploader.ts index 8e125fb..6ca8277 100644 --- a/src/client/web/src/client/uploader.ts +++ b/src/client/web/src/client/uploader.ts @@ -109,19 +109,36 @@ export class FileUploader { } else { this.errMsg = uploadResp.statusText; this.chunkLen = Math.ceil(this.chunkLen * speedDownRatio); - const uploadStatusResp = await this.uploadStatus(this.filePath); + + let uploadStatusResp: Response = undefined; + try { + uploadStatusResp = await this.uploadStatus(this.filePath); + } catch (e) { + if (uploadStatusResp == null) { + this.errMsg = `${this.errMsg}; unknown error: empty uploadStatus response`; + break; + } else if (uploadStatusResp.status === 500) { + if ( + !uploadStatusResp.statusText.includes("fail to lock the file") && + uploadStatusResp.statusText !== "" + ) { + this.errMsg = `${this.errMsg}; unknown error: ${uploadStatusResp.statusText}`; + break; + } + } else if (uploadStatusResp.status === 600) { + this.errMsg = `${this.errMsg}; unknown error: ${uploadStatusResp.statusText}`; + break; + } else { + // ignore error and retry + } + } if (uploadStatusResp.status === 200) { this.offset = uploadStatusResp.data.uploaded; - } else if (uploadStatusResp.status === 600) { - this.errMsg = "unknown error"; - break; - } else { - // do nothing and retry } } - if (this.progressCb != null) { + if (this.progressCb != null) { this.progressCb(this.filePath, Math.ceil(this.offset / this.file.size)); } } diff --git a/src/handlers/fileshdr/handlers.go b/src/handlers/fileshdr/handlers.go index 2ba3f04..fd940b8 100644 --- a/src/handlers/fileshdr/handlers.go +++ b/src/handlers/fileshdr/handlers.go @@ -76,13 +76,16 @@ func (h *FileHandlers) NewAutoLocker(c *gin.Context, key string) *AutoLocker { func (lk *AutoLocker) Exec(handler func()) { var err error kv := lk.h.deps.KV() + locked := false defer func() { if p := recover(); p != nil { fmt.Println(p) } - if err = kv.Unlock(lk.key); err != nil { - fmt.Println(err) + if locked { + if err = kv.Unlock(lk.key); err != nil { + fmt.Println(err) + } } }() @@ -91,6 +94,7 @@ func (lk *AutoLocker) Exec(handler func()) { return } + locked = true handler() } diff --git a/src/server/server_files_test.go b/src/server/server_files_test.go index 7895a08..aa8c2dc 100644 --- a/src/server/server_files_test.go +++ b/src/server/server_files_test.go @@ -234,11 +234,11 @@ func TestFileHandlers(t *testing.T) { t.Run("test dirs APIs: Mkdir-Create-UploadChunk-List", func(t *testing.T) { for dirPath, files := range map[string]map[string]string{ - "dir/path1/": map[string]string{ + "dir/path1": map[string]string{ "f1.md": "11111", "f2.md": "22222222222", }, - "dir/path1/path2": map[string]string{ + "dir/path2/path2": map[string]string{ "f3.md": "3333333", }, } {