更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -20,33 +20,27 @@ import (
|
||||
"github.com/qiniu/go-sdk/v7/storage"
|
||||
)
|
||||
|
||||
// 来源端
|
||||
const (
|
||||
SourceBackend = "backend" // 租户后台端
|
||||
SourcePlatform = "platform" // 平台端
|
||||
SourceBackend = "backend"
|
||||
SourcePlatform = "platform"
|
||||
)
|
||||
|
||||
// 归属范围
|
||||
const (
|
||||
ScopeTenant = "tenant" // 租户共享文件
|
||||
ScopeUser = "user" // 用户个人文件
|
||||
ScopeTenant = "tenant"
|
||||
ScopeUser = "user"
|
||||
)
|
||||
|
||||
// 存储类型
|
||||
const (
|
||||
StorageTypeLocal = "local"
|
||||
StorageTypeQiniu = "qiniu"
|
||||
)
|
||||
|
||||
// UploadContext 上传上下文,决定文件最终的存储路径
|
||||
type UploadContext struct {
|
||||
Source string // backend / platform
|
||||
Tid uint64 // 租户 ID(platform 端可为 0)
|
||||
Tuid uint64 // 归属用户 ID,0 表示租户共享文件
|
||||
Source string
|
||||
Tid uint64
|
||||
Tuid uint64
|
||||
}
|
||||
|
||||
// Scope 返回归属范围:带了归属用户即个人文件,否则为租户共享文件。
|
||||
// 平台端不做用户分层,统一按租户共享处理。
|
||||
func (c UploadContext) Scope() string {
|
||||
if c.Source == SourcePlatform {
|
||||
return ScopeTenant
|
||||
@@ -57,11 +51,6 @@ func (c UploadContext) Scope() string {
|
||||
return ScopeTenant
|
||||
}
|
||||
|
||||
// BuildObjectKey 生成存储相对路径(不含域名、不含本地 BaseDir)
|
||||
//
|
||||
// backend 共享: backend/234573/2026/09/09/xxx.png
|
||||
// backend 个人: backend/234573/67091493/2026/09/09/xxx.png
|
||||
// platform : platform/2026/09/09/xxx.png
|
||||
func BuildObjectKey(ctx UploadContext, ext string) string {
|
||||
datePath := time.Now().Format("2006/01/02")
|
||||
name := fmt.Sprintf("%d_%s%s", time.Now().UnixNano(), randomHex(6), normalizeExt(ext))
|
||||
@@ -75,7 +64,6 @@ func BuildObjectKey(ctx UploadContext, ext string) string {
|
||||
return path.Join(SourceBackend, fmt.Sprint(ctx.Tid), datePath, name)
|
||||
}
|
||||
|
||||
// normalizeExt 规范扩展名:小写、补前导点
|
||||
func normalizeExt(ext string) string {
|
||||
ext = strings.TrimSpace(ext)
|
||||
if ext == "" {
|
||||
@@ -87,7 +75,6 @@ func normalizeExt(ext string) string {
|
||||
return strings.ToLower(ext)
|
||||
}
|
||||
|
||||
// randomHex 生成 n 位十六进制随机串(使用 crypto/rand,无需种子)
|
||||
func randomHex(n int) string {
|
||||
b := make([]byte, (n+1)/2)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
@@ -96,47 +83,37 @@ func randomHex(n int) string {
|
||||
return hex.EncodeToString(b)[:n]
|
||||
}
|
||||
|
||||
// StagedFile 已落临时文件、算完 MD5 的待提交文件
|
||||
type StagedFile struct {
|
||||
TempPath string // 本地临时文件路径
|
||||
MD5 string // 文件内容 MD5
|
||||
Size int64 // 文件大小
|
||||
MimeType string // 文件类型
|
||||
Ext string // 扩展名(含点)
|
||||
TempPath string
|
||||
MD5 string
|
||||
Size int64
|
||||
MimeType string
|
||||
Ext string
|
||||
}
|
||||
|
||||
// UploadResult 上传结果
|
||||
type UploadResult struct {
|
||||
URL string // 完整访问URL
|
||||
Key string // 存储key/相对路径(object_key)
|
||||
Size int64 // 文件大小
|
||||
MD5 string // 文件MD5
|
||||
MimeType string // 文件类型
|
||||
URL string
|
||||
Key string
|
||||
Size int64
|
||||
MD5 string
|
||||
MimeType string
|
||||
}
|
||||
|
||||
// StorageService 存储服务接口
|
||||
type StorageService interface {
|
||||
// Stage 把上传流写入临时文件并计算 MD5,此时文件尚未进入正式存储目录
|
||||
Stage(file multipart.File, header *multipart.FileHeader) (*StagedFile, error)
|
||||
// Commit 把临时文件提交到按 ctx 计算出的正式路径;返回访问 URL 与 object_key
|
||||
Commit(staged *StagedFile, ctx UploadContext) (*UploadResult, error)
|
||||
// Discard 丢弃临时文件(去重命中或出错时调用)
|
||||
Discard(staged *StagedFile) error
|
||||
GetPublicURL(key string) string
|
||||
Delete(key string) error
|
||||
// Move 把已存文件从 oldKey 改名到 newKey(迁移用:本地 Rename / 七牛 Move)
|
||||
Move(oldKey, newKey string) error
|
||||
// Type 返回存储类型 local / qiniu
|
||||
Type() string
|
||||
}
|
||||
|
||||
// LocalStorage 本地存储实现
|
||||
type LocalStorage struct {
|
||||
BaseDir string // 基础目录,默认 "uploads"
|
||||
BaseURL string // 基础URL,默认 "/"
|
||||
BaseDir string
|
||||
BaseURL string
|
||||
}
|
||||
|
||||
// NewLocalStorage 创建本地存储服务
|
||||
func NewLocalStorage() *LocalStorage {
|
||||
return &LocalStorage{
|
||||
BaseDir: "uploads",
|
||||
@@ -144,15 +121,12 @@ func NewLocalStorage() *LocalStorage {
|
||||
}
|
||||
}
|
||||
|
||||
// Type 存储类型
|
||||
func (s *LocalStorage) Type() string { return StorageTypeLocal }
|
||||
|
||||
// tempDir 临时目录:与正式目录同盘,保证 Commit 时 os.Rename 不跨设备
|
||||
func (s *LocalStorage) tempDir() string {
|
||||
return filepath.Join(s.BaseDir, ".tmp")
|
||||
}
|
||||
|
||||
// Stage 写入临时文件并计算 MD5
|
||||
func (s *LocalStorage) Stage(file multipart.File, header *multipart.FileHeader) (*StagedFile, error) {
|
||||
dir := s.tempDir()
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
@@ -181,7 +155,6 @@ func (s *LocalStorage) Stage(file multipart.File, header *multipart.FileHeader)
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Commit 提交到正式目录
|
||||
func (s *LocalStorage) Commit(staged *StagedFile, ctx UploadContext) (*UploadResult, error) {
|
||||
if staged == nil {
|
||||
return nil, fmt.Errorf("待提交文件为空")
|
||||
@@ -192,7 +165,6 @@ func (s *LocalStorage) Commit(staged *StagedFile, ctx UploadContext) (*UploadRes
|
||||
return nil, fmt.Errorf("创建目录失败: %w", err)
|
||||
}
|
||||
|
||||
// 同盘优先 Rename;跨设备失败则回退复制
|
||||
if err := os.Rename(staged.TempPath, destPath); err != nil {
|
||||
if copyErr := copyFile(staged.TempPath, destPath); copyErr != nil {
|
||||
return nil, fmt.Errorf("保存文件失败: %w", copyErr)
|
||||
@@ -209,7 +181,6 @@ func (s *LocalStorage) Commit(staged *StagedFile, ctx UploadContext) (*UploadRes
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Discard 删除临时文件
|
||||
func (s *LocalStorage) Discard(staged *StagedFile) error {
|
||||
if staged == nil || staged.TempPath == "" {
|
||||
return nil
|
||||
@@ -217,19 +188,16 @@ func (s *LocalStorage) Discard(staged *StagedFile) error {
|
||||
return os.Remove(staged.TempPath)
|
||||
}
|
||||
|
||||
// GetPublicURL 获取公开访问URL
|
||||
func (s *LocalStorage) GetPublicURL(key string) string {
|
||||
full := filepath.ToSlash(filepath.Join(s.BaseDir, filepath.FromSlash(key)))
|
||||
return s.BaseURL + strings.ReplaceAll(full, "\\", "/")
|
||||
}
|
||||
|
||||
// Delete 删除本地文件
|
||||
func (s *LocalStorage) Delete(key string) error {
|
||||
filePath := filepath.Join(s.BaseDir, filepath.FromSlash(key))
|
||||
return os.Remove(filePath)
|
||||
}
|
||||
|
||||
// Move 本地改名(同盘,不搬数据)
|
||||
func (s *LocalStorage) Move(oldKey, newKey string) error {
|
||||
oldPath := filepath.Join(s.BaseDir, filepath.FromSlash(oldKey))
|
||||
newPath := filepath.Join(s.BaseDir, filepath.FromSlash(newKey))
|
||||
@@ -237,7 +205,6 @@ func (s *LocalStorage) Move(oldKey, newKey string) error {
|
||||
return fmt.Errorf("创建目标目录失败: %w", err)
|
||||
}
|
||||
if err := os.Rename(oldPath, newPath); err != nil {
|
||||
// 跨设备时回退为复制 + 删除
|
||||
if copyErr := copyFile(oldPath, newPath); copyErr != nil {
|
||||
return fmt.Errorf("移动文件失败: %w", copyErr)
|
||||
}
|
||||
@@ -246,7 +213,6 @@ func (s *LocalStorage) Move(oldKey, newKey string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// QiniuStorage 七牛云存储实现
|
||||
type QiniuStorage struct {
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
@@ -255,7 +221,6 @@ type QiniuStorage struct {
|
||||
Region string
|
||||
}
|
||||
|
||||
// NewQiniuStorage 创建七牛云存储服务
|
||||
func NewQiniuStorage(cfg *models.StorageConfig) *QiniuStorage {
|
||||
return &QiniuStorage{
|
||||
AccessKey: cfg.QiniuAccessKey,
|
||||
@@ -266,10 +231,8 @@ func NewQiniuStorage(cfg *models.StorageConfig) *QiniuStorage {
|
||||
}
|
||||
}
|
||||
|
||||
// Type 存储类型
|
||||
func (s *QiniuStorage) Type() string { return StorageTypeQiniu }
|
||||
|
||||
// getZone 根据区域代码获取存储区域
|
||||
func (s *QiniuStorage) getZone() *storage.Region {
|
||||
switch s.Region {
|
||||
case "z0":
|
||||
@@ -285,12 +248,10 @@ func (s *QiniuStorage) getZone() *storage.Region {
|
||||
case "cn-east-2":
|
||||
return &storage.ZoneHuadongZheJiang2
|
||||
default:
|
||||
return &storage.ZoneHuadong // 默认华东
|
||||
return &storage.ZoneHuadong
|
||||
}
|
||||
}
|
||||
|
||||
// Stage 写入临时文件并计算 MD5(七牛直传拿不到内容,这里统一先落临时文件算 MD5,
|
||||
// 便于去重;未命中才会真正上传到七牛)
|
||||
func (s *QiniuStorage) Stage(file multipart.File, header *multipart.FileHeader) (*StagedFile, error) {
|
||||
tmp, err := os.CreateTemp("", "yz_upload_*")
|
||||
if err != nil {
|
||||
@@ -314,7 +275,6 @@ func (s *QiniuStorage) Stage(file multipart.File, header *multipart.FileHeader)
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Commit 上传到七牛云
|
||||
func (s *QiniuStorage) Commit(staged *StagedFile, ctx UploadContext) (*UploadResult, error) {
|
||||
if staged == nil {
|
||||
return nil, fmt.Errorf("待提交文件为空")
|
||||
@@ -352,7 +312,6 @@ func (s *QiniuStorage) Commit(staged *StagedFile, ctx UploadContext) (*UploadRes
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Discard 删除临时文件
|
||||
func (s *QiniuStorage) Discard(staged *StagedFile) error {
|
||||
if staged == nil || staged.TempPath == "" {
|
||||
return nil
|
||||
@@ -360,23 +319,19 @@ func (s *QiniuStorage) Discard(staged *StagedFile) error {
|
||||
return os.Remove(staged.TempPath)
|
||||
}
|
||||
|
||||
// GetPublicURL 获取七牛云公开访问URL
|
||||
func (s *QiniuStorage) GetPublicURL(key string) string {
|
||||
domain := strings.TrimRight(s.Domain, "/")
|
||||
return fmt.Sprintf("%s/%s", domain, key)
|
||||
}
|
||||
|
||||
// Delete 删除七牛云文件
|
||||
func (s *QiniuStorage) Delete(key string) error {
|
||||
return s.runBucketManager(func(bm *storage.BucketManager) error {
|
||||
return bm.Delete(s.Bucket, key)
|
||||
}, "删除七牛云文件失败")
|
||||
}
|
||||
|
||||
// Move 七牛服务端改名(同 bucket 内原子操作,不走流量)
|
||||
func (s *QiniuStorage) Move(oldKey, newKey string) error {
|
||||
return s.runBucketManager(func(bm *storage.BucketManager) error {
|
||||
// force=false:目标 key 已存在时直接报错,避免覆盖,保证迁移可重复执行
|
||||
return bm.Move(s.Bucket, oldKey, s.Bucket, newKey, false)
|
||||
}, "移动七牛云文件失败")
|
||||
}
|
||||
@@ -391,11 +346,9 @@ func (s *QiniuStorage) runBucketManager(fn func(*storage.BucketManager) error, e
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetStorageService 根据配置获取存储服务
|
||||
func GetStorageService() (StorageService, error) {
|
||||
cfg, err := models.GetStorageConfig()
|
||||
if err != nil {
|
||||
// 默认使用本地存储
|
||||
return NewLocalStorage(), nil
|
||||
}
|
||||
|
||||
@@ -413,7 +366,6 @@ func GetStorageService() (StorageService, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// mimeTypeOf 从 multipart header 取 Content-Type
|
||||
func mimeTypeOf(header *multipart.FileHeader) string {
|
||||
if header == nil {
|
||||
return ""
|
||||
@@ -421,7 +373,6 @@ func mimeTypeOf(header *multipart.FileHeader) string {
|
||||
return header.Header.Get("Content-Type")
|
||||
}
|
||||
|
||||
// copyFile 文件复制(Rename 跨设备失败时的回退方案)
|
||||
func copyFile(src, dst string) error {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user