Przeglądaj źródła

:art: 支持伺服代码片段 `/snippets/` Fix https://github.com/siyuan-note/siyuan/issues/6356

Liang Ding 2 lat temu
rodzic
commit
2063203f23
2 zmienionych plików z 26 dodań i 0 usunięć
  1. 1 0
      kernel/api/router.go
  2. 25 0
      kernel/api/snippet.go

+ 1 - 0
kernel/api/router.go

@@ -283,4 +283,5 @@ func ServeAPI(ginServer *gin.Engine) {
 	ginServer.Handle("POST", "/api/notification/pushErrMsg", model.CheckAuth, pushErrMsg)
 
 	ginServer.Handle("POST", "/api/snippet/getSnippet", model.CheckAuth, getSnippet)
+	ginServer.Handle("GET", "/snippets/*filepath", serveSnippets)
 }

+ 25 - 0
kernel/api/snippet.go

@@ -17,15 +17,40 @@
 package api
 
 import (
+	"mime"
 	"net/http"
+	"path/filepath"
+	"strings"
 
 	"github.com/88250/gulu"
 	"github.com/gin-gonic/gin"
+	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/siyuan/kernel/conf"
 	"github.com/siyuan-note/siyuan/kernel/model"
 	"github.com/siyuan-note/siyuan/kernel/util"
 )
 
+func serveSnippets(c *gin.Context) {
+	name := strings.TrimPrefix(c.Request.URL.Path, "/snippets/")
+	ext := filepath.Ext(name)
+	name = strings.TrimSuffix(name, ext)
+	confSnippets, err := model.LoadSnippets()
+	if nil != err {
+		logging.LogErrorf("load snippets failed: %s", name, err)
+		c.Status(404)
+		return
+	}
+
+	for _, s := range confSnippets {
+		if s.Name == name && ("" != ext && s.Type == ext[1:]) {
+			c.Header("Content-Type", mime.TypeByExtension(ext))
+			c.String(http.StatusOK, s.Content)
+			return
+		}
+	}
+	c.Status(404)
+}
+
 func getSnippet(c *gin.Context) {
 	ret := gulu.Ret.NewResult()
 	defer c.JSON(http.StatusOK, ret)