...
|
...
|
@@ -18,15 +18,43 @@ import ( |
|
|
)
|
|
|
|
|
|
//上传图片
|
|
|
func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error){
|
|
|
func Image(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){
|
|
|
var (
|
|
|
|
|
|
)
|
|
|
for i:=range request.Files{
|
|
|
f :=request.Files[i]
|
|
|
subfix:=path.Ext(f.Filename)
|
|
|
//文件格式不符合
|
|
|
if !(subfix==".jpg" || subfix==".gif" || subfix==".png"){
|
|
|
err = common.NewErrorWithMsg(2,"file format error")
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
request.FileType = protocol.FileImage
|
|
|
return UploadFile(request)
|
|
|
}
|
|
|
|
|
|
func Voice(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){
|
|
|
var (
|
|
|
|
|
|
)
|
|
|
request.FileType = protocol.FileVoice
|
|
|
return UploadFile(request)
|
|
|
}
|
|
|
|
|
|
func UploadFile(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){
|
|
|
if request.FileType==""{
|
|
|
request.FileType =protocol.FileImage
|
|
|
}
|
|
|
var (
|
|
|
src multipart.File
|
|
|
dst *os.File
|
|
|
virtualPath string = beego.AppConfig.String("source_virtual_path") //虚拟路径
|
|
|
sourcePath string = filepath.Join(beego.AppConfig.String("source_path"),"image") //真实路径
|
|
|
sourcePath string = filepath.Join(beego.AppConfig.String("source_path"),request.FileType) //真实路径
|
|
|
date,filename string
|
|
|
)
|
|
|
rsp =&protocol.ImageResponse{}
|
|
|
rsp =&protocol.FileResponse{}
|
|
|
date =comm_time.GetTimeByYyyymmdd()
|
|
|
sourcePath = filepath.Join(sourcePath,date)
|
|
|
if _,err=os.Stat(sourcePath);err!=nil{
|
...
|
...
|
@@ -36,15 +64,10 @@ func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error |
|
|
return
|
|
|
}
|
|
|
}
|
|
|
virtualPath=beego.AppConfig.String("source_host")+filepath.Join(virtualPath,"image",date)
|
|
|
virtualPath=beego.AppConfig.String("source_host")+filepath.Join(virtualPath,request.FileType,date)
|
|
|
for i:=range request.Files{
|
|
|
f :=request.Files[i]
|
|
|
subfix:=path.Ext(f.Filename)
|
|
|
//文件格式不符合
|
|
|
if !(subfix==".jpg" || subfix==".gif" || subfix==".png"){
|
|
|
err = common.NewErrorWithMsg(2,"file format error")
|
|
|
return
|
|
|
}
|
|
|
filename =fmt.Sprintf("%v_%v%v",time.Now().Unix(),common.RandomString(32),subfix)
|
|
|
src,err=f.Open()
|
|
|
if err!=nil{
|
...
|
...
|
|