Pārlūkot izejas kodu

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

Liang Ding 2 gadi atpakaļ
vecāks
revīzija
c0c605b664
4 mainītis faili ar 33 papildinājumiem un 36 dzēšanām
  1. 10 10
      API.md
  2. 4 6
      API_zh_CN.md
  3. 14 17
      kernel/model/export.go
  4. 5 3
      kernel/model/upload.go

+ 10 - 10
API.md

@@ -457,13 +457,12 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
 * `/api/asset/upload`
 * 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
-        2. `"/Test Notebook/assets/"`: Assets folder under `Test Notebook`
-        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.
+      Under normal circumstances, it is recommended to use the first method, which is stored in the assets folder
+      of the workspace.
     * `file[]`: Uploaded file list
 * 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
 
   ```json
@@ -718,7 +717,7 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
     }
   }
   ```
-  
+
 ## 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"
   }
   ```
-  * `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
 
   ```json

+ 4 - 6
API_zh_CN.md

@@ -454,13 +454,11 @@
 * `/api/asset/upload`
 * 参数为 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[]`:上传的文件列表
 * 返回值
 

+ 14 - 17
kernel/model/export.go

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

+ 5 - 3
kernel/model/upload.go

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