作者 yangfu

upload review

... ... @@ -22,7 +22,7 @@ func(this *UploadController)Image(){
defer func(){
this.Resp(msg)
}()
var request = &protocol.ImageRequest{}
var request = &protocol.FileRequest{}
//if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
// log.Error(err)
// msg = mybeego.NewMessage(1)
... ... @@ -38,3 +38,29 @@ func(this *UploadController)Image(){
}
msg = this.GenMessage(upload.Image(request))
}
//Voice
func(this *UploadController)Voice(){
var (
msg *mybeego.Message
err error
)
defer func(){
this.Resp(msg)
}()
var request = &protocol.FileRequest{}
//if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
// log.Error(err)
// msg = mybeego.NewMessage(1)
// return
//}
//if b,m :=this.Valid(request);!b{
// msg = m
// return
//}
if request.Files,err =this.GetFiles("file");err!=nil{
log.Error(err)
return
}
msg = this.GenMessage(upload.Voice(request))
}
\ No newline at end of file
... ...
... ... @@ -2,11 +2,17 @@ package protocol
import "mime/multipart"
const(
FileImage ="image"
FileVoice ="voice"
)
/*Image */
type ImageRequest struct {
type FileRequest struct {
//Xxx string`json:"xxx" valid:"Required"`
Files []*multipart.FileHeader
FileType string
}
type ImageResponse struct {
type FileResponse struct {
Paths []string `json:"paths"`
}
\ No newline at end of file
... ...
... ... @@ -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{
... ...
... ... @@ -15,7 +15,7 @@ func init(){
func Test_Image(t *testing.T){
input := &protocol.ImageRequest{}
input := &protocol.FileRequest{}
var bufReader bytes.Buffer
mpWriter:=multipart.NewWriter(&bufReader)
... ...