Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

transferadapter.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package lfs
  5. import (
  6. "context"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "net/http"
  11. )
  12. // TransferAdapter represents an adapter for downloading/uploading LFS objects
  13. type TransferAdapter interface {
  14. Name() string
  15. Download(ctx context.Context, r *ObjectResponse) (io.ReadCloser, error)
  16. //Upload(ctx context.Context, reader io.Reader) error
  17. }
  18. // BasicTransferAdapter implements the "basic" adapter
  19. type BasicTransferAdapter struct {
  20. client *http.Client
  21. }
  22. // Name returns the name of the adapter
  23. func (a *BasicTransferAdapter) Name() string {
  24. return "basic"
  25. }
  26. // Download reads the download location and downloads the data
  27. func (a *BasicTransferAdapter) Download(ctx context.Context, r *ObjectResponse) (io.ReadCloser, error) {
  28. download, ok := r.Actions["download"]
  29. if !ok {
  30. return nil, errors.New("lfs.BasicTransferAdapter.Download: Action 'download' not found")
  31. }
  32. req, err := http.NewRequestWithContext(ctx, "GET", download.Href, nil)
  33. if err != nil {
  34. return nil, fmt.Errorf("lfs.BasicTransferAdapter.Download http.NewRequestWithContext: %w", err)
  35. }
  36. for key, value := range download.Header {
  37. req.Header.Set(key, value)
  38. }
  39. res, err := a.client.Do(req)
  40. if err != nil {
  41. select {
  42. case <-ctx.Done():
  43. return nil, ctx.Err()
  44. default:
  45. }
  46. return nil, fmt.Errorf("lfs.BasicTransferAdapter.Download http.Do: %w", err)
  47. }
  48. return res.Body, nil
  49. }