可切换本地与七牛云上传,增加头像与图片组建
This commit is contained in:
@@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/global/response"
|
||||
"gin-vue-admin/model"
|
||||
"gin-vue-admin/model/request"
|
||||
@@ -27,8 +28,17 @@ func UploadFile(c *gin.Context) {
|
||||
response.FailWithMessage(fmt.Sprintf("上传文件失败,%v", err), c)
|
||||
} else {
|
||||
// 文件上传后拿到文件路径
|
||||
err, filePath, key := utils.Upload(header)
|
||||
if err != nil {
|
||||
var uploadErr error
|
||||
var filePath string
|
||||
var key string
|
||||
if global.GVA_CONFIG.LocalUpload.Local {
|
||||
// 本地上传
|
||||
uploadErr, filePath, key = utils.UploadFileLocal(header)
|
||||
} else {
|
||||
// 七牛云上传
|
||||
uploadErr, filePath, key = utils.UploadRemote(header)
|
||||
}
|
||||
if uploadErr != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("接收返回值失败,%v", err), c)
|
||||
} else {
|
||||
// 修改数据库后得到修改后的user并且返回供前端使用
|
||||
|
@@ -195,8 +195,16 @@ func UploadHeaderImg(c *gin.Context) {
|
||||
response.FailWithMessage(fmt.Sprintf("上传文件失败,%v", err), c)
|
||||
} else {
|
||||
// 文件上传后拿到文件路径
|
||||
err, filePath, _ := utils.Upload(header)
|
||||
if err != nil {
|
||||
var uploadErr error
|
||||
var filePath string
|
||||
if global.GVA_CONFIG.LocalUpload.Local {
|
||||
// 本地上传
|
||||
uploadErr, filePath, _ = utils.UploadAvatarLocal(header)
|
||||
} else {
|
||||
// 七牛云上传
|
||||
uploadErr, filePath, _ = utils.UploadRemote(header)
|
||||
}
|
||||
if uploadErr != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("接收返回值失败,%v", err), c)
|
||||
} else {
|
||||
// 修改数据库后得到修改后的user并且返回供前端使用
|
||||
|
@@ -1,61 +1,46 @@
|
||||
# Gin-Vue-Admin Global Configuration
|
||||
|
||||
# casbin configuration
|
||||
casbin:
|
||||
model-path: './resource/rbac_model.conf'
|
||||
|
||||
# jwt configuration
|
||||
jwt:
|
||||
signing-key: 'qmPlus'
|
||||
|
||||
# mysql connect configuration
|
||||
mysql:
|
||||
username: root
|
||||
password: 'Aa@6447985'
|
||||
path: '127.0.0.1:3306'
|
||||
db-name: 'qmPlus'
|
||||
config: 'charset=utf8mb4&parseTime=True&loc=Local'
|
||||
max-idle-conns: 10
|
||||
max-open-conns: 10
|
||||
log-mode: false
|
||||
|
||||
#sqlite 配置
|
||||
sqlite:
|
||||
path: db.db
|
||||
log-mode: true
|
||||
config: 'loc=Asia/Shanghai'
|
||||
|
||||
# oss configuration
|
||||
|
||||
# 请自行七牛申请对应的 公钥 私钥 bucket 和 域名地址
|
||||
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'
|
||||
password: ''
|
||||
db: 0
|
||||
|
||||
# system configuration
|
||||
system:
|
||||
use-multipoint: false
|
||||
env: 'public' # Change to "develop" to skip authentication for development mode
|
||||
addr: 8888
|
||||
db-type: "mysql" # support mysql/sqlite
|
||||
|
||||
# captcha configuration
|
||||
captcha:
|
||||
key-long: 6
|
||||
img-width: 240
|
||||
img-height: 80
|
||||
|
||||
# logger configuration
|
||||
key-long: 6
|
||||
img-width: 240
|
||||
img-height: 80
|
||||
casbin:
|
||||
model-path: ./resource/rbac_model.conf
|
||||
jwt:
|
||||
signing-key: qmPlus
|
||||
localupload:
|
||||
local: true
|
||||
avatar-path: uploads/avatar
|
||||
file-path: uploads/file
|
||||
log:
|
||||
prefix: '[GIN-VUE-ADMIN]'
|
||||
log-file: true
|
||||
stdout: 'DEBUG'
|
||||
file: 'DEBUG'
|
||||
prefix: '[GIN-VUE-ADMIN]'
|
||||
log-file: true
|
||||
stdout: DEBUG
|
||||
file: DEBUG
|
||||
mysql:
|
||||
username: root
|
||||
password: Aa@6447985
|
||||
path: 127.0.0.1:3306
|
||||
db-name: qmPlus
|
||||
config: charset=utf8mb4&parseTime=True&loc=Local
|
||||
max-idle-conns: 10
|
||||
max-open-conns: 10
|
||||
log-mode: false
|
||||
qiniu:
|
||||
access-key: 25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ
|
||||
secret-key: pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY
|
||||
bucket: qm-plus-img
|
||||
img-path: http://qmplusimg.henrongyi.top
|
||||
redis:
|
||||
addr: 127.0.0.1:6379
|
||||
password: ""
|
||||
db: 0
|
||||
sqlite:
|
||||
username: ""
|
||||
password: ""
|
||||
path: db.db
|
||||
config: loc=Asia/Shanghai
|
||||
log-mode: true
|
||||
system:
|
||||
use-multipoint: false
|
||||
env: public
|
||||
addr: 8888
|
||||
db-type: mysql
|
||||
|
@@ -10,6 +10,7 @@ type Server struct {
|
||||
JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
|
||||
Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`
|
||||
Log Log `mapstructure:"log" json:"log" yaml:"log"`
|
||||
LocalUpload LocalUpload `mapstructure:"localUpload" json:"localUpload" yaml:"localUpload"`
|
||||
}
|
||||
|
||||
type System struct {
|
||||
@@ -43,6 +44,13 @@ type Redis struct {
|
||||
Password string `mapstructure:"password" json:"password" yaml:"password"`
|
||||
DB int `mapstructure:"db" json:"db" yaml:"db"`
|
||||
}
|
||||
|
||||
type LocalUpload struct {
|
||||
Local bool `mapstructure:"local" json:"local" yaml:"local"`
|
||||
AvatarPath string `mapstructure:"avatar-path" json:"avatarPath" yaml:"avatar-path"`
|
||||
FilePath string `mapstructure:"file-path" json:"filePath" yaml:"file-path"`
|
||||
}
|
||||
|
||||
type Qiniu struct {
|
||||
AccessKey string `mapstructure:"access-key" json:"accessKey" yaml:"access-key"`
|
||||
SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"`
|
||||
|
@@ -8,12 +8,16 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/swaggo/gin-swagger"
|
||||
"github.com/swaggo/gin-swagger/swaggerFiles"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// 初始化总路由
|
||||
|
||||
func Routers() *gin.Engine {
|
||||
var Router = gin.Default()
|
||||
// 为用户头像和文件提供静态地址
|
||||
Router.StaticFS(global.GVA_CONFIG.LocalUpload.AvatarPath, http.Dir(global.GVA_CONFIG.LocalUpload.AvatarPath))
|
||||
Router.StaticFS(global.GVA_CONFIG.LocalUpload.FilePath, http.Dir(global.GVA_CONFIG.LocalUpload.FilePath))
|
||||
// Router.Use(middleware.LoadTls()) // 打开就能玩https了
|
||||
global.GVA_LOG.Debug("use middleware logger")
|
||||
// 跨域
|
||||
|
53
server/utils/upload_avatar_local.go
Normal file
53
server/utils/upload_avatar_local.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UploadAvatarLocal(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.LocalUpload.AvatarPath
|
||||
// 尝试创建此路径
|
||||
err = os.MkdirAll(savePath, os.ModePerm)
|
||||
if err != nil{
|
||||
global.GVA_LOG.Error("upload local file fail:", err)
|
||||
return err, "", ""
|
||||
}
|
||||
// 拼接路径和文件名
|
||||
dst := savePath + "/" + lastName
|
||||
// 下面为上传逻辑
|
||||
// 打开文件 defer 关闭
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("upload local file fail:", err)
|
||||
return err, "", ""
|
||||
}
|
||||
defer src.Close()
|
||||
// 创建文件 defer 关闭
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("upload local file fail:", err)
|
||||
return err, "", ""
|
||||
}
|
||||
defer out.Close()
|
||||
// 传输(拷贝)文件
|
||||
_, err = io.Copy(out, src)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("upload local file fail:", err)
|
||||
return err, "", ""
|
||||
}
|
||||
return nil, dst, lastName
|
||||
}
|
53
server/utils/upload_file_local.go
Normal file
53
server/utils/upload_file_local.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
"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.LocalUpload.FilePath
|
||||
// 尝试创建此路径
|
||||
err = os.MkdirAll(savePath, os.ModePerm)
|
||||
if err != nil{
|
||||
global.GVA_LOG.Error("upload local file fail:", err)
|
||||
return err, "", ""
|
||||
}
|
||||
// 拼接路径和文件名
|
||||
dst := savePath + "/" + lastName
|
||||
// 下面为上传逻辑
|
||||
// 打开文件 defer 关闭
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("upload local file fail:", err)
|
||||
return err, "", ""
|
||||
}
|
||||
defer src.Close()
|
||||
// 创建文件 defer 关闭
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("upload local file fail:", err)
|
||||
return err, "", ""
|
||||
}
|
||||
defer out.Close()
|
||||
// 传输(拷贝)文件
|
||||
_, err = io.Copy(out, src)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("upload local file fail:", err)
|
||||
return err, "", ""
|
||||
}
|
||||
return nil, dst, lastName
|
||||
}
|
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// 接收两个参数 一个文件流 一个 bucket 你的七牛云标准空间的名字
|
||||
func Upload(file *multipart.FileHeader) (err error, path string, key string) {
|
||||
func UploadRemote(file *multipart.FileHeader) (err error, path string, key string) {
|
||||
putPolicy := storage.PutPolicy{
|
||||
Scope: global.GVA_CONFIG.Qiniu.Bucket,
|
||||
}
|
Reference in New Issue
Block a user