Skip to content

Commit

Permalink
some optimizations for uploading to minio
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Apr 3, 2021
1 parent 42e62fa commit a688671
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
30 changes: 30 additions & 0 deletions modules/io/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package io

import "io"

// ReadSizer represents a reader which have size information
type ReadSizer interface {
io.Reader
Size() int64
}

type readSizer struct {
io.Reader
size int64
}

func (r *readSizer) Size() int64 {
return r.size
}

// WithSize binding io.Reader and size into a ReadSizer
func WithSize(rd io.Reader, size int64) ReadSizer {
return &readSizer{
Reader: rd,
size: size,
}
}
9 changes: 9 additions & 0 deletions modules/lfs/content_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"

"code.gitea.io/gitea/models"
myio "code.gitea.io/gitea/modules/io"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/storage"
)
Expand Down Expand Up @@ -125,6 +126,10 @@ type hashingReader struct {
expectedHash string
}

var (
_ myio.ReadSizer = &hashingReader{}
)

func (r *hashingReader) Read(b []byte) (int, error) {
n, err := r.internal.Read(b)

Expand All @@ -150,6 +155,10 @@ func (r *hashingReader) Read(b []byte) (int, error) {
return n, err
}

func (r *hashingReader) Size() int64 {
return r.expectedSize
}

func newHashingReader(expectedSize int64, expectedHash string, reader io.Reader) *hashingReader {
return &hashingReader{
internal: reader,
Expand Down
3 changes: 2 additions & 1 deletion modules/migrations/gitea_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
myio "code.gitea.io/gitea/modules/io"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/migrations/base"
"code.gitea.io/gitea/modules/repository"
Expand Down Expand Up @@ -283,7 +284,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(), myio.WithSize(rc, attach.Size))
return err
}()
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion modules/storage/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

myio "code.gitea.io/gitea/modules/io"
"code.gitea.io/gitea/modules/log"

"github.com/minio/minio-go/v7"
Expand Down Expand Up @@ -132,12 +133,16 @@ 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) {
var size int64 = -1
if sizer, ok := r.(myio.ReadSizer); ok {
size = sizer.Size()
}
uploadInfo, err := m.client.PutObject(
m.ctx,
m.bucket,
m.buildMinioPath(path),
r,
-1,
size,
minio.PutObjectOptions{ContentType: "application/octet-stream"},
)
if err != nil {
Expand Down

0 comments on commit a688671

Please sign in to comment.