Skip to content

Commit 15700b3

Browse files
committed
Add size to Save function (go-gitea#15264)
This PR proposes an alternative solution to go-gitea#15255 - just add the size to the save function. Yes it is less apparently clean but it may be more correct. Close go-gitea#15255 Fix go-gitea#15253 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 33c4e24 commit 15700b3

File tree

7 files changed

+17
-10
lines changed

7 files changed

+17
-10
lines changed

integrations/attachment_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestGetAttachment(t *testing.T) {
122122
t.Run(tc.name, func(t *testing.T) {
123123
//Write empty file to be available for response
124124
if tc.createFile {
125-
_, err := storage.Attachments.Save(models.AttachmentRelativePath(tc.uuid), strings.NewReader("hello world"))
125+
_, err := storage.Attachments.Save(models.AttachmentRelativePath(tc.uuid), strings.NewReader("hello world"), -1)
126126
assert.NoError(t, err)
127127
}
128128
//Actual test

models/attachment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (a *Attachment) LinkedRepository() (*Repository, UnitType, error) {
8585
func NewAttachment(attach *Attachment, buf []byte, file io.Reader) (_ *Attachment, err error) {
8686
attach.UUID = gouuid.New().String()
8787

88-
size, err := storage.Attachments.Save(attach.RelativePath(), io.MultiReader(bytes.NewReader(buf), file))
88+
size, err := storage.Attachments.Save(attach.RelativePath(), io.MultiReader(bytes.NewReader(buf), file), -1)
8989
if err != nil {
9090
return nil, fmt.Errorf("Create: %v", err)
9191
}

modules/lfs/content_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (s *ContentStore) Put(meta *models.LFSMetaObject, r io.Reader) error {
7474

7575
// now pass the wrapped reader to Save - if there is a size mismatch or hash mismatch then
7676
// the errors returned by the newHashingReader should percolate up to here
77-
written, err := s.Save(p, wrappedRd)
77+
written, err := s.Save(p, wrappedRd, meta.Size)
7878
if err != nil {
7979
log.Error("Whilst putting LFS OID[%s]: Failed to copy to tmpPath: %s Error: %v", meta.Oid, p, err)
8080
return err

modules/migrations/gitea_uploader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
283283
}
284284
}
285285
defer rc.Close()
286-
_, err = storage.Attachments.Save(attach.RelativePath(), rc)
286+
_, err = storage.Attachments.Save(attach.RelativePath(), rc, int64(*asset.Size))
287287
return err
288288
}()
289289
if err != nil {

modules/storage/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (l *LocalStorage) Open(path string) (Object, error) {
6666
}
6767

6868
// Save a file
69-
func (l *LocalStorage) Save(path string, r io.Reader) (int64, error) {
69+
func (l *LocalStorage) Save(path string, r io.Reader, size int64) (int64, error) {
7070
p := filepath.Join(l.dir, path)
7171
if err := os.MkdirAll(filepath.Dir(p), os.ModePerm); err != nil {
7272
return 0, err

modules/storage/minio.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ func (m *MinioStorage) Open(path string) (Object, error) {
131131
}
132132

133133
// Save save a file to minio
134-
func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
134+
func (m *MinioStorage) Save(path string, r io.Reader, size int64) (int64, error) {
135135
uploadInfo, err := m.client.PutObject(
136136
m.ctx,
137137
m.bucket,
138138
m.buildMinioPath(path),
139139
r,
140-
-1,
140+
size,
141141
minio.PutObjectOptions{ContentType: "application/octet-stream"},
142142
)
143143
if err != nil {

modules/storage/storage.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ type Object interface {
6565
// ObjectStorage represents an object storage to handle a bucket and files
6666
type ObjectStorage interface {
6767
Open(path string) (Object, error)
68-
Save(path string, r io.Reader) (int64, error)
68+
// Save store a object, if size is unknown set -1
69+
Save(path string, r io.Reader, size int64) (int64, error)
6970
Stat(path string) (os.FileInfo, error)
7071
Delete(path string) error
7172
URL(path, name string) (*url.URL, error)
@@ -80,7 +81,13 @@ func Copy(dstStorage ObjectStorage, dstPath string, srcStorage ObjectStorage, sr
8081
}
8182
defer f.Close()
8283

83-
return dstStorage.Save(dstPath, f)
84+
size := int64(-1)
85+
fsinfo, err := f.Stat()
86+
if err == nil {
87+
size = fsinfo.Size()
88+
}
89+
90+
return dstStorage.Save(dstPath, f, size)
8491
}
8592

8693
// SaveFrom saves data to the ObjectStorage with path p from the callback
@@ -94,7 +101,7 @@ func SaveFrom(objStorage ObjectStorage, p string, callback func(w io.Writer) err
94101
}
95102
}()
96103

97-
_, err := objStorage.Save(p, pr)
104+
_, err := objStorage.Save(p, pr, -1)
98105
return err
99106
}
100107

0 commit comments

Comments
 (0)