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

This commit is contained in:
Daniel 2023-06-03 17:42:01 +08:00
parent e50a9b4e11
commit 30e70ade73
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 47 additions and 2 deletions

View file

@ -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
}

View file

@ -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))

View file

@ -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
}