SliverHorn
4 years ago
18 changed files with 312 additions and 175 deletions
-
52server/utils/cmd_Task.go
-
49server/utils/cmd_monitor.go
-
20server/utils/directory.go
-
25server/utils/email.go
-
9server/utils/file_operations.go
-
14server/utils/fmt_plus.go
-
6server/utils/md5.go
-
6server/utils/rotatelogs_unix.go
-
6server/utils/rotatelogs_windows.go
-
24server/utils/server.go
-
24server/utils/upload/local.go
-
28server/utils/upload/qiniu.go
-
12server/utils/upload/upload.go
-
54server/utils/upload_file_local.go
-
67server/utils/upload_remote.go
-
84server/utils/validator.go
-
1server/utils/verify.go
-
6server/utils/zipfiles.go
@ -1,54 +0,0 @@ |
|||
package utils |
|||
|
|||
import ( |
|||
"gin-vue-admin/global" |
|||
"go.uber.org/zap" |
|||
"io" |
|||
"mime/multipart" |
|||
"os" |
|||
"path" |
|||
"strings" |
|||
"time" |
|||
) |
|||
|
|||
func UploadFileLocal(file *multipart.FileHeader) (err error, localPath string, key string) { |
|||
// 读取文件后缀
|
|||
ext := path.Ext(file.Filename) |
|||
// 读取文件名并加密
|
|||
fileName := strings.TrimSuffix(file.Filename, ext) |
|||
fileName = MD5V([]byte(fileName)) |
|||
// 拼接新文件名
|
|||
lastName := fileName + "_" + time.Now().Format("20060102150405") + ext |
|||
// 读取全局变量的定义路径
|
|||
savePath := global.GVA_CONFIG.Local.Path |
|||
// 尝试创建此路径
|
|||
err = os.MkdirAll(savePath, os.ModePerm) |
|||
if err != nil { |
|||
global.GVA_LOG.Error("upload local file fail:", zap.Any("err", err)) |
|||
return err, "", "" |
|||
} |
|||
// 拼接路径和文件名
|
|||
dst := savePath + "/" + lastName |
|||
// 下面为上传逻辑
|
|||
// 打开文件 defer 关闭
|
|||
src, err := file.Open() |
|||
if err != nil { |
|||
global.GVA_LOG.Error("upload local file fail:", zap.Any("err", err)) |
|||
return err, "", "" |
|||
} |
|||
defer src.Close() |
|||
// 创建文件 defer 关闭
|
|||
out, err := os.Create(dst) |
|||
if err != nil { |
|||
global.GVA_LOG.Error("upload local file fail:", zap.Any("err", err)) |
|||
return err, "", "" |
|||
} |
|||
defer out.Close() |
|||
// 传输(拷贝)文件
|
|||
_, err = io.Copy(out, src) |
|||
if err != nil { |
|||
global.GVA_LOG.Error("upload local file fail:", zap.Any("err", err)) |
|||
return err, "", "" |
|||
} |
|||
return nil, dst, lastName |
|||
} |
@ -1,67 +0,0 @@ |
|||
package utils |
|||
|
|||
import ( |
|||
"context" |
|||
"fmt" |
|||
"gin-vue-admin/global" |
|||
"github.com/qiniu/api.v7/v7/auth/qbox" |
|||
"github.com/qiniu/api.v7/v7/storage" |
|||
"go.uber.org/zap" |
|||
"mime/multipart" |
|||
"time" |
|||
) |
|||
|
|||
// 接收两个参数 一个文件流 一个 bucket 你的七牛云标准空间的名字
|
|||
func UploadRemote(file *multipart.FileHeader) (err error, path string, key string) { |
|||
putPolicy := storage.PutPolicy{ |
|||
Scope: global.GVA_CONFIG.Qiniu.Bucket, |
|||
} |
|||
mac := qbox.NewMac(global.GVA_CONFIG.Qiniu.AccessKey, global.GVA_CONFIG.Qiniu.SecretKey) |
|||
upToken := putPolicy.UploadToken(mac) |
|||
cfg := storage.Config{} |
|||
// 空间对应的机房
|
|||
cfg.Zone = &storage.ZoneHuadong |
|||
// 是否使用https域名
|
|||
cfg.UseHTTPS = false |
|||
// 上传是否使用CDN上传加速
|
|||
cfg.UseCdnDomains = false |
|||
formUploader := storage.NewFormUploader(&cfg) |
|||
ret := storage.PutRet{} |
|||
putExtra := storage.PutExtra{ |
|||
Params: map[string]string{ |
|||
"x:name": "github logo", |
|||
}, |
|||
} |
|||
f, e := file.Open() |
|||
if e != nil { |
|||
fmt.Println(e) |
|||
return e, "", "" |
|||
} |
|||
dataLen := file.Size |
|||
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性
|
|||
err = formUploader.Put(context.Background(), &ret, upToken, fileKey, f, dataLen, &putExtra) |
|||
if err != nil { |
|||
global.GVA_LOG.Error("upload file fail:", zap.Any("err", err)) |
|||
return err, "", "" |
|||
} |
|||
return err, global.GVA_CONFIG.Qiniu.ImgPath + "/" + ret.Key, ret.Key |
|||
} |
|||
|
|||
func DeleteFile(key string) error { |
|||
|
|||
mac := qbox.NewMac(global.GVA_CONFIG.Qiniu.AccessKey, global.GVA_CONFIG.Qiniu.SecretKey) |
|||
cfg := storage.Config{ |
|||
// 是否使用https域名进行资源管理
|
|||
UseHTTPS: false, |
|||
} |
|||
// 指定空间所在的区域,如果不指定将自动探测
|
|||
// 如果没有特殊需求,默认不需要指定
|
|||
// cfg.Zone=&storage.ZoneHuabei
|
|||
bucketManager := storage.NewBucketManager(mac, &cfg) |
|||
err := bucketManager.Delete(global.GVA_CONFIG.Qiniu.Bucket, key) |
|||
if err != nil { |
|||
fmt.Println(err) |
|||
return err |
|||
} |
|||
return nil |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue