summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2022-07-21 21:18:41 +0200
committerGitHub <noreply@github.com>2022-07-21 21:18:41 +0200
commit0a97480934fc2082c9cff18a5b66cedc12575919 (patch)
tree8f756dd6e2abaefd9909e3e438b22b8ecae292d6 /routers
parent7690de56f7bdcc5065af2c9478e8572d318a84f3 (diff)
downloadgitea-0a97480934fc2082c9cff18a5b66cedc12575919.tar.gz
gitea-0a97480934fc2082c9cff18a5b66cedc12575919.zip
Add "X-Gitea-Object-Type" header for GET `/raw/` & `/media/` API (#20438)
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/repo/file.go14
1 files changed, 10 insertions, 4 deletions
diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go
index 1ac1088839..ba8a938b83 100644
--- a/routers/api/v1/repo/file.go
+++ b/routers/api/v1/repo/file.go
@@ -33,6 +33,8 @@ import (
files_service "code.gitea.io/gitea/services/repository/files"
)
+const giteaObjectTypeHeader = "X-Gitea-Object-Type"
+
// GetRawFile get a file by path on a repository
func GetRawFile(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/raw/{filepath} repository repoGetRawFile
@@ -72,11 +74,13 @@ func GetRawFile(ctx *context.APIContext) {
return
}
- blob, lastModified := getBlobForEntry(ctx)
+ blob, entry, lastModified := getBlobForEntry(ctx)
if ctx.Written() {
return
}
+ ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry)))
+
if err := common.ServeBlob(ctx.Context, blob, lastModified); err != nil {
ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
}
@@ -119,11 +123,13 @@ func GetRawFileOrLFS(ctx *context.APIContext) {
return
}
- blob, lastModified := getBlobForEntry(ctx)
+ blob, entry, lastModified := getBlobForEntry(ctx)
if ctx.Written() {
return
}
+ ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry)))
+
// LFS Pointer files are at most 1024 bytes - so any blob greater than 1024 bytes cannot be an LFS file
if blob.Size() > 1024 {
// First handle caching for the blob
@@ -218,7 +224,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) {
}
}
-func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, lastModified time.Time) {
+func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEntry, lastModified time.Time) {
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
if err != nil {
if git.IsErrNotExist(err) {
@@ -251,7 +257,7 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, lastModified time
}
blob = entry.Blob()
- return blob, lastModified
+ return blob, entry, lastModified
}
// GetArchive get archive of a repository