Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2022-10-26 12:12:27 +08:00
commit b478a9098e
2 changed files with 26 additions and 0 deletions

View file

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

View file

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