4
4
package backend
5
5
6
6
import (
7
- "bytes"
8
7
"context"
9
8
"encoding/base64"
10
9
"fmt"
@@ -29,7 +28,7 @@ var Capabilities = []string{
29
28
"locking" ,
30
29
}
31
30
32
- var _ transfer.Backend = & GiteaBackend {}
31
+ var _ transfer.Backend = ( * GiteaBackend )( nil )
33
32
34
33
// GiteaBackend is an adapter between git-lfs-transfer library and Gitea's internal LFS API
35
34
type GiteaBackend struct {
@@ -78,17 +77,17 @@ func (g *GiteaBackend) Batch(_ string, pointers []transfer.BatchItem, args trans
78
77
headerAccept : mimeGitLFS ,
79
78
headerContentType : mimeGitLFS ,
80
79
}
81
- req := newInternalRequest (g .ctx , url , http .MethodPost , headers , bodyBytes )
80
+ req := newInternalRequestLFS (g .ctx , url , http .MethodPost , headers , bodyBytes )
82
81
resp , err := req .Response ()
83
82
if err != nil {
84
83
g .logger .Log ("http request error" , err )
85
84
return nil , err
86
85
}
86
+ defer resp .Body .Close ()
87
87
if resp .StatusCode != http .StatusOK {
88
88
g .logger .Log ("http statuscode error" , resp .StatusCode , statusCodeToErr (resp .StatusCode ))
89
89
return nil , statusCodeToErr (resp .StatusCode )
90
90
}
91
- defer resp .Body .Close ()
92
91
respBytes , err := io .ReadAll (resp .Body )
93
92
if err != nil {
94
93
g .logger .Log ("http read error" , err )
@@ -158,8 +157,7 @@ func (g *GiteaBackend) Batch(_ string, pointers []transfer.BatchItem, args trans
158
157
return pointers , nil
159
158
}
160
159
161
- // Download implements transfer.Backend. The returned reader must be closed by the
162
- // caller.
160
+ // Download implements transfer.Backend. The returned reader must be closed by the caller.
163
161
func (g * GiteaBackend ) Download (oid string , args transfer.Args ) (io.ReadCloser , int64 , error ) {
164
162
idMapStr , exists := args [argID ]
165
163
if ! exists {
@@ -187,25 +185,25 @@ func (g *GiteaBackend) Download(oid string, args transfer.Args) (io.ReadCloser,
187
185
headerGiteaInternalAuth : g .internalAuth ,
188
186
headerAccept : mimeOctetStream ,
189
187
}
190
- req := newInternalRequest (g .ctx , url , http .MethodGet , headers , nil )
188
+ req := newInternalRequestLFS (g .ctx , url , http .MethodGet , headers , nil )
191
189
resp , err := req .Response ()
192
190
if err != nil {
193
- return nil , 0 , err
191
+ return nil , 0 , fmt . Errorf ( "failed to get response: %w" , err )
194
192
}
193
+ // no need to close the body here by "defer resp.Body.Close()", see below
195
194
if resp .StatusCode != http .StatusOK {
196
195
return nil , 0 , statusCodeToErr (resp .StatusCode )
197
196
}
198
- defer resp . Body . Close ()
199
- respBytes , err := io . ReadAll (resp .Body )
197
+
198
+ respSize , err := strconv . ParseInt (resp .Header . Get ( "X-Gitea-LFS-Content-Length" ), 10 , 64 )
200
199
if err != nil {
201
- return nil , 0 , err
200
+ return nil , 0 , fmt . Errorf ( "failed to parse content length: %w" , err )
202
201
}
203
- respSize := int64 (len (respBytes ))
204
- respBuf := io .NopCloser (bytes .NewBuffer (respBytes ))
205
- return respBuf , respSize , nil
202
+ // transfer.Backend will check io.Closer interface and close this Body reader
203
+ return resp .Body , respSize , nil
206
204
}
207
205
208
- // StartUpload implements transfer.Backend.
206
+ // Upload implements transfer.Backend.
209
207
func (g * GiteaBackend ) Upload (oid string , size int64 , r io.Reader , args transfer.Args ) error {
210
208
idMapStr , exists := args [argID ]
211
209
if ! exists {
@@ -234,15 +232,14 @@ func (g *GiteaBackend) Upload(oid string, size int64, r io.Reader, args transfer
234
232
headerContentType : mimeOctetStream ,
235
233
headerContentLength : strconv .FormatInt (size , 10 ),
236
234
}
237
- reqBytes , err := io .ReadAll (r )
238
- if err != nil {
239
- return err
240
- }
241
- req := newInternalRequest (g .ctx , url , http .MethodPut , headers , reqBytes )
235
+
236
+ req := newInternalRequestLFS (g .ctx , url , http .MethodPut , headers , nil )
237
+ req .Body (r )
242
238
resp , err := req .Response ()
243
239
if err != nil {
244
240
return err
245
241
}
242
+ defer resp .Body .Close ()
246
243
if resp .StatusCode != http .StatusOK {
247
244
return statusCodeToErr (resp .StatusCode )
248
245
}
@@ -284,11 +281,12 @@ func (g *GiteaBackend) Verify(oid string, size int64, args transfer.Args) (trans
284
281
headerAccept : mimeGitLFS ,
285
282
headerContentType : mimeGitLFS ,
286
283
}
287
- req := newInternalRequest (g .ctx , url , http .MethodPost , headers , bodyBytes )
284
+ req := newInternalRequestLFS (g .ctx , url , http .MethodPost , headers , bodyBytes )
288
285
resp , err := req .Response ()
289
286
if err != nil {
290
287
return transfer .NewStatus (transfer .StatusInternalServerError ), err
291
288
}
289
+ defer resp .Body .Close ()
292
290
if resp .StatusCode != http .StatusOK {
293
291
return transfer .NewStatus (uint32 (resp .StatusCode ), http .StatusText (resp .StatusCode )), statusCodeToErr (resp .StatusCode )
294
292
}
0 commit comments