mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-04 01:09:31 +08:00
🎨 Add internal kernel API /api/repo/getRepoFile
https://github.com/siyuan-note/siyuan/issues/10101
This commit is contained in:
parent
2017494493
commit
ae2fb32569
@ -18,14 +18,48 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/88250/gulu"
|
"github.com/88250/gulu"
|
||||||
|
"github.com/gabriel-vasile/mimetype"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/siyuan-note/siyuan/kernel/model"
|
"github.com/siyuan-note/siyuan/kernel/model"
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getRepoFile(c *gin.Context) {
|
||||||
|
// Add internal kernel API `/api/repo/getRepoFile` https://github.com/siyuan-note/siyuan/issues/10101
|
||||||
|
|
||||||
|
ret := gulu.Ret.NewResult()
|
||||||
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
||||||
|
arg, ok := util.JsonArg(c, ret)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id := arg["id"].(string)
|
||||||
|
data, p, err := model.GetRepoFile(id)
|
||||||
|
if nil != err {
|
||||||
|
ret.Code = -1
|
||||||
|
ret.Msg = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
contentType := mime.TypeByExtension(filepath.Ext(p))
|
||||||
|
if "" == contentType {
|
||||||
|
if m := mimetype.Detect(data); nil != m {
|
||||||
|
contentType = m.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if "" == contentType {
|
||||||
|
contentType = "application/octet-stream"
|
||||||
|
}
|
||||||
|
c.Data(http.StatusOK, contentType, data)
|
||||||
|
}
|
||||||
|
|
||||||
func openRepoSnapshotDoc(c *gin.Context) {
|
func openRepoSnapshotDoc(c *gin.Context) {
|
||||||
ret := gulu.Ret.NewResult()
|
ret := gulu.Ret.NewResult()
|
||||||
defer c.JSON(http.StatusOK, ret)
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
@ -363,6 +363,7 @@ func ServeAPI(ginServer *gin.Engine) {
|
|||||||
ginServer.Handle("POST", "/api/repo/downloadCloudSnapshot", model.CheckAuth, model.CheckReadonly, downloadCloudSnapshot)
|
ginServer.Handle("POST", "/api/repo/downloadCloudSnapshot", model.CheckAuth, model.CheckReadonly, downloadCloudSnapshot)
|
||||||
ginServer.Handle("POST", "/api/repo/diffRepoSnapshots", model.CheckAuth, diffRepoSnapshots)
|
ginServer.Handle("POST", "/api/repo/diffRepoSnapshots", model.CheckAuth, diffRepoSnapshots)
|
||||||
ginServer.Handle("POST", "/api/repo/openRepoSnapshotDoc", model.CheckAuth, openRepoSnapshotDoc)
|
ginServer.Handle("POST", "/api/repo/openRepoSnapshotDoc", model.CheckAuth, openRepoSnapshotDoc)
|
||||||
|
ginServer.Handle("POST", "/api/repo/getRepoFile", model.CheckAuth, getRepoFile)
|
||||||
|
|
||||||
ginServer.Handle("POST", "/api/riff/createRiffDeck", model.CheckAuth, model.CheckReadonly, createRiffDeck)
|
ginServer.Handle("POST", "/api/riff/createRiffDeck", model.CheckAuth, model.CheckReadonly, createRiffDeck)
|
||||||
ginServer.Handle("POST", "/api/riff/renameRiffDeck", model.CheckAuth, model.CheckReadonly, renameRiffDeck)
|
ginServer.Handle("POST", "/api/riff/renameRiffDeck", model.CheckAuth, model.CheckReadonly, renameRiffDeck)
|
||||||
|
@ -57,18 +57,25 @@ import (
|
|||||||
"github.com/studio-b12/gowebdav"
|
"github.com/studio-b12/gowebdav"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func GetRepoFile(fileID string) (ret []byte, p string, err error) {
|
||||||
subscribeRepoEvents()
|
if 1 > len(Conf.Repo.Key) {
|
||||||
|
err = errors.New(Conf.Language(26))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type Snapshot struct {
|
repo, err := newRepository()
|
||||||
*dejavu.Log
|
if nil != err {
|
||||||
TypesCount []*TypeCount `json:"typesCount"`
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type TypeCount struct {
|
file, err := repo.GetFile(fileID)
|
||||||
Type string `json:"type"`
|
if nil != err {
|
||||||
Count int `json:"count"`
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ret, err = repo.OpenFile(file)
|
||||||
|
p = file.Path
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func OpenRepoSnapshotDoc(fileID string) (content string, isProtyleDoc bool, updated int64, err error) {
|
func OpenRepoSnapshotDoc(fileID string) (content string, isProtyleDoc bool, updated int64, err error) {
|
||||||
@ -327,6 +334,16 @@ func parseTreeInSnapshot(data []byte, luteEngine *lute.Lute) (isProtyleDoc bool,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Snapshot struct {
|
||||||
|
*dejavu.Log
|
||||||
|
TypesCount []*TypeCount `json:"typesCount"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TypeCount struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Count int `json:"count"`
|
||||||
|
}
|
||||||
|
|
||||||
func GetRepoSnapshots(page int) (ret []*Snapshot, pageCount, totalCount int, err error) {
|
func GetRepoSnapshots(page int) (ret []*Snapshot, pageCount, totalCount int, err error) {
|
||||||
ret = []*Snapshot{}
|
ret = []*Snapshot{}
|
||||||
if 1 > len(Conf.Repo.Key) {
|
if 1 > len(Conf.Repo.Key) {
|
||||||
@ -1570,6 +1587,10 @@ func newRepository() (ret *dejavu.Repo, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
subscribeRepoEvents()
|
||||||
|
}
|
||||||
|
|
||||||
func subscribeRepoEvents() {
|
func subscribeRepoEvents() {
|
||||||
eventbus.Subscribe(eventbus.EvtIndexBeforeWalkData, func(context map[string]interface{}, path string) {
|
eventbus.Subscribe(eventbus.EvtIndexBeforeWalkData, func(context map[string]interface{}, path string) {
|
||||||
msg := fmt.Sprintf(Conf.Language(158), path)
|
msg := fmt.Sprintf(Conf.Language(158), path)
|
||||||
|
Loading…
Reference in New Issue
Block a user