mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-21 14:51:00 +01:00
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 <Berengar.Lehr@kompetenztest.de> * Update Gopkg.lock Can't update all vendors due to errors Signed-off-by: Berengar W. Lehr <Berengar.Lehr@kompetenztest.de> * style: Add Gitea copyright header * feat: Added integration test for /repo/u/r/raw/blob * fix: correct year in copyright header
This commit is contained in:
parent
4651ba06c1
commit
e08c7e521b
6 changed files with 85 additions and 2 deletions
4
Gopkg.lock
generated
4
Gopkg.lock
generated
|
@ -3,11 +3,11 @@
|
|||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:835585f8450b4ec12252d032b0f13e6571ecf846e49076f69067f2503a7c1e07"
|
||||
digest = "1:296fd9dfbae66f6feeb09c7163ec39c262de425289154430a55d0a248c520486"
|
||||
name = "code.gitea.io/git"
|
||||
packages = ["."]
|
||||
pruneopts = "NUT"
|
||||
revision = "6ef79e80b3b06ca13a1f3a7b940903ebc73b44cb"
|
||||
revision = "d945eda535aa7d6b3c1f486279df2a3f7d05f78b"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
|
|
24
integrations/download_test.go
Normal file
24
integrations/download_test.go
Normal file
|
@ -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())
|
||||
}
|
|
@ -484,6 +484,8 @@ const (
|
|||
RepoRefTag
|
||||
// RepoRefCommit commit
|
||||
RepoRefCommit
|
||||
// RepoRefBlob blob
|
||||
RepoRefBlob
|
||||
)
|
||||
|
||||
// RepoRef handles repository reference names when the ref name is not
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
30
vendor/code.gitea.io/git/repo_blob.go
generated
vendored
Normal file
30
vendor/code.gitea.io/git/repo_blob.go
generated
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
// 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 git
|
||||
|
||||
func (repo *Repository) getBlob(id SHA1) (*Blob, error) {
|
||||
if _, err := NewCommand("cat-file", "-p", id.String()).RunInDir(repo.Path); err != nil {
|
||||
return nil, ErrNotExist{id.String(), ""}
|
||||
}
|
||||
|
||||
return &Blob{
|
||||
repo: repo,
|
||||
TreeEntry: &TreeEntry{
|
||||
ID: id,
|
||||
ptree: &Tree{
|
||||
repo: repo,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetBlob finds the blob object in the repository.
|
||||
func (repo *Repository) GetBlob(idStr string) (*Blob, error) {
|
||||
id, err := NewIDFromString(idStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return repo.getBlob(id)
|
||||
}
|
Loading…
Reference in a new issue