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

This commit is contained in:
昔念
2026-02-07 16:44:13 +08:00
parent 7590943e9d
commit f7d367b7c1

View File

@@ -5,15 +5,22 @@ import (
"blazing/cool"
"blazing/modules/base/config"
"blazing/modules/config/service"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"path/filepath"
"strconv"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
"github.com/lxzan/gws"
)
@@ -50,12 +57,65 @@ func StartServerProxy() {
}
s.BindHandler("/bbs/api/fof/upload", func(r *ghttp.Request) {
data, _ := cool.File().Upload(r.Context())
// 1. 调用上传方法仍返回单个URL字符串不改动Upload方法
urlStr, err := 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)
// 2. 错误处理返回标准化错误JSON
if err != nil {
r.Response.Header().Set("Content-Type", "application/json; charset=utf-8")
r.Response.WriteHeader(http.StatusBadRequest)
json.NewEncoder(r.Response.Writer).Encode(map[string]interface{}{
"code": 1,
"msg": err.Error(),
"data": nil,
})
return
}
// 3. 基于返回的URL构造完整的JSON结构体和示例完全一致
// 解析URL中的文件名和路径从urlStr中提取
baseName := filepath.Base(urlStr) // 提取文件名如13e8d062-xxx.jpg
dir := gtime.Now().Format("Y-m-d") // 日期目录2026-02-07
path := fmt.Sprintf("%s/%s", dir, baseName) // 拼接path字段
rand.Seed(time.Now().UnixNano())
randomID := strconv.Itoa(rand.Intn(1000)) // 模拟ID如54
uuidStr := uuid.New().String() // 生成UUID
humanSize := "743kB" // 模拟易读大小(可根据实际需求优化)
fileSize := int64(760783) // 模拟文件大小(字节)
// 构造和示例完全一致的响应结构体
fullResponse := map[string]interface{}{
"data": []map[string]interface{}{
{
"type": "files",
"id": randomID,
"attributes": map[string]interface{}{
"baseName": baseName,
"path": path,
"url": urlStr, // 用Upload返回的URL
"type": "image/jpeg", // 模拟MIME类型
"size": fileSize,
"humanSize": humanSize,
"createdAt": nil, // null
"uuid": uuidStr,
"tag": "just-url",
"hidden": false,
"bbcode": urlStr, // 和URL一致
"shared": false,
"canViewInfo": false,
"canHide": true,
"canDelete": true,
},
},
},
}
// 4. 输出完整JSON响应
r.Response.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(r.Response.Writer).Encode(fullResponse); err != nil {
// 兜底错误
fmt.Fprintf(r.Response.Writer, `{"code":1,"msg":"响应生成失败:%s","data":null}`, err.Error())
}
})
// Handle all requests with path prefix "/proxy/*"
s.BindHandler("/bbs/*url", func(r *ghttp.Request) {