|
@@ -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{
|