mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
【添加】图片上传
This commit is contained in:
48
apps/system/api/upload.go
Normal file
48
apps/system/api/upload.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
filek "github.com/XM-GO/PandaKit/file"
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
"github.com/kakuilan/kgo"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UploadApi struct{}
|
||||
|
||||
const filePath = "uploads/file"
|
||||
|
||||
// UploadImage 图片上传
|
||||
func (up *UploadApi) UploadImage(rc *restfulx.ReqCtx) {
|
||||
_, fileHeader, err := rc.Request.Request.FormFile("imagefile")
|
||||
biz.ErrIsNil(err, "请传入文件")
|
||||
ext := path.Ext(fileHeader.Filename)
|
||||
// 读取文件名并加密
|
||||
name := strings.TrimSuffix(fileHeader.Filename, ext)
|
||||
name = kgo.KStr.Md5(name, 32)
|
||||
// 拼接新文件名
|
||||
filename := name + "_" + time.Now().Format("20060102150405") + ext
|
||||
filek.SaveUploadedFile(fileHeader, fmt.Sprintf("%s/%s", filePath, filename))
|
||||
biz.ErrIsNil(err, "文件上传失败")
|
||||
rc.ResData = map[string]string{"fileName": name}
|
||||
}
|
||||
|
||||
func (up *UploadApi) GetImage(rc *restfulx.ReqCtx) {
|
||||
actual := path.Join(filePath, restfulx.PathParam(rc, "subpath"))
|
||||
http.ServeFile(
|
||||
rc.Response.ResponseWriter,
|
||||
rc.Request.Request,
|
||||
actual)
|
||||
}
|
||||
|
||||
func (up *UploadApi) DeleteImage(rc *restfulx.ReqCtx) {
|
||||
fileName := restfulx.QueryParam(rc, "fileName")
|
||||
biz.NotEmpty(fileName, "请传要删除的图片名")
|
||||
err := os.Remove(fmt.Sprintf("%s/%s", filePath, fileName))
|
||||
biz.ErrIsNil(err, "文件删除失败")
|
||||
}
|
||||
39
apps/system/router/upload.go
Normal file
39
apps/system/router/upload.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
restfulspec "github.com/emicklei/go-restful-openapi/v2"
|
||||
"github.com/emicklei/go-restful/v3"
|
||||
"pandax/apps/system/api"
|
||||
)
|
||||
|
||||
func InitUploadRouter(container *restful.Container) {
|
||||
s := &api.UploadApi{}
|
||||
ws := new(restful.WebService)
|
||||
ws.Path("/upload").Produces(restful.MIME_JSON)
|
||||
tags := []string{"upload"}
|
||||
|
||||
ws.Route(ws.POST("/up").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithLog("上传图片").Handle(s.UploadImage)
|
||||
}).
|
||||
Doc("上传图片").
|
||||
Param(ws.FormParameter("imagefile", "文件")).
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Returns(200, "OK", map[string]string{}))
|
||||
|
||||
ws.Route(ws.GET("/get/{subpath}").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithNeedToken(false).WithNeedCasbin(false).WithLog("获取图片").Handle(s.GetImage)
|
||||
}).
|
||||
Doc("获取图片").
|
||||
Param(ws.PathParameter("subpath", "文件名")).
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags))
|
||||
|
||||
ws.Route(ws.DELETE("/delete").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithLog("删除图片").Handle(s.DeleteImage)
|
||||
}).
|
||||
Doc("删除图片").
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Param(ws.QueryParameter("fileName", "文件名称").DataType("string")))
|
||||
|
||||
container.Add(ws)
|
||||
}
|
||||
Reference in New Issue
Block a user