Skip to content

Commit e08c7e5

Browse files
HoffmannPtechknowlogick
authored andcommitted
Add raw blob endpoint to get objects by SHA ID (#5334)
* 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
1 parent 4651ba0 commit e08c7e5

File tree

6 files changed

+85
-2
lines changed

6 files changed

+85
-2
lines changed

Gopkg.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integrations/download_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestDownloadByID(t *testing.T) {
15+
prepareTestEnv(t)
16+
17+
session := loginUser(t, "user2")
18+
19+
// Request raw blob
20+
req := NewRequest(t, "GET", "/user2/repo1/raw/blob/4b4851ad51df6a7d9f25c979345979eaeb5b349f")
21+
resp := session.MakeRequest(t, req, http.StatusOK)
22+
23+
assert.Equal(t, "# repo1\n\nDescription for repo1", resp.Body.String())
24+
}

modules/context/repo.go

+11
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ const (
484484
RepoRefTag
485485
// RepoRefCommit commit
486486
RepoRefCommit
487+
// RepoRefBlob blob
488+
RepoRefBlob
487489
)
488490

489491
// RepoRef handles repository reference names when the ref name is not
@@ -519,6 +521,9 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
519521
if refName := getRefName(ctx, RepoRefCommit); len(refName) > 0 {
520522
return refName
521523
}
524+
if refName := getRefName(ctx, RepoRefBlob); len(refName) > 0 {
525+
return refName
526+
}
522527
ctx.Repo.TreePath = path
523528
return ctx.Repo.Repository.DefaultBranch
524529
case RepoRefBranch:
@@ -531,6 +536,12 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
531536
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
532537
return parts[0]
533538
}
539+
case RepoRefBlob:
540+
_, err := ctx.Repo.GitRepo.GetBlob(path)
541+
if err != nil {
542+
return ""
543+
}
544+
return path
534545
default:
535546
log.Error(4, "Unrecognized path type: %v", path)
536547
}

routers/repo/download.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2018 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -69,3 +70,19 @@ func SingleDownload(ctx *context.Context) {
6970
ctx.ServerError("ServeBlob", err)
7071
}
7172
}
73+
74+
// DownloadByID download a file by sha1 ID
75+
func DownloadByID(ctx *context.Context) {
76+
blob, err := ctx.Repo.GitRepo.GetBlob(ctx.Params("sha"))
77+
if err != nil {
78+
if git.IsErrNotExist(err) {
79+
ctx.NotFound("GetBlob", nil)
80+
} else {
81+
ctx.ServerError("GetBlob", err)
82+
}
83+
return
84+
}
85+
if err = ServeBlob(ctx, blob); err != nil {
86+
ctx.ServerError("ServeBlob", err)
87+
}
88+
}

routers/routes/routes.go

+1
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ func RegisterRoutes(m *macaron.Macaron) {
693693
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownload)
694694
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownload)
695695
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownload)
696+
m.Get("/blob/:sha", context.RepoRefByType(context.RepoRefBlob), repo.DownloadByID)
696697
// "/*" route is deprecated, and kept for backward compatibility
697698
m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.SingleDownload)
698699
}, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode))

vendor/code.gitea.io/git/repo_blob.go

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)