package utils import ( "fmt" "gitee.ltd/lxh/logger/log" "github.com/google/uuid" "os" "strings" "time" "wireguard-ui/config" "wireguard-ui/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.Config.File.Type { case "oss": ossObj, err := client.OSS.Put(config.Config.File.Path, file, suffix) if err != nil { return "", err } filePath = ossObj.LongPath case "local": basePath := fmt.Sprintf("%s/%d/avatar", config.Config.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.Config.File.Path), "") filePath = fmt.Sprintf("http://%s/assets/%s", config.Config.Http.Endpoint, subPath) } return filePath, err }