[files] auto rename file if there is a duplicated one (#56)

This commit is contained in:
Hexxa 2021-05-20 15:59:36 +08:00 committed by GitHub
parent 884f255e2d
commit 10c13f5ad8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 4 deletions

View file

@ -296,7 +296,12 @@ func (h *FileHandlers) UploadChunk(c *gin.Context) {
// move the file from uploading dir to uploaded dir // move the file from uploading dir to uploaded dir
if uploaded+int64(wrote) == fileSize { if uploaded+int64(wrote) == fileSize {
fsFilePath := h.FsPath(req.Path) fsFilePath, err := h.getFSFilePath(req.Path)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
err = h.deps.FS().Rename(tmpFilePath, fsFilePath) err = h.deps.FS().Rename(tmpFilePath, fsFilePath)
if err != nil { if err != nil {
c.JSON(q.ErrResp(c, 500, fmt.Errorf("%s error: %w", req.Path, err))) c.JSON(q.ErrResp(c, 500, fmt.Errorf("%s error: %w", req.Path, err)))
@ -318,6 +323,38 @@ func (h *FileHandlers) UploadChunk(c *gin.Context) {
}) })
} }
func (h *FileHandlers) getFSFilePath(reqPath string) (string, error) {
fsFilePath := h.FsPath(reqPath)
_, err := h.deps.FS().Stat(fsFilePath)
if err != nil {
if os.IsNotExist(err) {
return fsFilePath, nil
}
return "", err
}
// this file exists
maxDetect := 1024
for i := 1; i < maxDetect; i++ {
dir := path.Dir(fsFilePath)
nameAndExt := path.Base(fsFilePath)
ext := path.Ext(nameAndExt)
fileName := nameAndExt[:len(nameAndExt)-len(ext)]
detectPath := path.Join(dir, fmt.Sprintf("%s_%d%s", fileName, i, ext))
_, err := h.deps.FS().Stat(detectPath)
if err != nil {
if os.IsNotExist(err) {
return detectPath, nil
} else {
return "", err
}
}
}
return "", fmt.Errorf("found more than %d duplicated files", maxDetect)
}
type UploadStatusResp struct { type UploadStatusResp struct {
Path string `json:"path"` Path string `json:"path"`
IsDir bool `json:"isDir"` IsDir bool `json:"isDir"`

View file

@ -149,6 +149,38 @@ func TestFileHandlers(t *testing.T) {
return true return true
} }
t.Run("test uploading files with duplicated names", func(t *testing.T) {
files := map[string]string{
"dupdir/dup_file1": "12345678",
"dupdir/dup_file2.ext": "12345678",
}
renames := map[string]string{
"dupdir/dup_file1": "dupdir/dup_file1_1",
"dupdir/dup_file2.ext": "dupdir/dup_file2_1.ext",
}
for filePath, content := range files {
for i := 0; i < 2; i++ {
assertUploadOK(t, filePath, content)
err = fs.Sync()
if err != nil {
t.Fatal(err)
}
if i == 0 {
assetDownloadOK(t, filePath, content)
} else if i == 1 {
renamedFilePath, ok := renames[filePath]
if !ok {
t.Fatal("new name not found")
}
assetDownloadOK(t, renamedFilePath, content)
}
}
}
})
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{
"path1/f1.md": "1111 1111 1111 1111", "path1/f1.md": "1111 1111 1111 1111",
@ -397,7 +429,7 @@ func TestFileHandlers(t *testing.T) {
wg.Wait() wg.Wait()
}) })
t.Run("test uploading APIs: Create, ListUploadings, DelUploading)", func(t *testing.T) { t.Run("test uploading APIs: Create, ListUploadings, DelUploading", func(t *testing.T) {
files := map[string]string{ files := map[string]string{
"uploadings/path1/f1": "123456", "uploadings/path1/f1": "123456",
"uploadings/path1/path2": "12345678", "uploadings/path1/path2": "12345678",
@ -454,7 +486,7 @@ func TestFileHandlers(t *testing.T) {
} }
}) })
t.Run("test uploading APIs: Create, Stop, UploadChunk)", func(t *testing.T) { t.Run("test uploading APIs: Create, Stop, UploadChunk", func(t *testing.T) {
cl := client.NewFilesClient(addr) cl := client.NewFilesClient(addr)
files := map[string]string{ files := map[string]string{
@ -509,7 +541,7 @@ func TestFileHandlers(t *testing.T) {
} }
}) })
t.Run("test uploading APIs: Create and UploadChunk randomly)", func(t *testing.T) { t.Run("test uploading APIs: Create and UploadChunk randomly", func(t *testing.T) {
cl := client.NewFilesClient(addr) cl := client.NewFilesClient(addr)
files := map[string]string{ files := map[string]string{