GetFile api

tokarchuk/v1.17
Unknwon 10 years ago
parent 340a4595dd
commit a0f9197b45
  1. 1
      cmd/web.go
  2. 16
      modules/avatar/avatar.go
  3. 11
      modules/middleware/repo.go
  4. 16
      routers/api/v1/repo.go
  5. 27
      routers/api/v1/repo_file.go
  6. 28
      routers/repo/download.go

@ -183,6 +183,7 @@ func runWeb(*cli.Context) {
m.Group("/:username/:reponame", func() { m.Group("/:username/:reponame", func() {
m.Combo("/hooks").Get(v1.ListRepoHooks).Post(bind(v1.CreateRepoHookForm{}), v1.CreateRepoHook) m.Combo("/hooks").Get(v1.ListRepoHooks).Post(bind(v1.CreateRepoHookForm{}), v1.CreateRepoHook)
m.Patch("/hooks/:id:int", bind(v1.EditRepoHookForm{}), v1.EditRepoHook) m.Patch("/hooks/:id:int", bind(v1.EditRepoHookForm{}), v1.EditRepoHook)
m.Get("/raw/*", middleware.RepoRef(), v1.GetRepoRawFile)
}, middleware.ApiRepoAssignment(), middleware.ApiReqToken()) }, middleware.ApiRepoAssignment(), middleware.ApiReqToken())
}) })

@ -33,11 +33,17 @@ import (
"github.com/nfnt/resize" "github.com/nfnt/resize"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting"
) )
var ( var gravatarSource string
gravatar = "http://www.gravatar.com/avatar"
) func init() {
gravatarSource = setting.GravatarSource
if !strings.HasPrefix(gravatarSource, "http:") {
gravatarSource = "http:" + gravatarSource
}
}
// hash email to md5 string // hash email to md5 string
// keep this func in order to make this package indenpent // keep this func in order to make this package indenpent
@ -121,7 +127,7 @@ func (this *Avatar) Encode(wr io.Writer, size int) (err error) {
// get image from gravatar.com // get image from gravatar.com
func (this *Avatar) Update() { func (this *Avatar) Update() {
thunder.Fetch(gravatar+"/"+this.Hash+"?"+this.reqParams, thunder.Fetch(gravatarSource+this.Hash+"?"+this.reqParams,
this.imagePath) this.imagePath)
} }
@ -129,7 +135,7 @@ func (this *Avatar) UpdateTimeout(timeout time.Duration) (err error) {
select { select {
case <-time.After(timeout): case <-time.After(timeout):
err = fmt.Errorf("get gravatar image %s timeout", this.Hash) err = fmt.Errorf("get gravatar image %s timeout", this.Hash)
case err = <-thunder.GoFetch(gravatar+"/"+this.Hash+"?"+this.reqParams, case err = <-thunder.GoFetch(gravatarSource+this.Hash+"?"+this.reqParams,
this.imagePath): this.imagePath):
} }
return err return err

@ -122,6 +122,17 @@ func RepoRef() macaron.Handler {
err error err error
) )
// For API calls.
if ctx.Repo.GitRepo == nil {
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
ctx.Handle(500, "RepoRef Invalid repo "+repoPath, err)
return
}
ctx.Repo.GitRepo = gitRepo
}
// Get default branch. // Get default branch.
if len(ctx.Params("*")) == 0 { if len(ctx.Params("*")) == 0 {
refName = ctx.Repo.Repository.DefaultBranch refName = ctx.Repo.Repository.DefaultBranch

@ -15,6 +15,7 @@ import (
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
@ -161,20 +162,14 @@ func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) {
func ListMyRepos(ctx *middleware.Context) { func ListMyRepos(ctx *middleware.Context) {
ownRepos, err := models.GetRepositories(ctx.User.Id, true) ownRepos, err := models.GetRepositories(ctx.User.Id, true)
if err != nil { if err != nil {
ctx.JSON(500, map[string]interface{}{ ctx.JSON(500, &base.ApiJsonErr{"GetRepositories: " + err.Error(), base.DOC_URL})
"ok": false,
"error": err.Error(),
})
return return
} }
numOwnRepos := len(ownRepos) numOwnRepos := len(ownRepos)
collaRepos, err := models.GetCollaborativeRepos(ctx.User.Name) collaRepos, err := models.GetCollaborativeRepos(ctx.User.Name)
if err != nil { if err != nil {
ctx.JSON(500, map[string]interface{}{ ctx.JSON(500, &base.ApiJsonErr{"GetCollaborativeRepos: " + err.Error(), base.DOC_URL})
"ok": false,
"error": err.Error(),
})
return return
} }
@ -204,10 +199,7 @@ func ListMyRepos(ctx *middleware.Context) {
} }
for i := range collaRepos { for i := range collaRepos {
if err = collaRepos[i].GetOwner(); err != nil { if err = collaRepos[i].GetOwner(); err != nil {
ctx.JSON(500, map[string]interface{}{ ctx.JSON(500, &base.ApiJsonErr{"GetOwner: " + err.Error(), base.DOC_URL})
"ok": false,
"error": err.Error(),
})
return return
} }
j := i + numOwnRepos j := i + numOwnRepos

@ -3,3 +3,30 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package v1 package v1
import (
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/git"
"github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/routers/repo"
)
func GetRepoRawFile(ctx *middleware.Context) {
if ctx.Repo.Repository.IsPrivate && !ctx.Repo.HasAccess {
ctx.Error(404)
return
}
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreeName)
if err != nil {
if err == git.ErrNotExist {
ctx.Error(404)
} else {
ctx.JSON(500, &base.ApiJsonErr{"GetBlobByPath: " + err.Error(), base.DOC_URL})
}
return
}
if err = repo.ServeBlob(ctx, blob); err != nil {
ctx.JSON(500, &base.ApiJsonErr{"ServeBlob: " + err.Error(), base.DOC_URL})
}
}

@ -9,20 +9,14 @@ import (
"path" "path"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/git"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
) )
func SingleDownload(ctx *middleware.Context) { func ServeBlob(ctx *middleware.Context, blob *git.Blob) error {
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreeName)
if err != nil {
ctx.Handle(500, "GetBlobByPath", err)
return
}
dataRc, err := blob.Data() dataRc, err := blob.Data()
if err != nil { if err != nil {
ctx.Handle(500, "Data", err) return err
return
} }
buf := make([]byte, 1024) buf := make([]byte, 1024)
@ -40,4 +34,20 @@ func SingleDownload(ctx *middleware.Context) {
} }
ctx.Resp.Write(buf) ctx.Resp.Write(buf)
io.Copy(ctx.Resp, dataRc) io.Copy(ctx.Resp, dataRc)
return nil
}
func SingleDownload(ctx *middleware.Context) {
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreeName)
if err != nil {
if err == git.ErrNotExist {
ctx.Handle(404, "GetBlobByPath", nil)
} else {
ctx.Handle(500, "GetBlobByPath", err)
}
return
}
if err = ServeBlob(ctx, blob); err != nil {
ctx.Handle(500, "ServeBlob", err)
}
} }

Loading…
Cancel
Save