fix: fails to upload empty file
This commit is contained in:
parent
ae03e8c95c
commit
75c6f06a6d
3 changed files with 97 additions and 31 deletions
|
@ -67,6 +67,7 @@ export class UploadWorker {
|
|||
};
|
||||
|
||||
onMsg = async (event: MessageEvent) => {
|
||||
try {
|
||||
this.working = true;
|
||||
const req = event.data as FileWorkerReq;
|
||||
|
||||
|
@ -75,12 +76,23 @@ export class UploadWorker {
|
|||
const syncReq = req as SyncReq;
|
||||
|
||||
if (syncReq.created) {
|
||||
if (syncReq.file.size === 0) {
|
||||
const resp: UploadInfoResp = {
|
||||
kind: uploadInfoKind,
|
||||
filePath: syncReq.filePath,
|
||||
uploaded: 0,
|
||||
state: UploadState.Ready,
|
||||
err: "",
|
||||
};
|
||||
this.sendEvent(resp);
|
||||
} else {
|
||||
const status = await this.uploader.upload(
|
||||
syncReq.filePath,
|
||||
syncReq.file,
|
||||
syncReq.uploaded
|
||||
);
|
||||
await this.handleUploadStatus(status);
|
||||
}
|
||||
} else {
|
||||
const status = await this.uploader.create(
|
||||
syncReq.filePath,
|
||||
|
@ -92,8 +104,9 @@ export class UploadWorker {
|
|||
default:
|
||||
console.error(`unknown worker request(${JSON.stringify(req)})`);
|
||||
}
|
||||
|
||||
} finally {
|
||||
this.working = false;
|
||||
}
|
||||
};
|
||||
|
||||
onError = (ev: ErrorEvent) => {
|
||||
|
|
|
@ -143,6 +143,55 @@ func (h *FileHandlers) Create(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
if req.FileSize == 0 {
|
||||
err = h.deps.FS().MkdirAll(filepath.Dir(req.Path))
|
||||
if err != nil {
|
||||
c.JSON(q.ErrResp(c, 500, err))
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: limit the number of files with 0 byte
|
||||
|
||||
fsFilePath, err := h.getFSFilePath(userID, req.Path)
|
||||
if err != nil {
|
||||
c.JSON(q.ErrResp(c, 500, err))
|
||||
return
|
||||
}
|
||||
|
||||
err = h.deps.FS().Create(fsFilePath)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
c.JSON(q.ErrResp(c, 304, fmt.Errorf("file(%s) exists", fsFilePath)))
|
||||
} else {
|
||||
c.JSON(q.ErrResp(c, 500, err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
msg, err := json.Marshal(Sha1Params{
|
||||
FilePath: fsFilePath,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(q.ErrResp(c, 500, err))
|
||||
return
|
||||
}
|
||||
|
||||
err = h.deps.Workers().TryPut(
|
||||
localworker.NewMsg(
|
||||
h.deps.ID().Gen(),
|
||||
map[string]string{localworker.MsgTypeKey: MsgTypeSha1},
|
||||
string(msg),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(q.ErrResp(c, 500, err))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(q.Resp(200))
|
||||
return
|
||||
}
|
||||
|
||||
tmpFilePath := q.UploadPath(userName, req.Path)
|
||||
locker := h.NewAutoLocker(c, lockName(tmpFilePath))
|
||||
locker.Exec(func() {
|
||||
|
|
|
@ -190,6 +190,7 @@ func TestFileHandlers(t *testing.T) {
|
|||
|
||||
t.Run("test files APIs: Create-UploadChunk-UploadStatus-Metadata-Delete", func(t *testing.T) {
|
||||
for filePath, content := range map[string]string{
|
||||
"qs/files/path1/empty": "",
|
||||
"qs/files/path1/f1.md": "1111 1111 1111 1111",
|
||||
"qs/files/path1/path2/f2.md": "1010 1010 1111 0000 0010",
|
||||
} {
|
||||
|
@ -203,6 +204,8 @@ func TestFileHandlers(t *testing.T) {
|
|||
}
|
||||
|
||||
// check uploading file
|
||||
// empty file is not created under the uploading folder
|
||||
if fileSize != 0 {
|
||||
uploadFilePath := q.UploadPath(adminName, filePath)
|
||||
info, err := fs.Stat(uploadFilePath)
|
||||
if err != nil {
|
||||
|
@ -210,6 +213,7 @@ func TestFileHandlers(t *testing.T) {
|
|||
} else if info.Name() != filepath.Base(uploadFilePath) {
|
||||
t.Fatal(info.Name(), filepath.Base(uploadFilePath))
|
||||
}
|
||||
}
|
||||
|
||||
// upload a chunk
|
||||
i := 0
|
||||
|
@ -250,7 +254,7 @@ func TestFileHandlers(t *testing.T) {
|
|||
|
||||
// check uploaded file
|
||||
// fsFilePath := filepath.Join("0", filePath)
|
||||
info, err = fs.Stat(filePath)
|
||||
info, err := fs.Stat(filePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if info.Name() != filepath.Base(filePath) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue