refactor(backend): update UploadFileSlice and CreateFileSlice functions to include upload path handling for improved file management

This commit is contained in:
keven1024
2025-12-21 16:19:57 +08:00
parent b4a4be09fa
commit e8653a8e8b
3 changed files with 14 additions and 13 deletions

View File

@@ -150,7 +150,12 @@ func UploadFileSlice(c echo.Context) error {
}
defer file.Close()
if err := services.CreateFileSlice(file, r.FileId, r.FileIndex); err != nil {
uploadPath, err := utils.GetUploadDirPath()
if err != nil {
return utils.HTTPErrorHandler(c, err)
}
if err := services.CreateFileSlice(file, uploadPath, r.FileId, r.FileIndex); err != nil {
return utils.HTTPErrorHandler(c, err)
}

View File

@@ -1,7 +1,6 @@
package services
import (
"backend/internal/utils"
"fmt"
"io"
"os"
@@ -9,12 +8,7 @@ import (
"strconv"
)
func CreateFileSlice(fileSlice io.Reader, fileId string, fileIndex int64) error {
uploadPath, err := utils.GetUploadDirPath()
if err != nil {
return err
}
func CreateFileSlice(fileSlice io.Reader, uploadPath string, fileId string, fileIndex int64) error {
filePath := filepath.Join(uploadPath, fmt.Sprintf("%s_%s", fileId, "tmp"))
if err := os.MkdirAll(filePath, 0755); err != nil {
return err

View File

@@ -37,12 +37,14 @@ func GetFileMd5(file io.Reader) (string, error) {
}
func GetUploadDirPath() (string, error) {
uploadPath := utils.GetEnv("upload.path")
if uploadPath == "" {
basepath, err := os.Getwd()
if err != nil {
return "", err
}
finalPath := filepath.Join(basepath, "uploads")
uploadPath := utils.GetEnvWithDefault("upload.path", finalPath)
uploadPath = filepath.Join(basepath, "uploads")
}
if err := os.MkdirAll(uploadPath, 0755); err != nil {
return "", err
}