1
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

This commit is contained in:
昔念
2026-02-07 16:17:08 +08:00
parent ce2c381116
commit 2cf886d825
4 changed files with 43 additions and 9 deletions

View File

@@ -7,6 +7,7 @@ import (
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gtime"
)
@@ -17,21 +18,45 @@ type Local struct {
func (l *Local) Upload(ctx g.Ctx) (string, error) {
var (
err error
Request = g.RequestFromCtx(ctx)
request = g.RequestFromCtx(ctx)
file *ghttp.UploadFile
)
file := Request.GetUploadFile("file")
// -------------------------- 核心兼容逻辑适配PHP的字段名 --------------------------
// 优先级uploadfilesPHP转转适配器字段 > files通用多文件字段 > file原字段
// 1. 先尝试获取PHP fof/upload插件的 uploadfiles 字段(单文件)
file = request.GetUploadFile("uploadfiles")
if file == nil {
return "", gerror.New("上传文件为空")
// 2. 再尝试获取 files 字段多文件取第一个兼容PHP多文件上传
files := request.GetUploadFiles("files")
if len(files) > 0 {
file = files[0] // 取第一个文件和PHP Arr::get($data, 'data.0.url')逻辑一致
}
}
// 3. 最后兼容原 file 字段
if file == nil {
file = request.GetUploadFile("file")
}
// 所有字段都无文件,返回错误
if file == nil {
return "", gerror.New("上传文件为空未找到file/files/uploadfiles字段")
}
// -------------------------- 原有存储逻辑不变 --------------------------
// 以当前年月日为目录
dir := gtime.Now().Format("Ymd")
fileName, err := file.Save("./public/uploads/"+dir, true)
// 保存路径:./public/uploads/年月日
saveDir := "./public/uploads/" + dir
// 保存文件(自动重命名避免重复)
fileName, err := file.Save(saveDir, true)
if err != nil {
return "", err
return "", gerror.Wrap(err, "保存上传文件失败")
}
return cool.Config.File.Domain + "/public/uploads/" + dir + "/" + fileName, err
// -------------------------- 拼接访问URL原有逻辑不变 --------------------------
accessURL := cool.Config.File.Domain + "/public/uploads/" + dir + "/" + fileName
return accessURL, nil
}
func (l *Local) GetMode() (data interface{}, err error) {

File diff suppressed because one or more lines are too long

View File

@@ -59,7 +59,7 @@ blazing:
autoMigrate: true
eps: true
file:
mode: "zhuanzhuan" # local | minio | oss
mode: "local" # local | minio | oss
#前端上传地址,因为放弃本地,所以这个弃用了 ,现在被当成rpc地址
domain: "61.147.247.41"
# oss配置项兼容 minio oss 需要配置bucket公开读

View File

@@ -48,6 +48,15 @@ func StartServerProxy() {
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
writer.WriteHeader(http.StatusBadGateway)
}
s.BindHandler("/bbs/api/fof/upload", func(r *ghttp.Request) {
data, _ := cool.File().Upload(r.Context())
// 返回成功响应JSON格式极简版
r.Response.Header().Set("Content-Type", "application/json")
fmt.Fprintf(r.Response.Writer, `{"code":0,"msg":"上传成功","url":"%s"}`, data)
})
// Handle all requests with path prefix "/proxy/*"
s.BindHandler("/bbs/*url", func(r *ghttp.Request) {
var (