作者 yangfu

upload review

@@ -22,7 +22,7 @@ func(this *UploadController)Image(){ @@ -22,7 +22,7 @@ func(this *UploadController)Image(){
22 defer func(){ 22 defer func(){
23 this.Resp(msg) 23 this.Resp(msg)
24 }() 24 }()
25 - var request = &protocol.ImageRequest{} 25 + var request = &protocol.FileRequest{}
26 //if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ 26 //if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
27 // log.Error(err) 27 // log.Error(err)
28 // msg = mybeego.NewMessage(1) 28 // msg = mybeego.NewMessage(1)
@@ -38,3 +38,29 @@ func(this *UploadController)Image(){ @@ -38,3 +38,29 @@ func(this *UploadController)Image(){
38 } 38 }
39 msg = this.GenMessage(upload.Image(request)) 39 msg = this.GenMessage(upload.Image(request))
40 } 40 }
  41 +
  42 +//Voice
  43 +func(this *UploadController)Voice(){
  44 + var (
  45 + msg *mybeego.Message
  46 + err error
  47 + )
  48 + defer func(){
  49 + this.Resp(msg)
  50 + }()
  51 + var request = &protocol.FileRequest{}
  52 + //if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
  53 + // log.Error(err)
  54 + // msg = mybeego.NewMessage(1)
  55 + // return
  56 + //}
  57 + //if b,m :=this.Valid(request);!b{
  58 + // msg = m
  59 + // return
  60 + //}
  61 + if request.Files,err =this.GetFiles("file");err!=nil{
  62 + log.Error(err)
  63 + return
  64 + }
  65 + msg = this.GenMessage(upload.Voice(request))
  66 +}
@@ -2,11 +2,17 @@ package protocol @@ -2,11 +2,17 @@ package protocol
2 2
3 import "mime/multipart" 3 import "mime/multipart"
4 4
  5 +const(
  6 + FileImage ="image"
  7 + FileVoice ="voice"
  8 +)
  9 +
5 /*Image */ 10 /*Image */
6 -type ImageRequest struct { 11 +type FileRequest struct {
7 //Xxx string`json:"xxx" valid:"Required"` 12 //Xxx string`json:"xxx" valid:"Required"`
8 Files []*multipart.FileHeader 13 Files []*multipart.FileHeader
  14 + FileType string
9 } 15 }
10 -type ImageResponse struct { 16 +type FileResponse struct {
11 Paths []string `json:"paths"` 17 Paths []string `json:"paths"`
12 } 18 }
@@ -18,15 +18,43 @@ import ( @@ -18,15 +18,43 @@ import (
18 ) 18 )
19 19
20 //上传图片 20 //上传图片
21 -func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error){ 21 +func Image(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){
  22 + var (
  23 +
  24 + )
  25 + for i:=range request.Files{
  26 + f :=request.Files[i]
  27 + subfix:=path.Ext(f.Filename)
  28 + //文件格式不符合
  29 + if !(subfix==".jpg" || subfix==".gif" || subfix==".png"){
  30 + err = common.NewErrorWithMsg(2,"file format error")
  31 + return
  32 + }
  33 + }
  34 + request.FileType = protocol.FileImage
  35 + return UploadFile(request)
  36 +}
  37 +
  38 +func Voice(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){
  39 + var (
  40 +
  41 + )
  42 + request.FileType = protocol.FileVoice
  43 + return UploadFile(request)
  44 +}
  45 +
  46 +func UploadFile(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){
  47 + if request.FileType==""{
  48 + request.FileType =protocol.FileImage
  49 + }
22 var ( 50 var (
23 src multipart.File 51 src multipart.File
24 dst *os.File 52 dst *os.File
25 virtualPath string = beego.AppConfig.String("source_virtual_path") //虚拟路径 53 virtualPath string = beego.AppConfig.String("source_virtual_path") //虚拟路径
26 - sourcePath string = filepath.Join(beego.AppConfig.String("source_path"),"image") //真实路径 54 + sourcePath string = filepath.Join(beego.AppConfig.String("source_path"),request.FileType) //真实路径
27 date,filename string 55 date,filename string
28 ) 56 )
29 - rsp =&protocol.ImageResponse{} 57 + rsp =&protocol.FileResponse{}
30 date =comm_time.GetTimeByYyyymmdd() 58 date =comm_time.GetTimeByYyyymmdd()
31 sourcePath = filepath.Join(sourcePath,date) 59 sourcePath = filepath.Join(sourcePath,date)
32 if _,err=os.Stat(sourcePath);err!=nil{ 60 if _,err=os.Stat(sourcePath);err!=nil{
@@ -36,15 +64,10 @@ func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error @@ -36,15 +64,10 @@ func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error
36 return 64 return
37 } 65 }
38 } 66 }
39 - virtualPath=beego.AppConfig.String("source_host")+filepath.Join(virtualPath,"image",date) 67 + virtualPath=beego.AppConfig.String("source_host")+filepath.Join(virtualPath,request.FileType,date)
40 for i:=range request.Files{ 68 for i:=range request.Files{
41 f :=request.Files[i] 69 f :=request.Files[i]
42 subfix:=path.Ext(f.Filename) 70 subfix:=path.Ext(f.Filename)
43 - //文件格式不符合  
44 - if !(subfix==".jpg" || subfix==".gif" || subfix==".png"){  
45 - err = common.NewErrorWithMsg(2,"file format error")  
46 - return  
47 - }  
48 filename =fmt.Sprintf("%v_%v%v",time.Now().Unix(),common.RandomString(32),subfix) 71 filename =fmt.Sprintf("%v_%v%v",time.Now().Unix(),common.RandomString(32),subfix)
49 src,err=f.Open() 72 src,err=f.Open()
50 if err!=nil{ 73 if err!=nil{
@@ -15,7 +15,7 @@ func init(){ @@ -15,7 +15,7 @@ func init(){
15 15
16 16
17 func Test_Image(t *testing.T){ 17 func Test_Image(t *testing.T){
18 - input := &protocol.ImageRequest{} 18 + input := &protocol.FileRequest{}
19 19
20 var bufReader bytes.Buffer 20 var bufReader bytes.Buffer
21 mpWriter:=multipart.NewWriter(&bufReader) 21 mpWriter:=multipart.NewWriter(&bufReader)