diff options
author | vincent <pulltheflower@163.com> | 2024-02-19 22:50:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-19 14:50:03 +0000 |
commit | 35d5e4aea4bb02a0b4c7b38ecb2acf612151e891 (patch) | |
tree | 31875953d9d4f26e33126c78cfc9b430f84195d6 | |
parent | 217d71c48a10265e08b95cc961656b921f61f9ff (diff) | |
download | gitea-35d5e4aea4bb02a0b4c7b38ecb2acf612151e891.tar.gz gitea-35d5e4aea4bb02a0b4c7b38ecb2acf612151e891.zip |
Fix content size does not match error when uploading lfs file (#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.
-rw-r--r-- | routers/api/v1/repo/file.go | 2 | ||||
-rw-r--r-- | services/repository/files/update.go | 6 |
2 files changed, 6 insertions, 2 deletions
diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 370e4753f3..317213c946 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -408,7 +408,7 @@ func canReadFiles(r *context.Repository) bool { return r.Permission.CanRead(unit.TypeCode) } -func base64Reader(s string) (io.Reader, error) { +func base64Reader(s string) (io.ReadSeeker, error) { b, err := base64.StdEncoding.DecodeString(s) if err != nil { return nil, err diff --git a/services/repository/files/update.go b/services/repository/files/update.go index f223daf3a9..4f7178184b 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -40,7 +40,7 @@ type ChangeRepoFile struct { Operation string TreePath string FromTreePath string - ContentReader io.Reader + ContentReader io.ReadSeeker SHA string Options *RepoFileOptions } @@ -448,6 +448,10 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file return err } if !exist { + _, err := file.ContentReader.Seek(0, io.SeekStart) + if err != nil { + return err + } if err := contentStore.Put(lfsMetaObject.Pointer, file.ContentReader); err != nil { if _, err2 := git_model.RemoveLFSMetaObjectByOid(ctx, repoID, lfsMetaObject.Oid); err2 != nil { return fmt.Errorf("unable to remove failed inserted LFS object %s: %v (Prev Error: %w)", lfsMetaObject.Oid, err2, err) |