render plain text file if the LFS object doesn't exist (#31812)

We had an issue where a repo was using LFS to store a file, but the user
did not push the file. When trying to view the file, Gitea returned a
500 HTTP status code referencing `ErrLFSObjectNotExist`. It appears the
intent was the render this file as plain text, but the conditional was
flipped. I've also added a test to verify that the file is rendered as
plain text.

(cherry picked from commit 1310649331648d747c57a52ea3bc92da85e7d4d1)

Conflicts:
	tests/integration/lfs_view_test.go
  trivial context conflict
This commit is contained in:
Rowan Bohde 2024-08-14 16:50:09 -05:00 committed by Earl Warren
parent 7f1db1df3e
commit ebfdc659d8
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
7 changed files with 18 additions and 5 deletions

View file

@ -239,14 +239,12 @@ func getFileReader(ctx gocontext.Context, repoID int64, blob *git.Blob) ([]byte,
}
meta, err := git_model.GetLFSMetaObjectByOid(ctx, repoID, pointer.Oid)
if err != nil && err != git_model.ErrLFSObjectNotExist { // fallback to plain file
if err != nil { // fallback to plain file
log.Warn("Unable to access LFS pointer %s in repo %d: %v", pointer.Oid, repoID, err)
return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil
}
dataRc.Close()
if err != nil {
return nil, nil, nil, err
}
dataRc, err = lfs.ReadMetaObject(pointer)
if err != nil {

View file

@ -0,0 +1,2 @@
xKÊÉOR0´0`pö÷ òt
ñôs×ËMQHËÌ)I-²ÍI+VHÉLK3rSÒSÁ,Ý’ÔŠ.-½¬t"U&eæ¥23¯,1'“8ûØæAÅ

View file

@ -1 +1 @@
73cf03db6ece34e12bf91e8853dc58f678f2f82d
e9c32647bab825977942598c0efa415de300304b

View file

@ -111,6 +111,19 @@ func TestLFSRender(t *testing.T) {
doc := NewHTMLParser(t, resp.Body).doc
assert.Equal(t, 1, doc.Find(`.sha.label[href="/user2/lfs/commit/73cf03db6ece34e12bf91e8853dc58f678f2f82d"]`).Length(), "could not find link to commit")
})
// check that an invalid lfs entry defaults to plaintext
t.Run("Invalid", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/invalid")
resp := session.MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body).doc
content := doc.Find("div.file-view").Text()
assert.Contains(t, content, "oid sha256:9d178b5f15046343fd32f451df93acc2bdd9e6373be478b968e4cad6b6647351")
})
}
// TestLFSLockView tests the LFS lock view on settings page of repositories