Skip to content

Commit 00cd19f

Browse files
committed
Add raw blob endpoint
This should make it possible to download raw blobs directly from /:repo/:username/raw/blob/:sha1 URLs.
1 parent fb1daad commit 00cd19f

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

modules/context/repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ const (
479479
RepoRefTag
480480
// RepoRefCommit commit
481481
RepoRefCommit
482+
// RepoRefObject object
483+
RepoRefBlob
482484
)
483485

484486
// RepoRef handles repository reference names when the ref name is not
@@ -514,6 +516,9 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
514516
if refName := getRefName(ctx, RepoRefCommit); len(refName) > 0 {
515517
return refName
516518
}
519+
if refName := getRefName(ctx, RepoRefBlob); len(refName) > 0 {
520+
return refName
521+
}
517522
ctx.Repo.TreePath = path
518523
return ctx.Repo.Repository.DefaultBranch
519524
case RepoRefBranch:
@@ -526,6 +531,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
526531
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
527532
return parts[0]
528533
}
534+
case RepoRefBlob:
535+
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsBlobExist)
529536
default:
530537
log.Error(4, "Unrecognized path type: %v", path)
531538
}

routers/repo/download.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,19 @@ func SingleDownload(ctx *context.Context) {
6969
ctx.ServerError("ServeBlob", err)
7070
}
7171
}
72+
73+
// DownloadById download a file by sha1 ID
74+
func DownloadByID(ctx *context.Context) {
75+
blob, err := ctx.Repo.GitRepo.GetBlob(ctx.Params["sha"])
76+
if err != nil {
77+
if git.IsErrNotExist(err) {
78+
ctx.NotFound("GetBlob", nil)
79+
} else {
80+
ctx.ServerError("GetBlob", err)
81+
}
82+
return
83+
}
84+
if err = ServeBlob(ctx, blob); err != nil {
85+
ctx.ServerError("ServeBlob", err)
86+
}
87+
}

routers/routes/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ func RegisterRoutes(m *macaron.Macaron) {
678678
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownload)
679679
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownload)
680680
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownload)
681+
m.Get("/blob/:sha", context.RepoRefByType(context.RepoRefBlob), repo.DownloadByID)
681682
// "/*" route is deprecated, and kept for backward compatibility
682683
m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.SingleDownload)
683684
}, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode))

0 commit comments

Comments
 (0)