Files

90 lines
2.3 KiB
Go
Raw Permalink Normal View History

2021-04-09 00:25:57 +02:00
// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2021-04-09 00:25:57 +02:00
package lfs
import (
"bytes"
2021-04-09 00:25:57 +02:00
"context"
"io"
"net/http"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
2021-04-09 00:25:57 +02:00
)
2023-09-18 04:40:50 -04:00
// TransferAdapter represents an adapter for downloading/uploading LFS objects.
2021-04-09 00:25:57 +02:00
type TransferAdapter interface {
Name() string
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-09 00:25:57 +02:00
}
2023-09-18 04:40:50 -04:00
// BasicTransferAdapter implements the "basic" adapter.
2021-04-09 00:25:57 +02:00
type BasicTransferAdapter struct {
client *http.Client
}
2023-09-18 04:40:50 -04:00
// Name returns the name of the adapter.
2021-04-09 00:25:57 +02:00
func (a *BasicTransferAdapter) Name() string {
return "basic"
}
2023-09-18 04:40:50 -04:00
// Download reads the download location and downloads the data.
func (a *BasicTransferAdapter) Download(ctx context.Context, l *Link) (io.ReadCloser, error) {
2023-09-18 04:40:50 -04:00
req, err := createRequest(ctx, http.MethodGet, l.Href, l.Header, nil)
if err != nil {
return nil, err
}
log.Debug("Download Request: %+v", req)
2023-09-18 04:40:50 -04:00
resp, err := performRequest(ctx, a.client, req)
if err != nil {
return nil, err
2021-04-09 00:25:57 +02:00
}
return resp.Body, nil
}
2021-04-09 00:25:57 +02:00
2023-09-18 04:40:50 -04:00
// Upload sends the content to the LFS server.
func (a *BasicTransferAdapter) Upload(ctx context.Context, l *Link, p Pointer, r io.Reader) error {
2023-09-18 04:40:50 -04: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
2023-09-18 04:40:50 -04:00
res, err := performRequest(ctx, a.client, req)
2021-04-09 00:25:57 +02:00
if err != nil {
return err
2021-04-09 00:25:57 +02:00
}
2023-09-18 04:40:50 -04:00
defer res.Body.Close()
return nil
}
// Verify calls the verify handler on the LFS server
func (a *BasicTransferAdapter) Verify(ctx context.Context, l *Link, p Pointer) error {
b, err := json.Marshal(p)
if err != nil {
log.Error("Error encoding json: %v", err)
return err
}
2023-09-18 04:40:50 -04:00
req, err := createRequest(ctx, http.MethodPost, l.Href, l.Header, bytes.NewReader(b))
if err != nil {
return err
}
2023-09-18 04:40:50 -04:00
req.Header.Set("Content-Type", MediaType)
res, err := performRequest(ctx, a.client, req)
2021-04-09 00:25:57 +02:00
if err != nil {
2023-09-18 04:40:50 -04:00
return err
}
2023-09-18 04:40:50 -04:00
defer res.Body.Close()
return nil
2021-04-09 00:25:57 +02:00
}