:tada:初步完成用户
This commit is contained in:
50
utils/file_system.go
Normal file
50
utils/file_system.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/google/uuid"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"website-nav/config"
|
||||
"website-nav/global/client"
|
||||
)
|
||||
|
||||
type fileSystem struct{}
|
||||
|
||||
func FileSystem() fileSystem {
|
||||
return fileSystem{}
|
||||
}
|
||||
|
||||
// UploadFile
|
||||
// @description: 上传文件
|
||||
// @receiver fileSystem
|
||||
// @param file
|
||||
// @return filePath
|
||||
// @return err
|
||||
func (fileSystem) UploadFile(file []byte, suffix string) (filePath string, err error) {
|
||||
switch config.GlobalConfig.File.Type {
|
||||
case "oss":
|
||||
ossObj, err := client.OSS.Put(config.GlobalConfig.File.Path, file, suffix)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
filePath = ossObj.LongPath
|
||||
case "local":
|
||||
basePath := fmt.Sprintf("%s/%d/avatar", config.GlobalConfig.File.Path, time.Now().Unix())
|
||||
filePath = fmt.Sprintf("%s/%s%s", basePath, strings.ReplaceAll(uuid.NewString(), "-", ""), suffix)
|
||||
// 创建目录
|
||||
if err = os.MkdirAll(basePath, os.FileMode(0777)); err != nil {
|
||||
log.Errorf("本地存储目录创建失败: %v", err)
|
||||
return "", err
|
||||
}
|
||||
if err = os.WriteFile(filePath, file, os.FileMode(0777)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
subPath := strings.ReplaceAll(filePath, fmt.Sprintf("%s/", config.GlobalConfig.File.Path), "")
|
||||
filePath = fmt.Sprintf("http://%s/assets/%s", config.GlobalConfig.Http.Endpoint, subPath)
|
||||
}
|
||||
return filePath, err
|
||||
}
|
||||
Reference in New Issue
Block a user