Skip to content

Commit 843a191

Browse files
Add bucket storage for lfs
1 parent 87dea2c commit 843a191

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

modules/lfs/content_store.go

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package lfs
22

33
import (
4+
"context"
45
"crypto/sha256"
56
"encoding/hex"
67
"errors"
78
"io"
8-
"os"
99
"path/filepath"
1010

1111
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/setting"
13+
14+
"gocloud.dev/blob"
1215
)
1316

1417
var (
@@ -24,43 +27,45 @@ type ContentStore struct {
2427
// Get takes a Meta object and retrieves the content from the store, returning
2528
// it as an io.Reader. If fromByte > 0, the reader starts from that byte
2629
func (s *ContentStore) Get(meta *models.LFSMetaObject, fromByte int64) (io.ReadCloser, error) {
27-
path := filepath.Join(s.BasePath, transformKey(meta.Oid))
28-
29-
f, err := os.Open(path)
30+
ctx := context.Background()
31+
bucket, err := blob.OpenBucket(ctx, setting.FileStorage.Bucket)
3032
if err != nil {
3133
return nil, err
3234
}
33-
if fromByte > 0 {
34-
_, err = f.Seek(fromByte, os.SEEK_CUR)
35+
bucket = blob.PrefixedBucket(bucket, s.BasePath+"/")
36+
37+
reader, err := bucket.NewRangeReader(ctx, transformKey(meta.Oid), fromByte, meta.Size-fromByte, nil)
38+
if err != nil {
39+
return nil, err
3540
}
36-
return f, err
41+
42+
return reader, nil
3743
}
3844

3945
// Put takes a Meta object and an io.Reader and writes the content to the store.
4046
func (s *ContentStore) Put(meta *models.LFSMetaObject, r io.Reader) error {
41-
path := filepath.Join(s.BasePath, transformKey(meta.Oid))
42-
tmpPath := path + ".tmp"
43-
44-
dir := filepath.Dir(path)
45-
if err := os.MkdirAll(dir, 0750); err != nil {
47+
ctx := context.Background()
48+
bucket, err := blob.OpenBucket(ctx, setting.FileStorage.Bucket)
49+
if err != nil {
4650
return err
4751
}
4852

49-
file, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0640)
53+
bucket = blob.PrefixedBucket(bucket, s.BasePath+"/")
54+
defer bucket.Close()
55+
56+
writer, err := bucket.NewWriter(ctx, transformKey(meta.Oid), nil)
5057
if err != nil {
5158
return err
5259
}
53-
defer os.Remove(tmpPath)
60+
defer writer.Close()
5461

5562
hash := sha256.New()
56-
hw := io.MultiWriter(hash, file)
63+
hw := io.MultiWriter(hash, writer)
5764

5865
written, err := io.Copy(hw, r)
5966
if err != nil {
60-
file.Close()
6167
return err
6268
}
63-
file.Close()
6469

6570
if written != meta.Size {
6671
return errSizeMismatch
@@ -71,28 +76,44 @@ func (s *ContentStore) Put(meta *models.LFSMetaObject, r io.Reader) error {
7176
return errHashMismatch
7277
}
7378

74-
return os.Rename(tmpPath, path)
79+
return nil
7580
}
7681

7782
// Exists returns true if the object exists in the content store.
7883
func (s *ContentStore) Exists(meta *models.LFSMetaObject) bool {
79-
path := filepath.Join(s.BasePath, transformKey(meta.Oid))
80-
if _, err := os.Stat(path); os.IsNotExist(err) {
84+
ctx := context.Background()
85+
bucket, err := blob.OpenBucket(ctx, setting.FileStorage.Bucket)
86+
if err != nil {
8187
return false
8288
}
83-
return true
89+
90+
bucket = blob.PrefixedBucket(bucket, s.BasePath+"/")
91+
defer bucket.Close()
92+
93+
exist, _ := bucket.Exists(ctx, transformKey(meta.Oid))
94+
95+
return exist
8496
}
8597

8698
// Verify returns true if the object exists in the content store and size is correct.
8799
func (s *ContentStore) Verify(meta *models.LFSMetaObject) (bool, error) {
88-
path := filepath.Join(s.BasePath, transformKey(meta.Oid))
100+
ctx := context.Background()
101+
bucket, err := blob.OpenBucket(ctx, setting.FileStorage.Bucket)
102+
if err != nil {
103+
return false, err
104+
}
105+
bucket = blob.PrefixedBucket(bucket, s.BasePath+"/")
106+
defer bucket.Close()
89107

90-
fi, err := os.Stat(path)
91-
if os.IsNotExist(err) || err == nil && fi.Size() != meta.Size {
92-
return false, nil
93-
} else if err != nil {
108+
reader, err := bucket.NewReader(ctx, transformKey(meta.Oid), nil)
109+
if err != nil {
94110
return false, err
95111
}
112+
defer reader.Close()
113+
114+
if reader.Size() != meta.Size {
115+
return false, nil
116+
}
96117

97118
return true, nil
98119
}

modules/setting/setting.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,6 @@ func NewContext() {
683683
log.Fatal("Failed to map LFS settings: %v", err)
684684
}
685685
LFS.ContentPath = sec.Key("LFS_CONTENT_PATH").MustString(filepath.Join(AppDataPath, "lfs"))
686-
if !filepath.IsAbs(LFS.ContentPath) {
687-
LFS.ContentPath = filepath.Join(AppWorkPath, LFS.ContentPath)
688-
}
689686

690687
LFS.HTTPAuthExpiry = sec.Key("LFS_HTTP_AUTH_EXPIRY").MustDuration(20 * time.Minute)
691688

0 commit comments

Comments
 (0)