You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filesystem_client.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. "io"
  8. "net/url"
  9. "os"
  10. "path/filepath"
  11. "code.gitea.io/gitea/modules/util"
  12. )
  13. // FilesystemClient is used to read LFS data from a filesystem path
  14. type FilesystemClient struct {
  15. lfsdir string
  16. }
  17. func newFilesystemClient(endpoint *url.URL) *FilesystemClient {
  18. path, _ := util.FileURLToPath(endpoint)
  19. lfsdir := filepath.Join(path, "lfs", "objects")
  20. client := &FilesystemClient{lfsdir}
  21. return client
  22. }
  23. func (c *FilesystemClient) objectPath(oid string) string {
  24. return filepath.Join(c.lfsdir, oid[0:2], oid[2:4], oid)
  25. }
  26. // Download reads the specific LFS object from the target repository
  27. func (c *FilesystemClient) Download(ctx context.Context, oid string, size int64) (io.ReadCloser, error) {
  28. objectPath := c.objectPath(oid)
  29. if _, err := os.Stat(objectPath); os.IsNotExist(err) {
  30. return nil, err
  31. }
  32. file, err := os.Open(objectPath)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return file, nil
  37. }