文件上传:R2 对象存储 + 后台文件管理 + 编辑器/封面联动
存储抽象(internal/storage,新包)
- BlobStore 接口(Put / Open / Delete)+ 两个实现:R2(aws-sdk-go-v2 走
R2 的 S3 兼容 API,path-style、region auto)与本地磁盘(DataDir/uploads,
零配置兜底)。选择:Endpoint / S3Api / Bucket / AccessKey / SecretAccessKey
齐全 → R2,缺任一项回落本地并在启动日志提示缺失的字段名(只报名字不报值)
- 端点 URL 里的路径段不交给 SDK:path-style 下它会被折进对象 key,
导致「数据库 key」和「实际对象 key」对不上(直链 404,实测复现)。
EndpointKeyPrefix 提取路径段给上传 handler 拼进 key,StripEndpointPath
只取 scheme://host 给 SDK——数据库 / 存储端 / 直链三方一致
数据与 API
- files 表:id / key(唯一) / name / mime / size / sha256 / store(r2|local) /
created_at;URL 不入库,按「store 来源 + PublicBase」响应时解析,
切存储端不破坏存量链接
- POST /api/admin/files:multipart 多文件,单文件 ≤50MB(MaxBytesReader 64MB);
类型白名单 = 图片(jpg/png/webp/gif/avif)+ 附件(pdf/zip/txt),
扩展名 + http.DetectContentType 双重校验(实测拦截随机字节改名 .png),
SVG 拒绝(同源脚本);内容 sha256 做 key(2026/09/{哈希前12位}{扩展名}),
同内容重复上传自动去重复用
- GET /api/admin/files(分页 + 文件名搜索)、DELETE /{id}(先删对象再删行,
存储端失败保留行可重试)
- 公开路由 GET /uploads/{key}(main.go 挂载):按 key 查行、存储层流式返回,
Cache-Control immutable + ETag 304;R2 + PublicBase 时 302 直链(后端不出流量)
- URL 解析:FileURL(store, key, PublicBase)——R2 且配了公开域名走直链,
否则 /uploads/ 流式
后台文件管理页(FilesView,「工作台 → 文件」)
- 点击 / 拖拽多选上传(uploadFiles 走 FormData 裸 fetch,401 广播与
request() 一致);缩略图卡片网格(图片出图、其他出类型占位);
复制链接(clipboard,非 https 回落 prompt)/ 打开 / 删除(确认提示);
分页、loading/empty 沿用既有模式
编辑器联动(EditorView)
- 封面:URL 输入框旁「上传」按钮,选图自动填 cover_url
- wysiwyg:Crepe ImageBlock 官方 onUpload 钩子——粘贴 / 拖拽 / 插图
全部走上传,返回 URL 后由 Crepe 插节点
- Markdown 模式:插图弹层加「上传」按钮 + 编辑器粘贴 / 拖拽图片,
上传后在原光标处插入 (异步上传先记光标位,逐张追踪偏移)
.env.example
- 模板入库(无敏感值):Endpoint = 公开访问域名(直链)、S3Api = 上传端点、
Bucket / AccessKey / SecretAccessKey
验证
- 本地兜底全流程:上传 201、同内容去重复用、随机字节改名 .png 被
内容嗅探拒绝、.svg 拒绝、公开路由 immutable 缓存头 + 内容一致、
删除后存储与公开路由双清 404
- R2 真实链路(站主 .env):上传 store=r2、直链 200(过程中定位并修复
S3Api 路径段折进 key 导致的直链 404,见 EndpointKeyPrefix)
- 后端 go build/test/vet 全绿;前端构建通过
This commit is contained in:
@@ -6,19 +6,23 @@ import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"html"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"oneblog/internal/config"
|
||||
"oneblog/internal/httpx"
|
||||
"oneblog/internal/model"
|
||||
"oneblog/internal/storage"
|
||||
"oneblog/internal/store"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
Store *store.Store
|
||||
Cfg *config.Config
|
||||
Store *store.Store
|
||||
Cfg *config.Config
|
||||
Blobs storage.BlobStore // 文件上传的存储后端(main.go 装配,与 admin 共享)
|
||||
}
|
||||
|
||||
func (a *API) Routes() http.Handler {
|
||||
@@ -275,3 +279,53 @@ func orDefault(s, def string) string {
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// ---------- uploads(上传文件的公开访问) ----------
|
||||
|
||||
// UploadsHandler 供 main.go 挂在根 mux 的 /uploads/ 前缀上。按 key 查索引行,
|
||||
// 经存储层流式返回本体;R2 + 公开域名时 302 到直链(后端不出流量)。
|
||||
// key 必须在 files 表里有行——防止拿任意对象名探测存储端。
|
||||
func (a *API) UploadsHandler() http.Handler {
|
||||
return http.HandlerFunc(a.uploads)
|
||||
}
|
||||
|
||||
func (a *API) uploads(w http.ResponseWriter, r *http.Request) {
|
||||
key := strings.TrimPrefix(r.URL.Path, "/uploads/")
|
||||
if key == "" || strings.Contains(key, "..") {
|
||||
httpx.NotFound(w)
|
||||
return
|
||||
}
|
||||
f, err := a.Store.GetFileByKey(key)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
httpx.NotFound(w)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpx.ServerError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
// R2 且配了公开域名:302 到直链,后端不出流量
|
||||
if f.Store == "r2" && a.Cfg.UploadsPublicBase != "" {
|
||||
http.Redirect(w, r, storage.FileURL(f.Store, f.Key, a.Cfg.UploadsPublicBase), http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
rc, size, err := a.Blobs.Open(r.Context(), f.Key)
|
||||
if err != nil {
|
||||
httpx.NotFound(w)
|
||||
return
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
w.Header().Set("Content-Type", f.Mime)
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
|
||||
w.Header().Set("ETag", `"`+f.SHA256+`"`)
|
||||
// key 含内容哈希:内容不变则 URL 不变,可永久缓存
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
||||
if match := r.Header.Get("If-None-Match"); match != "" && match == `"`+f.SHA256+`"` {
|
||||
w.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
}
|
||||
io.Copy(w, rc)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user