🐛 API /asset/upload 使用子文件夹时返回结果不正确 Fix https://github.com/siyuan-note/siyuan/issues/7454

This commit is contained in:
Liang Ding 2023-02-23 10:20:21 +08:00
parent 8d4519c7df
commit c0c605b664
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
4 changed files with 33 additions and 36 deletions

20
API.md
View File

@ -457,13 +457,12 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
* `/api/asset/upload` * `/api/asset/upload`
* The parameter is an HTTP Multipart form * The parameter is an HTTP Multipart form
* `assetsDirPath`: The folder path where the assets are stored. The arguments have the following three cases * `assetsDirPath`: The folder path where assets are stored, with the data folder as the root path, for example:
* `"/assets/"`: workspace/data/assets/ folder
* `"/assets/sub/"`: workspace/data/assets/sub/ folder
1. `"/assets/"`: Workspace/data/assets folder Under normal circumstances, it is recommended to use the first method, which is stored in the assets folder
2. `"/Test Notebook/assets/"`: Assets folder under `Test Notebook` of the workspace.
3. `"/Test Notebook/foo/assets/"`: Assets folder under foo folder under `Test notebook`
It is recommended to use the first one, which is stored in the workspace assets folder uniformly.
* `file[]`: Uploaded file list * `file[]`: Uploaded file list
* Return value * Return value
@ -705,7 +704,7 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
} }
``` ```
* `id`: ID of the block to be got * `id`: ID of the block to be got
* Return value * Return value
```json ```json
@ -718,7 +717,7 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
} }
} }
``` ```
## Attributes ## Attributes
### Set block attributes ### Set block attributes
@ -814,8 +813,9 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
"path": "F:\\SiYuan\\data\\templates\\foo.md" "path": "F:\\SiYuan\\data\\templates\\foo.md"
} }
``` ```
* `id`: The ID of the document where the rendering is called
* `path`: Template file absolute path * `id`: The ID of the document where the rendering is called
* `path`: Template file absolute path
* Return value * Return value
```json ```json

View File

@ -454,13 +454,11 @@
* `/api/asset/upload` * `/api/asset/upload`
* 参数为 HTTP Multipart 表单 * 参数为 HTTP Multipart 表单
* `assetsDirPath`:资源文件存放的文件夹路径,实参有以下三种情况 * `assetsDirPath`:资源文件存放的文件夹路径,以 data 文件夹作为根路径,比如:
* `"/assets/"`:工作空间/data/assets/ 文件夹
* `"/assets/sub/"`:工作空间/data/assets/sub/ 文件夹
1. `"/assets/"`:工作空间/data/assets 文件夹 常规情况下建议用第一种,统一存放到工作空间资源文件夹下。
2. `"/测试笔记本/assets/"``测试笔记本`下的 assets 文件夹
3. `"/测试笔记本/foo/assets/"``测试笔记本`下 foo 文件夹下的 assets 文件夹
建议用第一种,统一存放到工作空间资源文件夹下。
* `file[]`:上传的文件列表 * `file[]`:上传的文件列表
* 返回值 * 返回值

View File

@ -665,6 +665,8 @@ func AddPDFOutline(id, p string, merge bool) (err error) {
return links[i].Page < links[j].Page return links[i].Page < links[j].Page
}) })
pdfcpu.VersionStr = "SiYuan v" + util.Ver
bms := map[string]*pdfcpu.Bookmark{} bms := map[string]*pdfcpu.Bookmark{}
for _, link := range links { for _, link := range links {
linkID := link.URI[strings.LastIndex(link.URI, "/")+1:] linkID := link.URI[strings.LastIndex(link.URI, "/")+1:]
@ -798,15 +800,11 @@ func AddPDFOutline(id, p string, merge bool) (err error) {
} }
linkMap := map[int][]*pdfcpu.IndirectRef{} linkMap := map[int][]*pdfcpu.IndirectRef{}
//pdfCtx.RemoveAnnotations(nil, nil, nil, false) pdfCtx.RemoveAnnotations(nil, nil, nil, false)
now := pdfcpu.StringLiteral(pdfcpu.DateString(time.Now()))
for i, link := range assetLinks { for _, link := range assetLinks {
link.URI = strings.ReplaceAll(link.URI, "http://127.0.0.1:6806/export/temp/", "") link.URI = strings.ReplaceAll(link.URI, "http://127.0.0.1:6806/export/temp/", "")
//if 1 > len(linkMap[link.Page]) { link.URI, _ = url.PathUnescape(link.URI)
// linkMap[link.Page] = []pdfcpu.Annotation{link}
//} else {
// linkMap[link.Page] = append(linkMap[link.Page], link)
//}
absPath, getErr := GetAssetAbsPath(link.URI) absPath, getErr := GetAssetAbsPath(link.URI)
if nil != getErr { if nil != getErr {
@ -832,15 +830,17 @@ func AddPDFOutline(id, p string, merge bool) (err error) {
continue continue
} }
now := pdfcpu.StringLiteral(pdfcpu.DateString(time.Now())) lx := link.Rect.LL.X + link.Rect.Width()
mediaBox := pdfcpu.RectForFormat("A4") ly := link.Rect.LL.Y + link.Rect.Height()/2
r := annotRect(i, mediaBox.Width(), mediaBox.Height(), 30, 80) ux := lx + link.Rect.Height()/2
uy := ly + link.Rect.Height()/2
d := pdfcpu.Dict( d := pdfcpu.Dict(
map[string]pdfcpu.Object{ map[string]pdfcpu.Object{
"Type": pdfcpu.Name("Annot"), "Type": pdfcpu.Name("Annot"),
"Subtype": pdfcpu.Name("FileAttachment"), "Subtype": pdfcpu.Name("FileAttachment"),
"Contents": pdfcpu.StringLiteral("FileAttachment Annotation"), "Contents": pdfcpu.StringLiteral(""),
"Rect": r.Array(), "Rect": pdfcpu.Rect(lx, ly, ux, uy).Array(),
"P": link.P, "P": link.P,
"M": now, "M": now,
"F": pdfcpu.Integer(0), "F": pdfcpu.Integer(0),
@ -850,7 +850,7 @@ func AddPDFOutline(id, p string, merge bool) (err error) {
"CreationDate": now, "CreationDate": now,
"Name": pdfcpu.Name("FileAttachment"), "Name": pdfcpu.Name("FileAttachment"),
"FS": *ir, "FS": *ir,
"NM": pdfcpu.StringLiteral("SoundFileAttachmentAnnot"), "NM": pdfcpu.StringLiteral(""),
}, },
) )
@ -900,7 +900,6 @@ func AddPDFOutline(id, p string, merge bool) (err error) {
obj, found := pageDict.Find("Annots") obj, found := pageDict.Find("Annots")
if !found { if !found {
pageDict.Insert("Annots", array) pageDict.Insert("Annots", array)
pdfCtx.EnsureVersionForWriting() pdfCtx.EnsureVersionForWriting()
continue continue
} }
@ -926,8 +925,6 @@ func AddPDFOutline(id, p string, merge bool) (err error) {
} }
entry.Object = append(annots, array...) entry.Object = append(annots, array...)
pdfCtx.EnsureVersionForWriting() pdfCtx.EnsureVersionForWriting()
//d.Insert("Annots", array)
} }
if writeErr := api.WriteContextFile(pdfCtx, inFile); nil != writeErr { if writeErr := api.WriteContextFile(pdfCtx, inFile); nil != writeErr {

View File

@ -130,9 +130,11 @@ func Upload(c *gin.Context) {
docDirLocalPath := filepath.Join(util.DataDir, bt.BoxID, path.Dir(bt.Path)) docDirLocalPath := filepath.Join(util.DataDir, bt.BoxID, path.Dir(bt.Path))
assetsDirPath = getAssetsDir(filepath.Join(util.DataDir, bt.BoxID), docDirLocalPath) assetsDirPath = getAssetsDir(filepath.Join(util.DataDir, bt.BoxID), docDirLocalPath)
} }
relAssetsDirPath := "assets"
if nil != form.Value["assetsDirPath"] { if nil != form.Value["assetsDirPath"] {
assetsDirPath = form.Value["assetsDirPath"][0] relAssetsDirPath = form.Value["assetsDirPath"][0]
assetsDirPath = filepath.Join(util.DataDir, assetsDirPath) assetsDirPath = filepath.Join(util.DataDir, relAssetsDirPath)
} }
if !gulu.File.IsExist(assetsDirPath) { if !gulu.File.IsExist(assetsDirPath) {
if err = os.MkdirAll(assetsDirPath, 0755); nil != err { if err = os.MkdirAll(assetsDirPath, 0755); nil != err {
@ -187,7 +189,7 @@ func Upload(c *gin.Context) {
break break
} }
f.Close() f.Close()
succMap[baseName] = "assets/" + fName succMap[baseName] = path.Join(relAssetsDirPath, fName)
} }
} }