add upload dir_name

This commit is contained in:
Havoc412 2024-10-18 20:51:40 +08:00
parent 9d4492e044
commit 81729bc718
5 changed files with 25 additions and 5 deletions

View File

@ -16,7 +16,14 @@ type Upload struct {
//
// 开始上传
func (u *Upload) StartUpload(context *gin.Context) {
savePath := variable.BasePath + variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath")
// TODO 如果之后要存储到 Linux 服务器上特殊路径下,就需要修改这里。
dir_name := context.GetString(consts.ValidatorPrefix + "dir_name")
// 检查 dir_name 是否以 / 结尾,如果不是则补充一个 /
if len(dir_name) > 0 && dir_name[len(dir_name)-1] != '/' {
dir_name += "/"
}
savePath := variable.BasePath + variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath") + dir_name
if r, finnalSavePath := upload_file.Upload(context, savePath); r == true {
response.Success(context, consts.CurdStatusOkMsg, finnalSavePath)
} else {

View File

@ -4,6 +4,7 @@ import (
"catface/app/global/consts"
"catface/app/global/variable"
"catface/app/http/controller/web"
"catface/app/http/validator/core/data_transfer"
"catface/app/utils/files"
"catface/app/utils/response"
"strconv"
@ -13,10 +14,24 @@ import (
)
type UpFiles struct {
DirName string `form:"dir_name" json:"dir_name"`
}
// 文件上传公共模块表单参数验证器
func (u UpFiles) CheckParams(context *gin.Context) {
// 1.基本的验证规则没有通过
if err := context.ShouldBind(&u); err != nil {
response.ValidatorError(context, err)
return
}
// 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值
extraAddBindDataContext := data_transfer.DataAddContext(u, consts.ValidatorPrefix, context)
if extraAddBindDataContext == nil {
response.ErrorSystem(context, "animialList表单验证器json化失败", "")
return
}
// 2. File 内容的验证;
tmpFile, err := context.FormFile(variable.ConfigYml.GetString("FileUploadSetting.UploadFileField")) // file 是一个文件结构体(文件对象)
var isPass bool
//获取文件发生错误,可能上传了空文件等

View File

@ -57,8 +57,8 @@ SnowFlake:
FileUploadSetting:
Size: 32 #设置上传文件的最大值单位M注意 如果go前置nginx服务器nginx 默认限制文件上传大小为 50 M ,用户上传文件限制还需要继续修改 nginx 配置
UploadFileField: "file" #post上传文件时表单的键名
UploadFileSavePath: "/storage/app/uploaded/" #上传文件保存在路径, 该路径与 BasePath 进行拼接使用
UploadFileReturnPath: "/public/storage/uploaded/" # 文件上后返回的路径由于程序会自动创建软连接自动将资源定位到实际路径所有资源的访问入口建议都从public开始
UploadFileSavePath: "/public/nginx/" #上传文件保存在路径, 该路径与 BasePath 进行拼接使用
UploadFileReturnPath: "" # 文件上后返回的路径由于程序会自动创建软连接自动将资源定位到实际路径所有资源的访问入口建议都从public开始
AllowMimeType: #允许的文件mime类型列表
- "image/jpeg" #jpg、jpeg图片格式
- "image/png" #png图片格式

View File

@ -3,12 +3,10 @@ package routers
import (
"catface/app/global/consts"
"catface/app/global/variable"
// "catface/app/http/controller/captcha" // 验证码组件
// "catface/app/http/middleware/authorization"
"catface/app/http/middleware/cors"
validatorFactory "catface/app/http/validator/core/factory"
// TODO validatorFactory "catface/app/http/validator/core/factory"
"catface/app/utils/gin_release"
"net/http"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 618 KiB