Skip to content

Commit 20cbb4e

Browse files
pulltheflowersilverwind
authored andcommitted
Fix content size does not match error when uploading lfs file (go-gitea#29259)
![image](https://github.com/go-gitea/gitea/assets/38434877/cd726b4d-4771-4547-8aee-ae4e4b56b1d1) When we update an lfs file by API `api/v1/repos/{owner}/{repo}/contents/{filepath}`, there will show an error ```json { "message": "Put \"http://localhost:9000/gitea/lfs/38/92/05904d6c7bb83fc676513911226f2be25bf1465616bb9b29587100ab1414\": readfrom tcp [::1]:57300->[::1]:9000: content size does not match", "url": "http://localhost:3000/api/swagger" } ``` The reason of this error is https://github.com/go-gitea/gitea/blob/main/services/repository/files/update.go, in this file, the `file.ContentReader` been used twice. So when use `file.ContentReader` in the second time, the `i` of this Reader has been updated to the length of the content. it will return 0 and an `io.EOF` error when we try to read cotent from this Reader.
1 parent a94bf6a commit 20cbb4e

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

routers/api/v1/repo/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ func canReadFiles(r *context.Repository) bool {
408408
return r.Permission.CanRead(unit.TypeCode)
409409
}
410410

411-
func base64Reader(s string) (io.Reader, error) {
411+
func base64Reader(s string) (io.ReadSeeker, error) {
412412
b, err := base64.StdEncoding.DecodeString(s)
413413
if err != nil {
414414
return nil, err

services/repository/files/update.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type ChangeRepoFile struct {
4040
Operation string
4141
TreePath string
4242
FromTreePath string
43-
ContentReader io.Reader
43+
ContentReader io.ReadSeeker
4444
SHA string
4545
Options *RepoFileOptions
4646
}
@@ -448,6 +448,10 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file
448448
return err
449449
}
450450
if !exist {
451+
_, err := file.ContentReader.Seek(0, io.SeekStart)
452+
if err != nil {
453+
return err
454+
}
451455
if err := contentStore.Put(lfsMetaObject.Pointer, file.ContentReader); err != nil {
452456
if _, err2 := git_model.RemoveLFSMetaObjectByOid(ctx, repoID, lfsMetaObject.Oid); err2 != nil {
453457
return fmt.Errorf("unable to remove failed inserted LFS object %s: %v (Prev Error: %w)", lfsMetaObject.Oid, err2, err)

0 commit comments

Comments
 (0)