Skip to content

Commit

Permalink
Add raw blob endpoint to get objects by SHA ID (#5334)
Browse files Browse the repository at this point in the history
* Add raw blob endpoint

This should make it possible to download raw blobs directly from
/:repo/:username/raw/blob/:sha1 URLs.

* fix: Make it work

* As an SHA-ID is no path getRefNameFromPath can't be used to verify
file specifying parameter
* added relevant change in go-gitea/git #132

Signed-off-by: Berengar W. Lehr <[email protected]>

* Update Gopkg.lock

Can't update all vendors due to errors

Signed-off-by: Berengar W. Lehr <[email protected]>

* style: Add Gitea copyright header

* feat: Added integration test for /repo/u/r/raw/blob

* fix: correct year in copyright header
  • Loading branch information
HoffmannP authored and techknowlogick committed Nov 18, 2018
1 parent 4651ba0 commit e08c7e5
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions integrations/download_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018 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 integrations

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDownloadByID(t *testing.T) {
prepareTestEnv(t)

session := loginUser(t, "user2")

// Request raw blob
req := NewRequest(t, "GET", "/user2/repo1/raw/blob/4b4851ad51df6a7d9f25c979345979eaeb5b349f")
resp := session.MakeRequest(t, req, http.StatusOK)

assert.Equal(t, "# repo1\n\nDescription for repo1", resp.Body.String())
}
11 changes: 11 additions & 0 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ const (
RepoRefTag
// RepoRefCommit commit
RepoRefCommit
// RepoRefBlob blob
RepoRefBlob
)

// RepoRef handles repository reference names when the ref name is not
Expand Down Expand Up @@ -519,6 +521,9 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
if refName := getRefName(ctx, RepoRefCommit); len(refName) > 0 {
return refName
}
if refName := getRefName(ctx, RepoRefBlob); len(refName) > 0 {
return refName
}
ctx.Repo.TreePath = path
return ctx.Repo.Repository.DefaultBranch
case RepoRefBranch:
Expand All @@ -531,6 +536,12 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
return parts[0]
}
case RepoRefBlob:
_, err := ctx.Repo.GitRepo.GetBlob(path)
if err != nil {
return ""
}
return path
default:
log.Error(4, "Unrecognized path type: %v", path)
}
Expand Down
17 changes: 17 additions & 0 deletions routers/repo/download.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2018 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.

Expand Down Expand Up @@ -69,3 +70,19 @@ func SingleDownload(ctx *context.Context) {
ctx.ServerError("ServeBlob", err)
}
}

// DownloadByID download a file by sha1 ID
func DownloadByID(ctx *context.Context) {
blob, err := ctx.Repo.GitRepo.GetBlob(ctx.Params("sha"))
if err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetBlob", nil)
} else {
ctx.ServerError("GetBlob", err)
}
return
}
if err = ServeBlob(ctx, blob); err != nil {
ctx.ServerError("ServeBlob", err)
}
}
1 change: 1 addition & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownload)
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownload)
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownload)
m.Get("/blob/:sha", context.RepoRefByType(context.RepoRefBlob), repo.DownloadByID)
// "/*" route is deprecated, and kept for backward compatibility
m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.SingleDownload)
}, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode))
Expand Down
30 changes: 30 additions & 0 deletions vendor/code.gitea.io/git/repo_blob.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e08c7e5

Please sign in to comment.