Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "X-Gitea-Object-Type" header for GET /raw/ & /media/ API #20438

Merged
merged 5 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions integrations/api_repo_raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"

"github.com/stretchr/testify/assert"
)

func TestAPIReposRaw(t *testing.T) {
Expand All @@ -25,9 +27,11 @@ func TestAPIReposRaw(t *testing.T) {
"65f1bf27bc3bf70f64657658635e66094edbcb4d", // Commit
} {
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/raw/%s/README.md?token="+token, user.Name, ref)
session.MakeRequest(t, req, http.StatusOK)
resp := session.MakeRequest(t, req, http.StatusOK)
assert.EqualValues(t, "file", resp.Header().Get("x-gitea-object-type"))
}
// Test default branch
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/raw/README.md?token="+token, user.Name)
session.MakeRequest(t, req, http.StatusOK)
resp := session.MakeRequest(t, req, http.StatusOK)
assert.EqualValues(t, "file", resp.Header().Get("x-gitea-object-type"))
}
18 changes: 14 additions & 4 deletions routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import (
files_service "code.gitea.io/gitea/services/repository/files"
)

const (
giteaObjectTypeHeader = "X-Gitea-Object-Type"
)
6543 marked this conversation as resolved.
Show resolved Hide resolved

// GetRawFile get a file by path on a repository
func GetRawFile(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/raw/{filepath} repository repoGetRawFile
Expand Down Expand Up @@ -72,11 +76,14 @@ 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.GetContentTypeFromTreeEntry(entry)))

if err := common.ServeBlob(ctx.Context, blob, lastModified); err != nil {
ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
}
Expand Down Expand Up @@ -119,11 +126,14 @@ 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.GetContentTypeFromTreeEntry(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
Expand Down Expand Up @@ -218,7 +228,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) {
Expand Down Expand Up @@ -251,7 +261,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
Expand Down
16 changes: 16 additions & 0 deletions services/repository/files/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePat
return fileList, nil
}

// GetContentTypeFromTreeEntry check what content is behind it
func GetContentTypeFromTreeEntry(entry *git.TreeEntry) ContentType {
6543 marked this conversation as resolved.
Show resolved Hide resolved
switch {
case entry.IsDir():
return ContentTypeDir
case entry.IsSubModule():
return ContentTypeSubmodule
case entry.IsExecutable(), entry.IsRegular():
return ContentTypeRegular
case entry.IsLink():
return ContentTypeLink
6543 marked this conversation as resolved.
Show resolved Hide resolved
default:
return ""
}
}

// GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag
func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
if ref == "" {
Expand Down