This commit is contained in:
@@ -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的字段名 --------------------------
|
||||
// 优先级:uploadfiles(PHP转转适配器字段) > 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
@@ -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公开读
|
||||
|
||||
@@ -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 (
|
||||
|
||||
Reference in New Issue
Block a user