Fix read size

This commit is contained in:
Lunny Xiao 2024-05-17 11:20:30 +08:00
parent 8dcf589c65
commit c3f1090719
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
4 changed files with 25 additions and 17 deletions

View File

@ -6,11 +6,8 @@ package setting
import (
"errors"
"fmt"
"net/url"
"path/filepath"
"strings"
"code.gitea.io/gitea/modules/log"
)
// StorageType is a type of Storage
@ -75,17 +72,6 @@ type AzureBlobStorageConfig struct {
ServeDirect bool `ini:"SERVE_DIRECT"`
}
func (cfg AzureBlobStorageConfig) ConnectionString() string {
u, err := url.Parse(cfg.Endpoint)
if err != nil {
log.Fatal("parse endpoint %s failed: %v", cfg.Endpoint, err)
return ""
}
return fmt.Sprintf("DefaultEndpointsProtocol=%s;AccountName=%s;AccountKey=%s;EndpointSuffix=%s",
u.Scheme, cfg.AccountName, cfg.AccountKey, u.Host)
}
func (cfg *AzureBlobStorageConfig) ToShadow() {
if cfg.AccountKey != "" {
cfg.AccountKey = "******"

View File

@ -182,18 +182,19 @@ func (a *AzureBlobStorage) Open(path string) (Object, error) {
// Save saves a file to azure blob storage
func (a *AzureBlobStorage) Save(path string, r io.Reader, size int64) (int64, error) {
rd := util.NewCountingReader(r)
_, err := a.client.UploadStream(
a.ctx,
a.cfg.Container,
a.buildAzureBlobPath(path),
r,
rd,
// TODO: support set block size and concurrency
&blockblob.UploadStreamOptions{},
)
if err != nil {
return 0, convertAzureBlobErr(err)
}
return size, nil
return int64(rd.Count()), nil
}
type azureBlobFileInfo struct {

View File

@ -76,3 +76,24 @@ func IsEmptyReader(r io.Reader) (err error) {
}
}
}
type CountingReader struct {
io.Reader
n int
}
var _ io.Reader = &CountingReader{}
func (w *CountingReader) Count() int {
return w.n
}
func (w *CountingReader) Read(p []byte) (int, error) {
n, err := w.Reader.Read(p)
w.n += n
return n, err
}
func NewCountingReader(rd io.Reader) *CountingReader {
return &CountingReader{Reader: rd}
}

View File

@ -55,7 +55,7 @@ func saveUploadChunkBase(st storage.ObjectStorage, ctx *ArtifactContext,
}
}
if writtenSize != contentSize {
checkErr = errors.Join(checkErr, fmt.Errorf("contentSize not match body size"))
checkErr = errors.Join(checkErr, fmt.Errorf("writtenSize %d not match contentSize %d", writtenSize, contentSize))
}
if checkErr != nil {
if err := st.Delete(storagePath); err != nil {