2024-03-06 16:24:49 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-05-08 14:21:49 +08:00
|
|
|
"gitee.ltd/lxh/logger/log"
|
2024-03-06 16:24:49 +08:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
"wireguard-dashboard/client"
|
|
|
|
"wireguard-dashboard/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
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.Config.File.Type {
|
|
|
|
case "oss":
|
|
|
|
ossObj, err := client.OSS.Put(config.Config.File.Path, file, suffix)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ossObj.LongPath, nil
|
|
|
|
case "local":
|
|
|
|
filePath = fmt.Sprintf("%v/%d-avatar%s", config.Config.File.Path, time.Now().Unix(), suffix)
|
2024-05-08 14:21:49 +08:00
|
|
|
// 创建目录
|
|
|
|
if err = os.MkdirAll(filePath, os.FileMode(0777)); err != nil {
|
|
|
|
log.Errorf("本地存储目录创建失败: %v", err)
|
|
|
|
return "", err
|
|
|
|
}
|
2024-03-06 16:24:49 +08:00
|
|
|
if err = os.WriteFile(filePath, file, os.FileMode(0777)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filePath, nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|