Browse Source

[api] re-add file raw download with api

cgars 7 years ago
parent
commit
aea12c35df
2 changed files with 23 additions and 1 deletions
  1. 8 0
      pkg/setting/setting.go
  2. 15 1
      routes/api/v1/repo/file.go

+ 8 - 0
pkg/setting/setting.go

@@ -316,6 +316,12 @@ var (
 	RunUser      string
 	IsWindows    bool
 	HasRobotsTxt bool
+
+	Search struct {
+		Do        bool
+		IndexUrl  string
+		SearchUrl string
+	}
 )
 
 // DateLang transforms standard language locale name to corresponding value in datetime plugin.
@@ -662,6 +668,8 @@ func NewContext() {
 		log.Fatal(2, "Fail to map API settings: %v", err)
 	} else if err = Cfg.Section("ui").MapTo(&UI); err != nil {
 		log.Fatal(2, "Fail to map UI settings: %v", err)
+	} else if err = Cfg.Section("search").MapTo(&Search); err != nil {
+		log.Fatal(2, "Fail to map Serch settings: %v", err)
 	}
 
 	if Mirror.DefaultInterval <= 0 {

+ 15 - 1
routes/api/v1/repo/file.go

@@ -10,6 +10,7 @@ import (
 	"github.com/G-Node/gogs/models"
 	"github.com/G-Node/gogs/pkg/context"
 	"github.com/G-Node/gogs/routes/repo"
+	"github.com/go-macaron/captcha"
 )
 
 // https://github.com/gogits/go-gogs-client/wiki/Repositories-Contents#download-raw-content
@@ -23,7 +24,20 @@ func GetRawFile(c *context.APIContext) {
 		c.Status(404)
 		return
 	}
-	return
+
+	blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath)
+	if err != nil {
+		if git.IsErrNotExist(err) {
+			c.Status(404)
+		} else {
+			c.Error(500, "GetBlobByPath", err)
+		}
+		return
+	}
+	cp := captcha.NewCaptcha(captcha.Options{})
+	if err = repo.ServeBlob(c.Context, blob, cp); err != nil {
+		c.Error(500, "ServeBlob", err)
+	}
 }
 
 // https://github.com/gogits/go-gogs-client/wiki/Repositories-Contents#download-archive