feat(assets): add searchable media library and verified orphan cleanup
This commit is contained in:
1 parent
06be0038cf
commit
91a359c2b7
13 files changed
+424
-51
No files matched your search
+11
-1
@@ -85,6 +85,7 @@ func (s *Server) routes() []route {
|
||||
{"POST /api/v1/folders", authed, s.mkdir},
|
||||
{"POST /api/v1/import", authed, s.importNotes},
|
||||
{"POST /api/v1/assets", authed, s.uploadAsset},
|
||||
{"POST /api/v1/assets/cleanup", authed, s.cleanupImages},
|
||||
{"POST /api/v1/assets/import", authed, s.importAsset},
|
||||
{"PUT /api/v1/assets/{path...}", authed, s.replaceImage},
|
||||
{"GET /api/v1/raw/{path...}", authed, s.raw},
|
||||
@@ -389,7 +390,16 @@ func (s *Server) uploadAsset(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
link, err := s.vault.SaveAsset(r.URL.Query().Get("note"), header.Filename, file, maxAssetBytes)
|
||||
var link string
|
||||
if r.URL.Query().Has("folder") {
|
||||
if r.URL.Query().Has("note") {
|
||||
writeError(w, 400, "不能同时指定笔记和文件夹")
|
||||
return
|
||||
}
|
||||
link, err = s.vault.SaveFolderAsset(r.URL.Query().Get("folder"), header.Filename, file, maxAssetBytes)
|
||||
} else {
|
||||
link, err = s.vault.SaveAsset(r.URL.Query().Get("note"), header.Filename, file, maxAssetBytes)
|
||||
}
|
||||
if err != nil {
|
||||
vaultError(w, r, err)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *Server) cleanupImages(w http.ResponseWriter, r *http.Request) {
|
||||
var body struct {
|
||||
Paths []string `json:"paths"`
|
||||
}
|
||||
if !readJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
if len(body.Paths) == 0 || len(body.Paths) > 1000 {
|
||||
writeError(w, 400, "请选择 1~1000 张待清理图片")
|
||||
return
|
||||
}
|
||||
result, err := s.vault.CleanupImages(body.Paths, func() ([]string, error) {
|
||||
shares, err := s.store.Shares()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
scopes := []string{}
|
||||
now := time.Now().Unix()
|
||||
for _, share := range shares {
|
||||
if share.ExpiresAt != 0 && share.ExpiresAt <= now {
|
||||
continue
|
||||
}
|
||||
for _, item := range share.Items {
|
||||
scopes = append(scopes, item.Path)
|
||||
}
|
||||
}
|
||||
return scopes, nil
|
||||
})
|
||||
if err != nil {
|
||||
vaultError(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, result)
|
||||
}
|
||||
@@ -737,6 +737,14 @@
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "folder",
|
||||
"in": "query",
|
||||
"description": "文件夹图片管理上传目标,空字符串表示根目录;与 note 互斥",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
@@ -2449,6 +2457,42 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/assets/cleanup": {
|
||||
"post": {
|
||||
"summary": "重新核对全库引用和有效分享后,将孤立图片移入回收站",
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"paths"
|
||||
],
|
||||
"properties": {
|
||||
"paths": {
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "已移入回收站的图片及跳过原因"
|
||||
},
|
||||
"400": {
|
||||
"description": "请求无效,或笔记元数据异常,无法确认引用"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
|
||||
Reference in new issue
Block a user