RegisterStaticRouter register static api router
(r *gin.RouterGroup)
| 44 | |
| 45 | // RegisterStaticRouter register static api router |
| 46 | func (a *StaticRouter) RegisterStaticRouter(r *gin.RouterGroup) { |
| 47 | r.Static("/uploads/"+constant.AvatarSubPath, filepath.Join(a.serviceConfig.UploadPath, constant.AvatarSubPath)) |
| 48 | r.Static("/uploads/"+constant.AvatarThumbSubPath, filepath.Join(a.serviceConfig.UploadPath, constant.AvatarThumbSubPath)) |
| 49 | r.Static("/uploads/"+constant.PostSubPath, filepath.Join(a.serviceConfig.UploadPath, constant.PostSubPath)) |
| 50 | r.Static("/uploads/"+constant.BrandingSubPath, filepath.Join(a.serviceConfig.UploadPath, constant.BrandingSubPath)) |
| 51 | r.GET("/uploads/"+constant.FilesPostSubPath+"/*filepath", func(c *gin.Context) { |
| 52 | // The filepath such as hash/123.pdf |
| 53 | filePath := c.Param("filepath") |
| 54 | // The original filename is 123.pdf |
| 55 | originalFilename := filepath.Base(filePath) |
| 56 | // The real filename is hash.pdf |
| 57 | realFilename := strings.TrimSuffix(filePath, "/"+originalFilename) + filepath.Ext(originalFilename) |
| 58 | // The file local path is /uploads/files/post/hash.pdf |
| 59 | fileLocalPath := filepath.Join(a.serviceConfig.UploadPath, constant.FilesPostSubPath, realFilename) |
| 60 | // If the file is not exist, return 404 |
| 61 | if !dir.CheckFileExist(fileLocalPath) { |
| 62 | c.Redirect(http.StatusFound, "/404") |
| 63 | return |
| 64 | } |
| 65 | c.FileAttachment(fileLocalPath, originalFilename) |
| 66 | }) |
| 67 | } |
no test coverage detected