Skip to content

Commit d48f05d

Browse files
committed
CB/bp: Add "X-Gitea-Object-Type" header for GET /raw/ & /media/ API (go-gitea#20438)
1 parent 14d28d1 commit d48f05d

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

integrations/api_repo_raw_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
"code.gitea.io/gitea/models/unittest"
1212
user_model "code.gitea.io/gitea/models/user"
13+
14+
"github.com/stretchr/testify/assert"
1315
)
1416

1517
func TestAPIReposRaw(t *testing.T) {
@@ -25,9 +27,11 @@ func TestAPIReposRaw(t *testing.T) {
2527
"65f1bf27bc3bf70f64657658635e66094edbcb4d", // Commit
2628
} {
2729
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/raw/%s/README.md?token="+token, user.Name, ref)
28-
session.MakeRequest(t, req, http.StatusOK)
30+
resp := session.MakeRequest(t, req, http.StatusOK)
31+
assert.EqualValues(t, "file", resp.Header().Get("x-gitea-object-type"))
2932
}
3033
// Test default branch
3134
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/raw/README.md?token="+token, user.Name)
32-
session.MakeRequest(t, req, http.StatusOK)
35+
resp := session.MakeRequest(t, req, http.StatusOK)
36+
assert.EqualValues(t, "file", resp.Header().Get("x-gitea-object-type"))
3337
}

routers/api/v1/repo/file.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
files_service "code.gitea.io/gitea/services/repository/files"
3434
)
3535

36+
const giteaObjectTypeHeader = "X-Gitea-Object-Type"
37+
3638
// GetRawFile get a file by path on a repository
3739
func GetRawFile(ctx *context.APIContext) {
3840
// swagger:operation GET /repos/{owner}/{repo}/raw/{filepath} repository repoGetRawFile
@@ -72,11 +74,13 @@ func GetRawFile(ctx *context.APIContext) {
7274
return
7375
}
7476

75-
blob, lastModified := getBlobForEntry(ctx)
77+
blob, entry, lastModified := getBlobForEntry(ctx)
7678
if ctx.Written() {
7779
return
7880
}
7981

82+
ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry)))
83+
8084
if err := common.ServeBlob(ctx.Context, blob, lastModified); err != nil {
8185
ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
8286
}
@@ -119,11 +123,13 @@ func GetRawFileOrLFS(ctx *context.APIContext) {
119123
return
120124
}
121125

122-
blob, lastModified := getBlobForEntry(ctx)
126+
blob, entry, lastModified := getBlobForEntry(ctx)
123127
if ctx.Written() {
124128
return
125129
}
126130

131+
ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry)))
132+
127133
// LFS Pointer files are at most 1024 bytes - so any blob greater than 1024 bytes cannot be an LFS file
128134
if blob.Size() > 1024 {
129135
// First handle caching for the blob
@@ -218,7 +224,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) {
218224
}
219225
}
220226

221-
func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, lastModified time.Time) {
227+
func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEntry, lastModified time.Time) {
222228
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
223229
if err != nil {
224230
if git.IsErrNotExist(err) {
@@ -251,7 +257,7 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, lastModified time
251257
}
252258
blob = entry.Blob()
253259

254-
return
260+
return blob, entry, lastModified
255261
}
256262

257263
// GetArchive get archive of a repository

services/repository/files/content.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePat
101101
return fileList, nil
102102
}
103103

104+
// GetObjectTypeFromTreeEntry check what content is behind it
105+
func GetObjectTypeFromTreeEntry(entry *git.TreeEntry) ContentType {
106+
switch {
107+
case entry.IsDir():
108+
return ContentTypeDir
109+
case entry.IsSubModule():
110+
return ContentTypeSubmodule
111+
case entry.IsExecutable(), entry.IsRegular():
112+
return ContentTypeRegular
113+
case entry.IsLink():
114+
return ContentTypeLink
115+
default:
116+
return ""
117+
}
118+
}
119+
104120
// GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag
105121
func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
106122
if ref == "" {

0 commit comments

Comments
 (0)