作者 yangfu

oss上传直接使用文件名

... ... @@ -22,7 +22,7 @@ type BuckObject struct {
}
// 创建视频上传凭证
func CreateOssUpload(header *protocol.RequestHeader, MultipartForm *multipart.Form) (rsp interface{}, err error) {
func CreateOssUpload(header *protocol.RequestHeader, MultipartForm *multipart.Form, disableRename bool) (rsp interface{}, err error) {
var objects []aliyun.Object
var listPath []BuckObject
var bucket *oss.Bucket
... ... @@ -42,7 +42,7 @@ func CreateOssUpload(header *protocol.RequestHeader, MultipartForm *multipart.Fo
f.Close()
reader := bytes.NewBuffer(data)
fileBase := filepath.Base(v[i].Filename)
key := utils.GetFileName(header.AppProject, aliyun.FileObject, fileBase)
key := utils.GetFileName(header.AppProject, aliyun.FileObject, fileBase, disableRename)
objects = append(objects, aliyun.Object{
Key: key,
Value: reader,
... ...
... ... @@ -19,6 +19,8 @@ type RequestHeader struct {
requestId string //请求编号 md5
reqIndex int64 //请求链序号
KeepFileName bool // 保存文件名 (上传)
}
func (reqHead *RequestHeader) SetRequestId(addString ...string) {
... ...
... ... @@ -36,8 +36,9 @@ func IsFile(path string) bool {
return !IsDir(path)
}
//fileType: video voice image
func GetFileName(projectName, fileType string, filename string) string {
// fileType: video voice image
func GetFileName(projectName, fileType string, filename string, args ...interface{}) string {
var oldFileName = filename
date := comm_time.GetTimeByYyyymmdd()
subfix := path.Ext(filename)
if len(projectName) == 0 {
... ... @@ -45,6 +46,9 @@ func GetFileName(projectName, fileType string, filename string) string {
}
prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32))
filename = fmt.Sprintf("%v%v", prefix, subfix)
if len(args) > 0 && args[0].(bool) {
filename = oldFileName
}
sourcePath := fmt.Sprintf("%v/%v/%v/%v/%v", projectName, beego.BConfig.RunMode, date, fileType, filename)
return sourcePath
}
... ...
... ... @@ -208,7 +208,7 @@ func (this *VodController) PutObject() {
}()
header := controllers.GetRequestHeader(this.Ctx)
msg = domain.NewReturnResponse(oss.CreateOssUpload(header, this.Ctx.Request.MultipartForm))
msg = domain.NewReturnResponse(oss.CreateOssUpload(header, this.Ctx.Request.MultipartForm, false))
}
// STS授权 CreateStsAuth
... ...
... ... @@ -8,6 +8,7 @@ import (
"openapi/pkg/domain"
"openapi/pkg/infrastructure/aliyun"
"openapi/pkg/port/beego/controllers"
"strconv"
)
type VodController struct {
... ... @@ -206,9 +207,14 @@ func (this *VodController) PutObject() {
defer func() {
this.Resp(msg)
}()
var request = struct {
DisableRename bool `json:"disableRename"` // 禁用重命名
}{}
if val := this.Ctx.Input.Query("disableRename"); len(val) > 0 {
request.DisableRename, _ = strconv.ParseBool(val)
}
header := controllers.GetRequestHeader(this.Ctx)
msg = domain.NewReturnResponse(oss.CreateOssUpload(header, this.Ctx.Request.MultipartForm))
msg = domain.NewReturnResponse(oss.CreateOssUpload(header, this.Ctx.Request.MultipartForm, request.DisableRename))
}
// STS授权 CreateStsAuth
... ...
... ... @@ -18,6 +18,7 @@ func init() {
)
beego.SetStaticPath("/log", constant.LogFilePath)
beego.Router("/v2/vod/createStsAuth", &v2.VodController{}, "Post:CreateStsAuth")
beego.Router("/v2/vod/putObject", &v2.VodController{}, "Post:PutObject")
beego.AddNamespace(nsV1)
beego.InsertFilter("/*", beego.BeforeRouter, controllers.AllowCors())
}
... ...