summaryrefslogtreecommitdiffstats
path: root/modules/lfs/pointers.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2019-02-12 15:09:43 +0000
committerGitHub <noreply@github.com>2019-02-12 15:09:43 +0000
commit2a03e96bceadfcc5e18bd61e755980ee72dcdb15 (patch)
tree2ecc1848c76fe458f10dffe568a8bb0cdc0f32ee /modules/lfs/pointers.go
parent296814e887f9bcf0b1d44552deaf40e89e08ab50 (diff)
downloadgitea-2a03e96bceadfcc5e18bd61e755980ee72dcdb15.tar.gz
gitea-2a03e96bceadfcc5e18bd61e755980ee72dcdb15.zip
Allow markdown files to read from the LFS (#5787)
This PR makes it possible for the markdown renderer to render images and media straight from the LFS. Fix #5746 Signed-off-by: Andrew Thornton [art27@cantab.net](mailto:art27@cantab.net)
Diffstat (limited to 'modules/lfs/pointers.go')
-rw-r--r--modules/lfs/pointers.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/modules/lfs/pointers.go b/modules/lfs/pointers.go
new file mode 100644
index 0000000000..bc27ee37a7
--- /dev/null
+++ b/modules/lfs/pointers.go
@@ -0,0 +1,69 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package lfs
+
+import (
+ "io"
+ "strconv"
+ "strings"
+
+ "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/base"
+ "code.gitea.io/gitea/modules/setting"
+)
+
+// ReadPointerFile will return a partially filled LFSMetaObject if the provided reader is a pointer file
+func ReadPointerFile(reader io.Reader) (*models.LFSMetaObject, *[]byte) {
+ if !setting.LFS.StartServer {
+ return nil, nil
+ }
+
+ buf := make([]byte, 1024)
+ n, _ := reader.Read(buf)
+ buf = buf[:n]
+
+ if isTextFile := base.IsTextFile(buf); !isTextFile {
+ return nil, nil
+ }
+
+ return IsPointerFile(&buf), &buf
+}
+
+// IsPointerFile will return a partially filled LFSMetaObject if the provided byte slice is a pointer file
+func IsPointerFile(buf *[]byte) *models.LFSMetaObject {
+ if !setting.LFS.StartServer {
+ return nil
+ }
+
+ headString := string(*buf)
+ if !strings.HasPrefix(headString, models.LFSMetaFileIdentifier) {
+ return nil
+ }
+
+ splitLines := strings.Split(headString, "\n")
+ if len(splitLines) < 3 {
+ return nil
+ }
+
+ oid := strings.TrimPrefix(splitLines[1], models.LFSMetaFileOidPrefix)
+ size, err := strconv.ParseInt(strings.TrimPrefix(splitLines[2], "size "), 10, 64)
+ if len(oid) != 64 || err != nil {
+ return nil
+ }
+
+ contentStore := &ContentStore{BasePath: setting.LFS.ContentPath}
+ meta := &models.LFSMetaObject{Oid: oid, Size: size}
+ if !contentStore.Exists(meta) {
+ return nil
+ }
+
+ return meta
+}
+
+// ReadMetaObject will read a models.LFSMetaObject and return a reader
+func ReadMetaObject(meta *models.LFSMetaObject) (io.ReadCloser, error) {
+ contentStore := &ContentStore{BasePath: setting.LFS.ContentPath}
+ return contentStore.Get(meta, 0)
+}