]> source.dussan.org Git - gitea.git/commitdiff
Add size to Save function (#15264) (#15270)
authorzeripath <art27@cantab.net>
Sun, 4 Apr 2021 16:04:55 +0000 (17:04 +0100)
committerGitHub <noreply@github.com>
Sun, 4 Apr 2021 16:04:55 +0000 (12:04 -0400)
This PR proposes an alternative solution to #15255 - just add the size to the
save function. Yes it is less apparently clean but it may be more correct.

Close #15255
Fix #15253

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
integrations/attachment_test.go
models/attachment.go
modules/lfs/content_store.go
modules/migrations/gitea_uploader.go
modules/storage/local.go
modules/storage/minio.go
modules/storage/storage.go

index a28e38b9907cfde57273efd874294504ded934ea..052f27605a2c4a24fedb8004cb5e4a56b5bc8b64 100644 (file)
@@ -122,7 +122,7 @@ func TestGetAttachment(t *testing.T) {
                t.Run(tc.name, func(t *testing.T) {
                        //Write empty file to be available for response
                        if tc.createFile {
-                               _, err := storage.Attachments.Save(models.AttachmentRelativePath(tc.uuid), strings.NewReader("hello world"))
+                               _, err := storage.Attachments.Save(models.AttachmentRelativePath(tc.uuid), strings.NewReader("hello world"), -1)
                                assert.NoError(t, err)
                        }
                        //Actual test
index 478b4cd0d289f5d36b25e612c7bf4674a884d24e..972fe5680e4332cc4e80d94408014494f1835827 100644 (file)
@@ -85,7 +85,7 @@ func (a *Attachment) LinkedRepository() (*Repository, UnitType, error) {
 func NewAttachment(attach *Attachment, buf []byte, file io.Reader) (_ *Attachment, err error) {
        attach.UUID = gouuid.New().String()
 
-       size, err := storage.Attachments.Save(attach.RelativePath(), io.MultiReader(bytes.NewReader(buf), file))
+       size, err := storage.Attachments.Save(attach.RelativePath(), io.MultiReader(bytes.NewReader(buf), file), -1)
        if err != nil {
                return nil, fmt.Errorf("Create: %v", err)
        }
index 788ef5b9a695061ec32589cdc84ca8244bdb4519..545d65c30f6da1d88f602257f8a562192bdb5ee7 100644 (file)
@@ -74,7 +74,7 @@ func (s *ContentStore) Put(meta *models.LFSMetaObject, r io.Reader) error {
 
        // now pass the wrapped reader to Save - if there is a size mismatch or hash mismatch then
        // the errors returned by the newHashingReader should percolate up to here
-       written, err := s.Save(p, wrappedRd)
+       written, err := s.Save(p, wrappedRd, meta.Size)
        if err != nil {
                log.Error("Whilst putting LFS OID[%s]: Failed to copy to tmpPath: %s Error: %v", meta.Oid, p, err)
                return err
index aa1ea4bc09d0492c0800e1861fa56fd2bb4a7c36..02f97c4ff56a5ce9ed8b233145786d74b4ed4a47 100644 (file)
@@ -283,7 +283,7 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
                                        }
                                }
                                defer rc.Close()
-                               _, err = storage.Attachments.Save(attach.RelativePath(), rc)
+                               _, err = storage.Attachments.Save(attach.RelativePath(), rc, int64(*asset.Size))
                                return err
                        }()
                        if err != nil {
index 982d2b88c6e791ef66285291581e17ccaa745acb..46e5d60e6b5d31d1d85b9797d1194e403545ace1 100644 (file)
@@ -66,7 +66,7 @@ func (l *LocalStorage) Open(path string) (Object, error) {
 }
 
 // Save a file
-func (l *LocalStorage) Save(path string, r io.Reader) (int64, error) {
+func (l *LocalStorage) Save(path string, r io.Reader, size int64) (int64, error) {
        p := filepath.Join(l.dir, path)
        if err := os.MkdirAll(filepath.Dir(p), os.ModePerm); err != nil {
                return 0, err
index 0e0cb3690da64d21c21cad0a00755cd879c16651..724445c0ab9de1704b0d26490b116ec58f0d49d6 100644 (file)
@@ -131,13 +131,13 @@ func (m *MinioStorage) Open(path string) (Object, error) {
 }
 
 // Save save a file to minio
-func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
+func (m *MinioStorage) Save(path string, r io.Reader, size int64) (int64, error) {
        uploadInfo, err := m.client.PutObject(
                m.ctx,
                m.bucket,
                m.buildMinioPath(path),
                r,
-               -1,
+               size,
                minio.PutObjectOptions{ContentType: "application/octet-stream"},
        )
        if err != nil {
index ec3a1c14a1a8eeb9a0735d56bb439f7087603a3d..65f8978e5a1b5c2b229bda24c0f80a31358c9f12 100644 (file)
@@ -65,7 +65,8 @@ type Object interface {
 // ObjectStorage represents an object storage to handle a bucket and files
 type ObjectStorage interface {
        Open(path string) (Object, error)
-       Save(path string, r io.Reader) (int64, error)
+       // Save store a object, if size is unknown set -1
+       Save(path string, r io.Reader, size int64) (int64, error)
        Stat(path string) (os.FileInfo, error)
        Delete(path string) error
        URL(path, name string) (*url.URL, error)
@@ -80,7 +81,13 @@ func Copy(dstStorage ObjectStorage, dstPath string, srcStorage ObjectStorage, sr
        }
        defer f.Close()
 
-       return dstStorage.Save(dstPath, f)
+       size := int64(-1)
+       fsinfo, err := f.Stat()
+       if err == nil {
+               size = fsinfo.Size()
+       }
+
+       return dstStorage.Save(dstPath, f, size)
 }
 
 // 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
                }
        }()
 
-       _, err := objStorage.Save(p, pr)
+       _, err := objStorage.Save(p, pr, -1)
        return err
 }