filesystem

This commit is contained in:
xing 2021-05-31 11:17:33 +08:00
parent fa5ba479db
commit 882149eee5
3 changed files with 22 additions and 0 deletions

1
.gitignore vendored
View File

@ -14,6 +14,7 @@
.idea .idea
storage/* storage/*
blog.iml blog.iml
go.sum
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/

View File

@ -8,6 +8,9 @@ import (
"blog/pkg/errorcode" "blog/pkg/errorcode"
"blog/pkg/upload" "blog/pkg/upload"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"os"
"path"
"strings"
) )
type Upload struct{} type Upload struct{}
@ -16,6 +19,20 @@ func NewUpload() Upload {
return Upload{} return Upload{}
} }
func ReadFile(c *gin.Context) {
fileName := strings.TrimLeft(c.Request.RequestURI, "/")
_, fileName = path.Split(fileName)
fileName = path.Join(global.AppSetting.UploadSavePath, fileName)
_, err := os.Stat(fileName)
if os.IsNotExist(err) {
if err != nil {
app.NewResponse(c).ToErrorResponse(errorcode.NotFound.WithDetails(err.Error()))
return
}
}
c.File(fileName)
}
// @Summary 上传图片 // @Summary 上传图片
// @Produce json // @Produce json
// @Param type formData int true "文件类型1图片" required Enums(1,2,3) // @Param type formData int true "文件类型1图片" required Enums(1,2,3)

View File

@ -2,12 +2,14 @@ package routess
import ( import (
_ "blog/docs" _ "blog/docs"
"blog/global"
"blog/internal/middleware" "blog/internal/middleware"
"blog/internal/routess/api" "blog/internal/routess/api"
v1 "blog/internal/routess/api/v1" v1 "blog/internal/routess/api/v1"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files" swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger" ginSwagger "github.com/swaggo/gin-swagger"
"net/http"
) )
func NewRouter() *gin.Engine { func NewRouter() *gin.Engine {
@ -19,6 +21,8 @@ func NewRouter() *gin.Engine {
tag := v1.NewTag() tag := v1.NewTag()
upload := api.NewUpload() upload := api.NewUpload()
r.POST("/upload/file", upload.UploadFile) r.POST("/upload/file", upload.UploadFile)
r.StaticFS("/static", http.Dir(global.AppSetting.UploadSavePath))
//r.GET("/static/*any",api.ReadFile)
r.GET("/doc/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) r.GET("/doc/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
apiv1 := r.Group("/api/v1") apiv1 := r.Group("/api/v1")
{ {