Skip to content

Fix profile render when the README.md size is larger than 1024 bytes #25131

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

Merged
merged 13 commits into from
Jun 13, 2023
15 changes: 11 additions & 4 deletions modules/git/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ func (b *Blob) Name() string {
}

// GetBlobContent Gets the content of the blob as raw text
func (b *Blob) GetBlobContent() (string, error) {
// limit=0 means no limit, limit>0 means limit the content length
func (b *Blob) GetBlobContent(limit int64) (string, error) {
dataRc, err := b.DataAsync()
if err != nil {
return "", err
}
defer dataRc.Close()
buf := make([]byte, 1024)
n, _ := util.ReadAtMost(dataRc, buf)
buf = buf[:n]

var buf []byte
if limit == 0 {
buf, _ = io.ReadAll(dataRc)
} else {
buf = make([]byte, limit)
n, _ := util.ReadAtMost(dataRc, buf)
buf = buf[:n]
}
return string(buf), nil
}

Expand Down
2 changes: 1 addition & 1 deletion routers/web/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func Profile(ctx *context.Context) {
}
blob, err := commit.GetBlobByPath("README.md")
if err == nil {
bytes, err := blob.GetBlobContent()
bytes, err := blob.GetBlobContent(0)
if err != nil {
ctx.ServerError("GetBlobContent", err)
return
Expand Down
2 changes: 1 addition & 1 deletion services/repository/files/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
} else if entry.IsLink() {
contentsResponse.Type = string(ContentTypeLink)
// The target of a symlink file is the content of the file
targetFromContent, err := entry.Blob().GetBlobContent()
targetFromContent, err := entry.Blob().GetBlobContent(1024)
Copy link
Member

@silverwind silverwind Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should investigate closer what this does, but does not have to be in this PR. It might be that this is another undiscovered bug and it should actually use 0 here.

if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/api_packages_cargo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func testPackageCargo(t *testing.T, _ *neturl.URL) {
blob, err := commit.GetBlobByPath(path)
assert.NoError(t, err)

content, err := blob.GetBlobContent()
content, err := blob.GetBlobContent(1024)
assert.NoError(t, err)

return content
Expand Down