Skip to content

Commit

Permalink
.uploadstemp for every disk
Browse files Browse the repository at this point in the history
  • Loading branch information
lovehunter9 committed Jan 17, 2025
1 parent b46a1e3 commit e147c51
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
43 changes: 12 additions & 31 deletions cmd/upload/app/handler4.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/shirou/gopsutil/mem"
"k8s.io/klog/v2"
"os"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -73,28 +72,6 @@ func extractPart(s string) string {
}
}

func AddVersionSuffix(source string) string {
counter := 1
dir, name := path.Split(source)
ext := filepath.Ext(name)
base := strings.TrimSuffix(name, ext)

for {
if _, err := os.Stat(source); err == nil {
renamed := fmt.Sprintf("%s(%d)%s", base, counter, ext)
source = path.Join(dir, renamed)
counter++
} else if os.IsNotExist(err) {
break
} else {
fmt.Println("Error checking file:", err)
break
}
}

return source
}

func (a *appController) UploadLink(c *fiber.Ctx) error {
_, userPvc, cachePvc, uploadsDir, err := getPVC(c)
if err != nil {
Expand Down Expand Up @@ -197,12 +174,13 @@ func (a *appController) UploadedBytes(c *fiber.Ctx) error {
}
klog.Infof("c:%+v", c)

fullPath := AddVersionSuffix(filepath.Join(parentDir, fileName))
//fullPath := AddVersionSuffix(filepath.Join(parentDir, fileName))
fullPath := filepath.Join(parentDir, fileName)

dirPath := filepath.Dir(fullPath)

dstName := filepath.Base(fullPath)
tmpName := dstName + ".uploading"
//dstName := filepath.Base(fullPath)
//tmpName := dstName + ".uploading"

if !utils.CheckDirExist(dirPath) {
return c.JSON(responseData)
Expand All @@ -217,6 +195,7 @@ func (a *appController) UploadedBytes(c *fiber.Ctx) error {
//uploadID := uid.MakeUid(fullPath)
//resumableIdentifier := uid.GenerateUniqueIdentifier(fileName)
innerIdentifier := uid.MakeUid(fullPath)
tmpName := innerIdentifier
fileutils.UploadsFiles4[innerIdentifier] = filepath.Join(uploadsDir, tmpName) // innerIdentifier)
exist, info := a.server.fileInfoMgr.ExistFileInfo(innerIdentifier)
//fileExist, fileLen := a.server.fileInfoMgr.CheckTempFile4(innerIdentifier, uploadsDir)
Expand Down Expand Up @@ -400,11 +379,13 @@ func (a *appController) UploadChunks(c *fiber.Ctx) error {
klog.Infof("uploadID:%s, patchInfo:%+v", uploadID, resumableInfo)

// Get file information based on upload ID
fullPath := AddVersionSuffix(filepath.Join(parentDir, resumableInfo.ResumableRelativePath))
dstName := filepath.Base(fullPath)
tmpName := dstName + ".uploading"
//fullPath := AddVersionSuffix(filepath.Join(parentDir, resumableInfo.ResumableRelativePath))
fullPath := filepath.Join(parentDir, resumableInfo.ResumableRelativePath)
//dstName := filepath.Base(fullPath)
//tmpName := dstName + ".uploading"
//resumableIdentifier := resumableInfo.ResumableIdentifier
innerIdentifier := uid.MakeUid(fullPath)
tmpName := innerIdentifier
fileutils.UploadsFiles4[innerIdentifier] = filepath.Join(uploadsDir, tmpName) // innerIdentifier)
exist, info := a.server.fileInfoMgr.ExistFileInfo(innerIdentifier)
if !exist {
Expand Down Expand Up @@ -693,8 +674,8 @@ func (a *appController) UploadChunks(c *fiber.Ctx) error {
// Check if the file has been written
if info.Offset == info.FileSize {
// Move the file to the specified upload path
//err = fileutils.MoveFileByInfo4(info, uploadsDir)
err = fileutils.RenameFileByInfo4(info, uploadsDir)
err = fileutils.MoveFileByInfo4(info, uploadsDir)
//err = fileutils.RenameFileByInfo4(info, uploadsDir)
if err != nil {
klog.Warningf("innerIdentifier:%s, info:%+v, err:%v", innerIdentifier, info, err)
return c.Status(fiber.StatusInternalServerError).JSON(
Expand Down
30 changes: 27 additions & 3 deletions pkg/upload/fileutils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/klog/v2"
"mime/multipart"
"os"
"path"
"path/filepath"
"sort"
"strconv"
Expand Down Expand Up @@ -378,12 +379,35 @@ func ClearTempFileContent(uid string, uploadsDir string) {
}
}

func AddVersionSuffix(source string) string {
counter := 1
dir, name := path.Split(source)
ext := filepath.Ext(name)
base := strings.TrimSuffix(name, ext)

for {
if _, err := os.Stat(source); err == nil {
renamed := fmt.Sprintf("%s(%d)%s", base, counter, ext)
source = path.Join(dir, renamed)
counter++
} else if os.IsNotExist(err) {
break
} else {
fmt.Println("Error checking file:", err)
break
}
}

return source
}

func RenameFileByInfo4(fileInfo models.FileInfo, uploadsDir string) error {
// Construct the current file path
filePath := fileInfo.FullPath + ".uploading" // filepath.Join(uploadsDir, fileInfo.ID)
//filePath := fileInfo.FullPath + ".uploading"
filePath := filepath.Join(uploadsDir, fileInfo.ID)

// Construct the target path
destinationPath := fileInfo.FullPath
destinationPath := AddVersionSuffix(fileInfo.FullPath)

// Perform the move operation by renaming the file
err := os.Rename(filePath, destinationPath)
Expand All @@ -401,7 +425,7 @@ func MoveFileByInfo4(fileInfo models.FileInfo, uploadsDir string) error {
filePath := filepath.Join(uploadsDir, fileInfo.ID)

// Construct target path
destinationPath := fileInfo.FullPath
destinationPath := AddVersionSuffix(fileInfo.FullPath)

// Move files to target path
err := MoveFile(filePath, destinationPath)
Expand Down

0 comments on commit e147c51

Please sign in to comment.