feat(assets): store note images in shared sibling assets directories
This commit is contained in:
1 parent
5abc99b89e
commit
08f4e4aa7d
14 files changed
+507
-75
No files matched your search
+1
-1
@@ -371,7 +371,7 @@ func (s *Server) mkdir(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, 200, map[string]bool{"ok": true})
|
||||
}
|
||||
|
||||
// uploadAsset 接收 multipart 表单中的 file 字段,存入 ?note= 指定笔记的 .assets 目录
|
||||
// uploadAsset 接收 multipart 表单中的 file 字段,存入指定笔记或目录下的共享 assets 目录。
|
||||
func (s *Server) uploadAsset(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxAssetBytes+1<<20)
|
||||
file, header, err := r.FormFile("file")
|
||||
|
||||
@@ -5,9 +5,41 @@ import (
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUploadsReturnSharedRelativeLinks(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.write("notes/a.md", "正文")
|
||||
f.write("notes/b.md", "正文")
|
||||
var body bytes.Buffer
|
||||
form := multipart.NewWriter(&body)
|
||||
file, err := form.CreateFormFile("file", "image.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := io.WriteString(file, "image"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := form.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var previous string
|
||||
for _, query := range []string{"note=notes/a.md", "note=notes/b.md", "folder=notes"} {
|
||||
result := decode[struct{ Link string }](t, f.do(f.admin, "POST", "/api/v1/assets?"+query, body.String(), map[string]string{"Content-Type": form.FormDataContentType()}))
|
||||
if !strings.HasPrefix(result.Link, "./assets/") || (previous != "" && previous != result.Link) {
|
||||
t.Fatalf("上传链接或共享去重不符:%s", result.Link)
|
||||
}
|
||||
previous = result.Link
|
||||
res := f.do(f.admin, "GET", "/api/v1/raw/notes/"+strings.TrimPrefix(result.Link, "./"), "", nil)
|
||||
data, err := io.ReadAll(res.Body)
|
||||
if res.StatusCode != 200 || err != nil || string(data) != "image" {
|
||||
t.Fatalf("返回链接不能读取原图:%d %q %v", res.StatusCode, data, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplaceImageAPI(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.write("assets/a.png", "original")
|
||||
|
||||
Reference in new issue
Block a user