正在显示
5 个修改的文件
包含
80 行增加
和
2 行删除
controllers/v1/file.go
0 → 100644
1 | +package v1 | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "github.com/astaxie/beego" | ||
6 | + "opp/controllers" | ||
7 | + "opp/internal/utils" | ||
8 | + "opp/protocol" | ||
9 | + "path/filepath" | ||
10 | + "strings" | ||
11 | +) | ||
12 | + | ||
13 | +type FileController struct { | ||
14 | + controllers.BaseController | ||
15 | +} | ||
16 | + | ||
17 | +// DownLoad | ||
18 | +// @router /opp/file [post] | ||
19 | +func (this *FileController) DownLoad() { | ||
20 | + var ( | ||
21 | + msg *protocol.ResponseMessage | ||
22 | + err error | ||
23 | + rsp *protocol.FileResponse = &protocol.FileResponse{} | ||
24 | + ) | ||
25 | + defer func() { | ||
26 | + if msg.Errno != 0 { | ||
27 | + this.Resp(msg) | ||
28 | + } | ||
29 | + }() | ||
30 | + fileUrl := this.Ctx.Request.RequestURI | ||
31 | + filePath := strings.Replace(fileUrl, "/file/opp", "", 1) | ||
32 | + filePath = filepath.Join(beego.AppConfig.String("source_path"), filePath) | ||
33 | + if utils.Exists(filePath) { | ||
34 | + this.Ctx.Output.Download(filePath) | ||
35 | + } else { | ||
36 | + err = protocol.NewCustomMessage(0, fmt.Sprintf("文件不存在:%v", filePath)) | ||
37 | + } | ||
38 | + msg = protocol.NewReturnResponse(rsp, err) | ||
39 | +} |
internal/utils/file.go
0 → 100644
1 | +package utils | ||
2 | + | ||
3 | +import "os" | ||
4 | + | ||
5 | +// 判断所给路径文件/文件夹是否存在 | ||
6 | +func Exists(path string) bool { | ||
7 | + _, err := os.Stat(path) //os.Stat获取文件信息 | ||
8 | + if err != nil { | ||
9 | + if os.IsExist(err) { | ||
10 | + return true | ||
11 | + } | ||
12 | + return false | ||
13 | + } | ||
14 | + return true | ||
15 | +} | ||
16 | + | ||
17 | +// 判断所给路径是否为文件夹 | ||
18 | +func IsDir(path string) bool { | ||
19 | + s, err := os.Stat(path) | ||
20 | + if err != nil { | ||
21 | + return false | ||
22 | + } | ||
23 | + return s.IsDir() | ||
24 | +} | ||
25 | + | ||
26 | +// 判断所给路径是否为文件 | ||
27 | +func IsFile(path string) bool { | ||
28 | + return !IsDir(path) | ||
29 | +} |
@@ -287,6 +287,14 @@ func init() { | @@ -287,6 +287,14 @@ func init() { | ||
287 | MethodParams: param.Make(), | 287 | MethodParams: param.Make(), |
288 | Params: nil}) | 288 | Params: nil}) |
289 | 289 | ||
290 | + beego.GlobalControllerRouter["opp/controllers/v1:FileController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:FileController"], | ||
291 | + beego.ControllerComments{ | ||
292 | + Method: "DownLoad", | ||
293 | + Router: `/opp/file`, | ||
294 | + AllowHTTPMethods: []string{"post"}, | ||
295 | + MethodParams: param.Make(), | ||
296 | + Params: nil}) | ||
297 | + | ||
290 | beego.GlobalControllerRouter["opp/controllers/v1:MessageController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:MessageController"], | 298 | beego.GlobalControllerRouter["opp/controllers/v1:MessageController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:MessageController"], |
291 | beego.ControllerComments{ | 299 | beego.ControllerComments{ |
292 | Method: "AnnouncementRead", | 300 | Method: "AnnouncementRead", |
@@ -33,7 +33,9 @@ func init() { | @@ -33,7 +33,9 @@ func init() { | ||
33 | 33 | ||
34 | nsH5 := beego.NewNamespace("h5", beego.NSBefore(controllers.LogRequestData), beego.NSBefore(controllers.AllowOption), beego.NSInclude(&controllers.H5Controller{})) | 34 | nsH5 := beego.NewNamespace("h5", beego.NSBefore(controllers.LogRequestData), beego.NSBefore(controllers.AllowOption), beego.NSInclude(&controllers.H5Controller{})) |
35 | beego.AddNamespace(nsH5) | 35 | beego.AddNamespace(nsH5) |
36 | - | 36 | + //post 下载文件 |
37 | + beego.Router("/file/opp/*", &v1.FileController{}, "post:DownLoad") | ||
38 | + //get 直接获取文件 | ||
37 | beego.SetStaticPath("/file/opp", beego.AppConfig.String("source_path")) | 39 | beego.SetStaticPath("/file/opp", beego.AppConfig.String("source_path")) |
38 | beego.SetStaticPath("/log", beego.AppConfig.String("aliyun_logs_access")) | 40 | beego.SetStaticPath("/log", beego.AppConfig.String("aliyun_logs_access")) |
39 | beego.Handler("/metrics", promhttp.Handler()) | 41 | beego.Handler("/metrics", promhttp.Handler()) |
@@ -67,7 +67,7 @@ func UploadFile(request *protocol.FileRequest) (rsp *protocol.FileResponse, err | @@ -67,7 +67,7 @@ func UploadFile(request *protocol.FileRequest) (rsp *protocol.FileResponse, err | ||
67 | return | 67 | return |
68 | } | 68 | } |
69 | } | 69 | } |
70 | - virtualPath = beego.AppConfig.String("source_host") + filepath.Join(virtualPath, request.FileType, date) | 70 | + virtualPath = fmt.Sprintf("%v%v/%v/%v", beego.AppConfig.String("source_host"), virtualPath, request.FileType, date) |
71 | for i := range request.Files { | 71 | for i := range request.Files { |
72 | f := request.Files[i] | 72 | f := request.Files[i] |
73 | prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32)) | 73 | prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32)) |
-
请 注册 或 登录 后发表评论