mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
refactor(backend): update UploadFileSlice and CreateFileSlice functions to include upload path handling for improved file management
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -37,12 +37,14 @@ func GetFileMd5(file io.Reader) (string, error) {
|
||||
}
|
||||
|
||||
func GetUploadDirPath() (string, error) {
|
||||
basepath, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
uploadPath := utils.GetEnv("upload.path")
|
||||
if uploadPath == "" {
|
||||
basepath, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
uploadPath = filepath.Join(basepath, "uploads")
|
||||
}
|
||||
finalPath := filepath.Join(basepath, "uploads")
|
||||
uploadPath := utils.GetEnvWithDefault("upload.path", finalPath)
|
||||
if err := os.MkdirAll(uploadPath, 0755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user