2021-04-08 22:25:57 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-04-08 22:25:57 +00:00
|
|
|
|
|
|
|
package lfs
|
|
|
|
|
|
|
|
import (
|
2021-06-14 17:20:43 +00:00
|
|
|
"bytes"
|
2021-04-08 22:25:57 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2021-06-14 17:20:43 +00:00
|
|
|
|
2021-07-24 16:03:58 +00:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2021-06-14 17:20:43 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-04-08 22:25:57 +00:00
|
|
|
)
|
|
|
|
|
2023-09-18 08:40:50 +00:00
|
|
|
// TransferAdapter represents an adapter for downloading/uploading LFS objects.
|
2021-04-08 22:25:57 +00:00
|
|
|
type TransferAdapter interface {
|
|
|
|
Name() string
|
2021-06-14 17:20:43 +00:00
|
|
|
Download(ctx context.Context, l *Link) (io.ReadCloser, error)
|
|
|
|
Upload(ctx context.Context, l *Link, p Pointer, r io.Reader) error
|
|
|
|
Verify(ctx context.Context, l *Link, p Pointer) error
|
2021-04-08 22:25:57 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 08:40:50 +00:00
|
|
|
// BasicTransferAdapter implements the "basic" adapter.
|
2021-04-08 22:25:57 +00:00
|
|
|
type BasicTransferAdapter struct {
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
2023-09-18 08:40:50 +00:00
|
|
|
// Name returns the name of the adapter.
|
2021-04-08 22:25:57 +00:00
|
|
|
func (a *BasicTransferAdapter) Name() string {
|
|
|
|
return "basic"
|
|
|
|
}
|
|
|
|
|
2023-09-18 08:40:50 +00:00
|
|
|
// Download reads the download location and downloads the data.
|
2021-06-14 17:20:43 +00:00
|
|
|
func (a *BasicTransferAdapter) Download(ctx context.Context, l *Link) (io.ReadCloser, error) {
|
2023-09-18 08:40:50 +00:00
|
|
|
req, err := createRequest(ctx, http.MethodGet, l.Href, l.Header, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err := performRequest(ctx, a.client, req)
|
2021-06-14 17:20:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-04-08 22:25:57 +00:00
|
|
|
}
|
2021-06-14 17:20:43 +00:00
|
|
|
return resp.Body, nil
|
|
|
|
}
|
2021-04-08 22:25:57 +00:00
|
|
|
|
2023-09-18 08:40:50 +00:00
|
|
|
// Upload sends the content to the LFS server.
|
2021-06-14 17:20:43 +00:00
|
|
|
func (a *BasicTransferAdapter) Upload(ctx context.Context, l *Link, p Pointer, r io.Reader) error {
|
2023-09-18 08:40:50 +00:00
|
|
|
req, err := createRequest(ctx, http.MethodPut, l.Href, l.Header, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if req.Header.Get("Content-Type") == "" {
|
|
|
|
req.Header.Set("Content-Type", "application/octet-stream")
|
|
|
|
}
|
|
|
|
if req.Header.Get("Transfer-Encoding") == "chunked" {
|
|
|
|
req.TransferEncoding = []string{"chunked"}
|
|
|
|
}
|
|
|
|
req.ContentLength = p.Size
|
2021-06-14 17:20:43 +00:00
|
|
|
|
2023-09-18 08:40:50 +00:00
|
|
|
res, err := performRequest(ctx, a.client, req)
|
2021-04-08 22:25:57 +00:00
|
|
|
if err != nil {
|
2021-06-14 17:20:43 +00:00
|
|
|
return err
|
2021-04-08 22:25:57 +00:00
|
|
|
}
|
2023-09-18 08:40:50 +00:00
|
|
|
defer res.Body.Close()
|
2021-06-14 17:20:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify calls the verify handler on the LFS server
|
|
|
|
func (a *BasicTransferAdapter) Verify(ctx context.Context, l *Link, p Pointer) error {
|
2021-07-24 16:03:58 +00:00
|
|
|
b, err := json.Marshal(p)
|
2021-06-14 17:20:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error encoding json: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-18 08:40:50 +00:00
|
|
|
req, err := createRequest(ctx, http.MethodPost, l.Href, l.Header, bytes.NewReader(b))
|
2021-06-14 17:20:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-18 08:40:50 +00:00
|
|
|
req.Header.Set("Content-Type", MediaType)
|
|
|
|
res, err := performRequest(ctx, a.client, req)
|
2021-04-08 22:25:57 +00:00
|
|
|
if err != nil {
|
2023-09-18 08:40:50 +00:00
|
|
|
return err
|
2021-06-14 17:20:43 +00:00
|
|
|
}
|
2023-09-18 08:40:50 +00:00
|
|
|
defer res.Body.Close()
|
|
|
|
return nil
|
2021-04-08 22:25:57 +00:00
|
|
|
}
|