Skip to content

Commit fcf459d

Browse files
committed
add context to Size
1 parent 960a844 commit fcf459d

File tree

22 files changed

+52
-48
lines changed

22 files changed

+52
-48
lines changed

modules/git/blob_gogit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package git
88

99
import (
10+
"context"
1011
"io"
1112

1213
"github.com/go-git/go-git/v5/plumbing"
@@ -27,6 +28,6 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) {
2728
}
2829

2930
// Size returns the uncompressed size of the blob
30-
func (b *Blob) Size() int64 {
31+
func (b *Blob) Size(_ context.Context) int64 {
3132
return b.gogitEncodedObj.Size()
3233
}

modules/git/blob_nogogit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package git
88
import (
99
"bufio"
1010
"bytes"
11+
"context"
1112
"io"
1213

1314
"code.gitea.io/gitea/modules/log"
@@ -62,12 +63,12 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) {
6263
}
6364

6465
// Size returns the uncompressed size of the blob
65-
func (b *Blob) Size() int64 {
66+
func (b *Blob) Size(ctx context.Context) int64 {
6667
if b.gotSize {
6768
return b.size
6869
}
6970

70-
wr, rd, cancel, err := b.repo.CatFileBatchCheck(b.repo.Ctx)
71+
wr, rd, cancel, err := b.repo.CatFileBatchCheck(ctx)
7172
if err != nil {
7273
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
7374
return 0

modules/issue/template/unmarshal.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package template
55

66
import (
7+
"context"
78
"fmt"
89
"io"
910
"path"
@@ -42,31 +43,31 @@ func Unmarshal(filename string, content []byte) (*api.IssueTemplate, error) {
4243
}
4344

4445
// UnmarshalFromEntry parses out a valid template from the blob in entry
45-
func UnmarshalFromEntry(entry *git.TreeEntry, dir string) (*api.IssueTemplate, error) {
46-
return unmarshalFromEntry(entry, path.Join(dir, entry.Name())) // Filepaths in Git are ALWAYS '/' separated do not use filepath here
46+
func UnmarshalFromEntry(ctx context.Context, entry *git.TreeEntry, dir string) (*api.IssueTemplate, error) {
47+
return unmarshalFromEntry(ctx, entry, path.Join(dir, entry.Name())) // Filepaths in Git are ALWAYS '/' separated do not use filepath here
4748
}
4849

4950
// UnmarshalFromCommit parses out a valid template from the commit
50-
func UnmarshalFromCommit(commit *git.Commit, filename string) (*api.IssueTemplate, error) {
51+
func UnmarshalFromCommit(ctx context.Context, commit *git.Commit, filename string) (*api.IssueTemplate, error) {
5152
entry, err := commit.GetTreeEntryByPath(filename)
5253
if err != nil {
5354
return nil, fmt.Errorf("get entry for %q: %w", filename, err)
5455
}
55-
return unmarshalFromEntry(entry, filename)
56+
return unmarshalFromEntry(ctx, entry, filename)
5657
}
5758

5859
// UnmarshalFromRepo parses out a valid template from the head commit of the branch
59-
func UnmarshalFromRepo(repo *git.Repository, branch, filename string) (*api.IssueTemplate, error) {
60+
func UnmarshalFromRepo(ctx context.Context, repo *git.Repository, branch, filename string) (*api.IssueTemplate, error) {
6061
commit, err := repo.GetBranchCommit(branch)
6162
if err != nil {
6263
return nil, fmt.Errorf("get commit on branch %q: %w", branch, err)
6364
}
6465

65-
return UnmarshalFromCommit(commit, filename)
66+
return UnmarshalFromCommit(ctx, commit, filename)
6667
}
6768

68-
func unmarshalFromEntry(entry *git.TreeEntry, filename string) (*api.IssueTemplate, error) {
69-
if size := entry.Blob().Size(); size > setting.UI.MaxDisplayFileSize {
69+
func unmarshalFromEntry(ctx context.Context, entry *git.TreeEntry, filename string) (*api.IssueTemplate, error) {
70+
if size := entry.Blob().Size(ctx); size > setting.UI.MaxDisplayFileSize {
7071
return nil, fmt.Errorf("too large: %v > MaxDisplayFileSize", size)
7172
}
7273

routers/api/v1/repo/file.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) {
137137
ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry)))
138138

139139
// LFS Pointer files are at most 1024 bytes - so any blob greater than 1024 bytes cannot be an LFS file
140-
if blob.Size() > 1024 {
140+
if blob.Size(ctx) > 1024 {
141141
// First handle caching for the blob
142142
if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
143143
return
@@ -180,7 +180,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) {
180180
}
181181

182182
// OK not cached - serve!
183-
common.ServeContentByReader(ctx.Base, ctx.Repo.TreePath, blob.Size(), bytes.NewReader(buf))
183+
common.ServeContentByReader(ctx.Base, ctx.Repo.TreePath, blob.Size(ctx), bytes.NewReader(buf))
184184
return
185185
}
186186

@@ -194,7 +194,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) {
194194
return
195195
}
196196

197-
common.ServeContentByReader(ctx.Base, ctx.Repo.TreePath, blob.Size(), bytes.NewReader(buf))
197+
common.ServeContentByReader(ctx.Base, ctx.Repo.TreePath, blob.Size(ctx), bytes.NewReader(buf))
198198
return
199199
} else if err != nil {
200200
ctx.APIErrorInternal(err)
@@ -384,7 +384,7 @@ func GetEditorconfig(ctx *context.APIContext) {
384384
// "404":
385385
// "$ref": "#/responses/notFound"
386386

387-
ec, _, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
387+
ec, _, err := ctx.Repo.GetEditorconfig(ctx, ctx.Repo.Commit)
388388
if err != nil {
389389
if git.IsErrNotExist(err) {
390390
ctx.APIErrorNotFound(err)

routers/api/v1/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ func GetIssueTemplates(ctx *context.APIContext) {
12001200
// "$ref": "#/responses/IssueTemplates"
12011201
// "404":
12021202
// "$ref": "#/responses/notFound"
1203-
ret := issue.ParseTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo)
1203+
ret := issue.ParseTemplatesFromDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo)
12041204
if cnt := len(ret.TemplateErrors); cnt != 0 {
12051205
ctx.Resp.Header().Add("X-Gitea-Warning", "error occurs when parsing issue template: count="+strconv.Itoa(cnt))
12061206
}

routers/api/v1/repo/wiki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit)
502502
// given tree entry, encoded with base64. Writes to ctx if an error occurs.
503503
func wikiContentsByEntry(ctx *context.APIContext, entry *git.TreeEntry) string {
504504
blob := entry.Blob()
505-
if blob.Size() > setting.API.DefaultMaxBlobSize {
505+
if blob.Size(ctx) > setting.API.DefaultMaxBlobSize {
506506
return ""
507507
}
508508
content, err := blob.GetBlobContentBase64()

routers/common/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func ServeBlob(ctx *context.Base, repo *repo_model.Repository, filePath string,
3535
}()
3636

3737
_ = repo.LoadOwner(ctx)
38-
httplib.ServeContentByReader(ctx.Req, ctx.Resp, blob.Size(), dataRc, &httplib.ServeHeaderOptions{
38+
httplib.ServeContentByReader(ctx.Req, ctx.Resp, blob.Size(ctx), dataRc, &httplib.ServeHeaderOptions{
3939
Filename: path.Base(filePath),
4040
CacheIsPublic: !repo.IsPrivate && repo.Owner != nil && repo.Owner.Visibility == structs.VisibleTypePublic,
4141
CacheDuration: setting.StaticCacheTime,

routers/web/repo/blame.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func RefBlame(ctx *context.Context) {
6767
ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
6868

6969
blob := entry.Blob()
70-
fileSize := blob.Size()
70+
fileSize := blob.Size(ctx)
7171
ctx.Data["FileSize"] = fileSize
7272
ctx.Data["FileName"] = blob.Name()
7373

routers/web/repo/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func setCsvCompareContext(ctx *context.Context) {
137137
return nil, nil, nil
138138
}
139139

140-
if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < blob.Size() {
140+
if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < blob.Size(ctx) {
141141
return nil, nil, errTooLarge
142142
}
143143

routers/web/repo/editor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
146146
}
147147

148148
blob := entry.Blob()
149-
if blob.Size() >= setting.UI.MaxDisplayFileSize {
149+
if blob.Size(ctx) >= setting.UI.MaxDisplayFileSize {
150150
ctx.NotFound(err)
151151
return
152152
}
@@ -159,7 +159,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
159159

160160
defer dataRc.Close()
161161

162-
ctx.Data["FileSize"] = blob.Size()
162+
ctx.Data["FileSize"] = blob.Size(ctx)
163163
ctx.Data["FileName"] = blob.Name()
164164

165165
buf := make([]byte, 1024)
@@ -201,7 +201,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
201201

202202
// GetEditorConfig returns a editorconfig JSON string for given treePath or "null"
203203
func GetEditorConfig(ctx *context.Context, treePath string) string {
204-
ec, _, err := ctx.Repo.GetEditorconfig()
204+
ec, _, err := ctx.Repo.GetEditorconfig(ctx)
205205
if err == nil {
206206
def, err := ec.GetDefinitionForFilename(treePath)
207207
if err == nil {

0 commit comments

Comments
 (0)