瀏覽代碼

:art: Add more file formats supporting snapshots comparing https://github.com/siyuan-note/siyuan/issues/8438

Daniel 2 年之前
父節點
當前提交
30e70ade73
共有 3 個文件被更改,包括 47 次插入2 次删除
  1. 14 2
      kernel/model/repository.go
  2. 10 0
      kernel/server/serve.go
  3. 23 0
      kernel/util/path.go

+ 14 - 2
kernel/model/repository.go

@@ -145,8 +145,20 @@ func OpenRepoSnapshotDoc(fileID string) (content string, isProtyleDoc bool, upda
 		if strings.HasSuffix(file.Path, ".json") {
 			content = gulu.Str.FromBytes(data)
 		} else {
-			if strings.Contains(file.Path, "assets/") {
-				content = file.Path[strings.Index(file.Path, "assets/"):]
+			if strings.Contains(file.Path, "assets/") { // 剔除笔记本级或者文档级资源文件路径前缀
+				file.Path = file.Path[strings.Index(file.Path, "assets/"):]
+				if util.IsDisplayableAsset(file.Path) {
+					dir, f := filepath.Split(file.Path)
+					tempRepoDiffDir := filepath.Join(util.TempDir, "repo", "diff", dir)
+					if mkErr := os.MkdirAll(tempRepoDiffDir, 0755); nil != mkErr {
+						logging.LogErrorf("mkdir [%s] failed: %v", tempRepoDiffDir, mkErr)
+					} else {
+						if wrErr := os.WriteFile(filepath.Join(tempRepoDiffDir, f), data, 0644); nil != wrErr {
+							logging.LogErrorf("write file [%s] failed: %v", filepath.Join(tempRepoDiffDir, file.Path), wrErr)
+						}
+					}
+					content = path.Join("repo", "diff", file.Path)
+				}
 			} else {
 				content = file.Path
 			}

+ 10 - 0
kernel/server/serve.go

@@ -75,6 +75,7 @@ func Serve(fastMode bool) {
 	servePlugins(ginServer)
 	serveEmojis(ginServer)
 	serveTemplates(ginServer)
+	serveRepoDiff(ginServer)
 	api.ServeAPI(ginServer)
 
 	var host string
@@ -349,6 +350,15 @@ func serveAssets(ginServer *gin.Engine) {
 	})
 }
 
+func serveRepoDiff(ginServer *gin.Engine) {
+	ginServer.GET("/repo/diff/*path", model.CheckAuth, func(context *gin.Context) {
+		requestPath := context.Param("path")
+		p := filepath.Join(util.TempDir, "repo", "diff", requestPath)
+		http.ServeFile(context.Writer, context.Request, p)
+		return
+	})
+}
+
 func serveDebug(ginServer *gin.Engine) {
 	ginServer.GET("/debug/pprof/", gin.WrapF(pprof.Index))
 	ginServer.GET("/debug/pprof/allocs", gin.WrapF(pprof.Index))

+ 23 - 0
kernel/util/path.go

@@ -193,3 +193,26 @@ func FilterSelfChildDocs(paths []string) (ret []string) {
 func IsAssetLinkDest(dest []byte) bool {
 	return bytes.HasPrefix(dest, []byte("assets/"))
 }
+
+var (
+	SiYuanAssetsImage = []string{".apng", ".ico", ".cur", ".jpg", ".jpe", ".jpeg", ".jfif", ".pjp", ".pjpeg", ".png", ".gif", ".webp", ".bmp", ".svg", ".avif"}
+	SiYuanAssetsAudio = []string{".mp3", ".wav", ".ogg", ".m4a"}
+	SiYuanAssetsVideo = []string{".mov", ".weba", ".mkv", ".mp4", ".webm"}
+)
+
+func IsDisplayableAsset(p string) bool {
+	ext := strings.ToLower(filepath.Ext(p))
+	if "" == ext {
+		return false
+	}
+	if gulu.Str.Contains(ext, SiYuanAssetsImage) {
+		return true
+	}
+	if gulu.Str.Contains(ext, SiYuanAssetsAudio) {
+		return true
+	}
+	if gulu.Str.Contains(ext, SiYuanAssetsVideo) {
+		return true
+	}
+	return false
+}