fix: fails to upload empty file

This commit is contained in:
hexxa 2022-01-25 22:39:05 +08:00 committed by Hexxa
parent ae03e8c95c
commit 75c6f06a6d
3 changed files with 97 additions and 31 deletions

View file

@ -67,33 +67,46 @@ export class UploadWorker {
}; };
onMsg = async (event: MessageEvent) => { onMsg = async (event: MessageEvent) => {
this.working = true; try {
const req = event.data as FileWorkerReq; this.working = true;
const req = event.data as FileWorkerReq;
switch (req.kind) { switch (req.kind) {
case syncReqKind: case syncReqKind:
const syncReq = req as SyncReq; const syncReq = req as SyncReq;
if (syncReq.created) { if (syncReq.created) {
const status = await this.uploader.upload( if (syncReq.file.size === 0) {
syncReq.filePath, const resp: UploadInfoResp = {
syncReq.file, kind: uploadInfoKind,
syncReq.uploaded filePath: syncReq.filePath,
); uploaded: 0,
await this.handleUploadStatus(status); state: UploadState.Ready,
} else { err: "",
const status = await this.uploader.create( };
syncReq.filePath, this.sendEvent(resp);
syncReq.file } else {
); const status = await this.uploader.upload(
await this.handleUploadStatus(status); syncReq.filePath,
} syncReq.file,
break; syncReq.uploaded
default: );
console.error(`unknown worker request(${JSON.stringify(req)})`); await this.handleUploadStatus(status);
}
} else {
const status = await this.uploader.create(
syncReq.filePath,
syncReq.file
);
await this.handleUploadStatus(status);
}
break;
default:
console.error(`unknown worker request(${JSON.stringify(req)})`);
}
} finally {
this.working = false;
} }
this.working = false;
}; };
onError = (ev: ErrorEvent) => { onError = (ev: ErrorEvent) => {

View file

@ -143,6 +143,55 @@ func (h *FileHandlers) Create(c *gin.Context) {
return 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) tmpFilePath := q.UploadPath(userName, req.Path)
locker := h.NewAutoLocker(c, lockName(tmpFilePath)) locker := h.NewAutoLocker(c, lockName(tmpFilePath))
locker.Exec(func() { locker.Exec(func() {

View file

@ -190,6 +190,7 @@ func TestFileHandlers(t *testing.T) {
t.Run("test files APIs: Create-UploadChunk-UploadStatus-Metadata-Delete", func(t *testing.T) { t.Run("test files APIs: Create-UploadChunk-UploadStatus-Metadata-Delete", func(t *testing.T) {
for filePath, content := range map[string]string{ for filePath, content := range map[string]string{
"qs/files/path1/empty": "",
"qs/files/path1/f1.md": "1111 1111 1111 1111", "qs/files/path1/f1.md": "1111 1111 1111 1111",
"qs/files/path1/path2/f2.md": "1010 1010 1111 0000 0010", "qs/files/path1/path2/f2.md": "1010 1010 1111 0000 0010",
} { } {
@ -203,12 +204,15 @@ func TestFileHandlers(t *testing.T) {
} }
// check uploading file // check uploading file
uploadFilePath := q.UploadPath(adminName, filePath) // empty file is not created under the uploading folder
info, err := fs.Stat(uploadFilePath) if fileSize != 0 {
if err != nil { uploadFilePath := q.UploadPath(adminName, filePath)
t.Fatal(err) info, err := fs.Stat(uploadFilePath)
} else if info.Name() != filepath.Base(uploadFilePath) { if err != nil {
t.Fatal(info.Name(), filepath.Base(uploadFilePath)) t.Fatal(err)
} else if info.Name() != filepath.Base(uploadFilePath) {
t.Fatal(info.Name(), filepath.Base(uploadFilePath))
}
} }
// upload a chunk // upload a chunk
@ -250,7 +254,7 @@ func TestFileHandlers(t *testing.T) {
// check uploaded file // check uploaded file
// fsFilePath := filepath.Join("0", filePath) // fsFilePath := filepath.Join("0", filePath)
info, err = fs.Stat(filePath) info, err := fs.Stat(filePath)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} else if info.Name() != filepath.Base(filePath) { } else if info.Name() != filepath.Base(filePath) {