refactor(backend): move GetUploadDirPath function to utils for better modularity

This commit is contained in:
keven1024
2025-05-24 21:39:04 +08:00
parent 7b263b7b76
commit 4b4773e336
5 changed files with 19 additions and 18 deletions

View File

@@ -2,7 +2,6 @@ package controllers
import (
"backend/internal/models"
"backend/internal/services"
"backend/internal/utils"
"backend/middleware"
"errors"
@@ -38,7 +37,7 @@ func DownloadShare(c echo.Context) error {
if shareInfo.Type == models.ShareTypeFile {
fileInfo, _ := models.GetRedisFileInfo(shareInfo.Data)
uploadPath, err := services.GetUploadDirPath()
uploadPath, err := utils.GetUploadDirPath()
if err != nil {
return err
}

View File

@@ -150,7 +150,7 @@ func FinishUploadTask(c echo.Context) error {
}
// 合并文件切片
uploadPath, _ := services.GetUploadDirPath()
uploadPath, _ := utils.GetUploadDirPath()
slicesPath := filepath.Join(uploadPath, fmt.Sprintf("%s_%s", r.FileId, "tmp"))
// 最终合并后的文件路径

View File

@@ -26,7 +26,7 @@ type RedisFileInfo struct {
FileInfo
FileType FileType `json:"type"`
CreatedAt int64 `json:"created_at"`
Expire int64 `json:"expire"`
Expire int64 `json:"expire"` // 只有上传文件(init)的时候有这个字段
}
func GetRedisFileInfo(fileId string) (*RedisFileInfo, error) {

View File

@@ -9,21 +9,8 @@ import (
"strconv"
)
func GetUploadDirPath() (string, error) {
basepath, err := os.Getwd()
if err != nil {
return "", err
}
finalPath := filepath.Join(basepath, "uploads")
uploadPath := utils.GetEnvWithDefault("UPLOAD_PATH", finalPath)
if err := os.MkdirAll(uploadPath, 0755); err != nil {
return "", err
}
return uploadPath, nil
}
func CreateFileSlice(fileSlice io.Reader, fileId string, fileIndex int64) error {
uploadPath, err := GetUploadDirPath()
uploadPath, err := utils.GetUploadDirPath()
if err != nil {
return err
}

View File

@@ -5,6 +5,8 @@ import (
"crypto/md5"
"fmt"
"io"
"os"
"path/filepath"
)
func GetFileId(fileHash string, fileSize int64) string {
@@ -30,3 +32,16 @@ func GetFileMd5(file io.Reader) (string, error) {
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func GetUploadDirPath() (string, error) {
basepath, err := os.Getwd()
if err != nil {
return "", err
}
finalPath := filepath.Join(basepath, "uploads")
uploadPath := GetEnvWithDefault("UPLOAD_PATH", finalPath)
if err := os.MkdirAll(uploadPath, 0755); err != nil {
return "", err
}
return uploadPath, nil
}