From 0d7a291b594ced6d2e5c40eae1689ffa87546c7b Mon Sep 17 00:00:00 2001 From: Granty1 Date: Sat, 25 Apr 2020 10:14:17 +0800 Subject: [PATCH] Fixed callback bug in front end file upload & move some constants to config.yaml --- server/api/v1/exa_file_upload_download.go | 5 +- server/api/v1/sys_user.go | 7 +- server/config.yaml | 5 +- server/config/config.go | 2 + server/utils/upload.go | 13 +- web/src/view/example/upload/upload.vue | 234 ++++++++++++---------- 6 files changed, 143 insertions(+), 123 deletions(-) diff --git a/server/api/v1/exa_file_upload_download.go b/server/api/v1/exa_file_upload_download.go index 2aeb468a..56ab422d 100644 --- a/server/api/v1/exa_file_upload_download.go +++ b/server/api/v1/exa_file_upload_download.go @@ -27,7 +27,7 @@ func UploadFile(c *gin.Context) { response.FailWithMessage(fmt.Sprintf("上传文件失败,%v", err), c) } else { //文件上传后拿到文件路径 - err, filePath, key := utils.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH) + err, filePath, key := utils.Upload(header) if err != nil { response.FailWithMessage(fmt.Sprintf("接收返回值失败,%v", err), c) } else { @@ -45,7 +45,6 @@ func UploadFile(c *gin.Context) { response.FailWithMessage(fmt.Sprintf("修改数据库链接失败,%v", err), c) } else { response.OkDetailed(resp.ExaFileResponse{File: file}, "上传成功", c) - } } } @@ -65,7 +64,7 @@ func DeleteFile(c *gin.Context) { if err != nil { response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c) } else { - err = utils.DeleteFile(USER_HEADER_BUCKET, f.Key) + err = utils.DeleteFile(f.Key) if err != nil { response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c) diff --git a/server/api/v1/sys_user.go b/server/api/v1/sys_user.go index 146a5291..90b1db15 100644 --- a/server/api/v1/sys_user.go +++ b/server/api/v1/sys_user.go @@ -18,11 +18,6 @@ import ( "time" ) -const ( - USER_HEADER_IMG_PATH string = "http://qmplusimg.henrongyi.top" - USER_HEADER_BUCKET string = "qm-plus-img" -) - // @Tags Base // @Summary 用户注册账号 // @Produce application/json @@ -172,7 +167,7 @@ func UploadHeaderImg(c *gin.Context) { response.FailWithMessage(fmt.Sprintf("上传文件失败,%v", err), c) } else { //文件上传后拿到文件路径 - err, filePath, _ := utils.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH) + err, filePath, _ := utils.Upload(header) if err != nil { response.FailWithMessage(fmt.Sprintf("接收返回值失败,%v", err), c) } else { diff --git a/server/config.yaml b/server/config.yaml index 51267d7a..7d462371 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -18,16 +18,19 @@ mysql: max-idle-conns: 10 max-open-conns: 10 log-mode: true + #sqlite 配置 sqlite: path: db.db log-mode: true config: 'loc=Asia/Shanghai' + # oss configuration qiniu: access-key: '25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ' secret-key: 'pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY' - + bucket: 'qm-plus-img' + img-path: 'http://qmplusimg.henrongyi.top' # redis configuration redis: addr: '127.0.0.1:6379' diff --git a/server/config/config.go b/server/config/config.go index 47bffe9f..53d1ca5e 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -46,6 +46,8 @@ type Redis struct { type Qiniu struct { AccessKey string `mapstructure:"access-key" json:"accessKey" yaml:"access-key"` SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` + Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` + ImgPath string `mapstructure:"img-path" json:"imgPath" yaml:"img-path"` } type Captcha struct { diff --git a/server/utils/upload.go b/server/utils/upload.go index ee182c2a..f528a787 100644 --- a/server/utils/upload.go +++ b/server/utils/upload.go @@ -14,9 +14,9 @@ var accessKey string = global.GVA_CONFIG.Qiniu.AccessKey // 你在七牛云的ac var secretKey string = global.GVA_CONFIG.Qiniu.SecretKey // 你在七牛云的secretKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西 // 接收两个参数 一个文件流 一个 bucket 你的七牛云标准空间的名字 -func Upload(file *multipart.FileHeader, bucket string, urlPath string) (err error, path string, key string) { +func Upload(file *multipart.FileHeader) (err error, path string, key string) { putPolicy := storage.PutPolicy{ - Scope: bucket, + Scope: global.GVA_CONFIG.Qiniu.Bucket, } mac := qbox.NewMac(accessKey, secretKey) upToken := putPolicy.UploadToken(mac) @@ -43,14 +43,13 @@ func Upload(file *multipart.FileHeader, bucket string, urlPath string) (err erro fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性 err = formUploader.Put(context.Background(), &ret, upToken, fileKey, f, dataLen, &putExtra) if err != nil { - fmt.Println(err) - //qmlog.QMLog.Info(err) + global.GVA_LOG.Error("upload file fail:", err) return err, "", "" } - return err, urlPath + "/" + ret.Key, ret.Key + return err, global.GVA_CONFIG.Qiniu.ImgPath + "/" + ret.Key, ret.Key } -func DeleteFile(bucket string, key string) error { +func DeleteFile(key string) error { mac := qbox.NewMac(accessKey, secretKey) cfg := storage.Config{ @@ -61,7 +60,7 @@ func DeleteFile(bucket string, key string) error { // 如果没有特殊需求,默认不需要指定 //cfg.Zone=&storage.ZoneHuabei bucketManager := storage.NewBucketManager(mac, &cfg) - err := bucketManager.Delete(bucket, key) + err := bucketManager.Delete(global.GVA_CONFIG.Qiniu.Bucket, key) if err != nil { fmt.Println(err) return err diff --git a/web/src/view/example/upload/upload.vue b/web/src/view/example/upload/upload.vue index f1d96e28..f05ae8f8 100644 --- a/web/src/view/example/upload/upload.vue +++ b/web/src/view/example/upload/upload.vue @@ -1,153 +1,175 @@ - + \ No newline at end of file +}; +