Jelajahi Sumber

[annex] Allow annex direct download

cgars 8 tahun lalu
induk
melakukan
0873ea23c5
2 mengubah file dengan 74 tambahan dan 12 penghapusan
  1. 23 1
      routes/repo/download.go
  2. 51 11
      routes/repo/view.go

+ 23 - 1
routes/repo/download.go

@@ -10,9 +10,12 @@ import (
 
 	"github.com/gogits/git-module"
 
-	"github.com/gogits/gogs/pkg/tool"
+	"bufio"
+	"github.com/G-Node/go-annex"
 	"github.com/gogits/gogs/pkg/context"
 	"github.com/gogits/gogs/pkg/setting"
+	"github.com/gogits/gogs/pkg/tool"
+	"os"
 )
 
 func ServeData(c *context.Context, name string, reader io.Reader) error {
@@ -21,6 +24,21 @@ func ServeData(c *context.Context, name string, reader io.Reader) error {
 	if n >= 0 {
 		buf = buf[:n]
 	}
+	isannex := tool.IsAnnexedFile(buf)
+	var afpR *bufio.Reader
+	var afp *os.File
+	if isannex == true {
+		af, err := gannex.NewAFile(c.Repo.Repository.RepoPath(), "annex", name, buf)
+		if err != nil {
+
+		}
+		afp, err = af.Open()
+		if err != nil {
+
+		}
+		afpR = bufio.NewReader(afp)
+		buf, _ = afpR.Peek(1024)
+	}
 
 	if !tool.IsTextFile(buf) {
 		if !tool.IsImageFile(buf) {
@@ -30,6 +48,10 @@ func ServeData(c *context.Context, name string, reader io.Reader) error {
 	} else if !setting.Repository.EnableRawFileRenderMode || !c.QueryBool("render") {
 		c.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
 	}
+	if isannex {
+		_, err := io.Copy(c.Resp, afpR)
+		return err
+	}
 	c.Resp.Write(buf)
 	_, err := io.Copy(c.Resp, reader)
 	return err

+ 51 - 11
routes/repo/view.go

@@ -17,6 +17,8 @@ import (
 
 	"github.com/gogits/git-module"
 
+	"bufio"
+	"github.com/G-Node/go-annex"
 	"github.com/gogits/gogs/models"
 	"github.com/gogits/gogs/pkg/context"
 	"github.com/gogits/gogs/pkg/markup"
@@ -25,6 +27,7 @@ import (
 	"github.com/gogits/gogs/pkg/template/highlight"
 	"github.com/gogits/gogs/pkg/tool"
 	"io"
+	"os"
 )
 
 const (
@@ -144,7 +147,31 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri
 	n, _ := r.Read(buf)
 	log.Trace("I read %s bytes", n)
 	buf = buf[:n]
-	isTextFile := tool.IsTextFile(buf) && !tool.IsAnnexedFile(buf)
+	isannex := tool.IsAnnexedFile(buf)
+
+	var afpR *bufio.Reader
+	var afp *os.File
+	var annexf *gannex.AFile
+	if isannex == true {
+		af, err := gannex.NewAFile(c.Repo.Repository.RepoPath(), "annex", entry.Name(), buf)
+		if err != nil {
+			c.Data["IsAnnexedFile"] = true
+			log.Trace("Could not get annex file: %v", err)
+			return
+		}
+		afp, err = af.Open()
+		if err != nil {
+			log.Trace("Could not open annex file: %v", err)
+			c.Data["IsAnnexedFile"] = true
+			return
+		}
+		afpR = bufio.NewReader(afp)
+		buf, _ = afpR.Peek(1024)
+		annexf = af
+		c.Data["FileSize"] = af.Info.Size()
+	}
+
+	isTextFile := tool.IsTextFile(buf)
 	c.Data["IsTextFile"] = isTextFile
 
 	// Assume file is not editable first.
@@ -155,17 +182,26 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri
 	canEnableEditor := c.Repo.CanEnableEditor()
 	switch {
 	case isTextFile:
-		if blob.Size() >= setting.UI.MaxDisplayFileSize {
-			c.Data["IsFileTooLarge"] = true
-			break
-		}
+		if !isannex {
+			if blob.Size() >= setting.UI.MaxDisplayFileSize {
+				c.Data["IsFileTooLarge"] = true
+				break
+			}
 
-		c.Data["ReadmeExist"] = markup.IsReadmeFile(blob.Name())
-		if blob.Size() > 1024 {
-			d := make([]byte, blob.Size()-
-				1024)
-			r.Read(d)
-			buf = append(buf, d...)
+			c.Data["ReadmeExist"] = markup.IsReadmeFile(blob.Name())
+			if blob.Size() > 1024 {
+				d := make([]byte, blob.Size()-
+					1024)
+				r.Read(d)
+				buf = append(buf, d...)
+			}
+		} else {
+			if annexf.Info.Size() >= setting.UI.MaxDisplayFileSize {
+				c.Data["IsFileTooLarge"] = true
+				break
+			}
+			c.Data["ReadmeExist"] = markup.IsReadmeFile(blob.Name())
+			afpR.Read(buf)
 		}
 
 		switch markup.Detect(blob.Name()) {
@@ -232,6 +268,10 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri
 	}
 }
 
+func renderAnnexFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink string) {
+
+}
+
 func setEditorconfigIfExists(c *context.Context) {
 	ec, err := c.Repo.GetEditorconfig()
 	if err != nil && !git.IsErrNotExist(err) {