Skip to content

Commit 23d6ec0

Browse files
authored
Merge branch 'main' into fix-explore-repo
2 parents 837312a + 5727056 commit 23d6ec0

File tree

7 files changed

+72
-22
lines changed

7 files changed

+72
-22
lines changed

cmd/migrate_storage.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,21 @@ var CmdMigrateStorage = cli.Command{
7272
cli.StringFlag{
7373
Name: "minio-base-path",
7474
Value: "",
75-
Usage: "Minio storage basepath on the bucket",
75+
Usage: "Minio storage base path on the bucket",
7676
},
7777
cli.BoolFlag{
7878
Name: "minio-use-ssl",
7979
Usage: "Enable SSL for minio",
8080
},
81+
cli.BoolFlag{
82+
Name: "minio-insecure-skip-verify",
83+
Usage: "Skip SSL verification",
84+
},
85+
cli.StringFlag{
86+
Name: "minio-checksum-algorithm",
87+
Value: "",
88+
Usage: "Minio checksum algorithm (default/md5)",
89+
},
8190
},
8291
}
8392

@@ -168,13 +177,15 @@ func runMigrateStorage(ctx *cli.Context) error {
168177
dstStorage, err = storage.NewMinioStorage(
169178
stdCtx,
170179
storage.MinioStorageConfig{
171-
Endpoint: ctx.String("minio-endpoint"),
172-
AccessKeyID: ctx.String("minio-access-key-id"),
173-
SecretAccessKey: ctx.String("minio-secret-access-key"),
174-
Bucket: ctx.String("minio-bucket"),
175-
Location: ctx.String("minio-location"),
176-
BasePath: ctx.String("minio-base-path"),
177-
UseSSL: ctx.Bool("minio-use-ssl"),
180+
Endpoint: ctx.String("minio-endpoint"),
181+
AccessKeyID: ctx.String("minio-access-key-id"),
182+
SecretAccessKey: ctx.String("minio-secret-access-key"),
183+
Bucket: ctx.String("minio-bucket"),
184+
Location: ctx.String("minio-location"),
185+
BasePath: ctx.String("minio-base-path"),
186+
UseSSL: ctx.Bool("minio-use-ssl"),
187+
InsecureSkipVerify: ctx.Bool("minio-insecure-skip-verify"),
188+
ChecksumAlgorithm: ctx.String("minio-checksum-algorithm"),
178189
})
179190
default:
180191
return fmt.Errorf("unsupported storage type: %s", ctx.String("storage"))

custom/conf/app.example.ini

+3
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,9 @@ ROUTER = console
18901890
;;
18911891
;; Minio skip SSL verification available when STORAGE_TYPE is `minio`
18921892
;MINIO_INSECURE_SKIP_VERIFY = false
1893+
;;
1894+
;; Minio checksum algorithm: default (for MinIO or AWS S3) or md5 (for Cloudflare or Backblaze)
1895+
;MINIO_CHECKSUM_ALGORITHM = default
18931896

18941897
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18951898
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/administration/config-cheat-sheet.en-us.md

+1
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ Default templates for project boards:
857857
- `MINIO_BASE_PATH`: **attachments/**: Minio base path on the bucket only available when STORAGE_TYPE is `minio`
858858
- `MINIO_USE_SSL`: **false**: Minio enabled ssl only available when STORAGE_TYPE is `minio`
859859
- `MINIO_INSECURE_SKIP_VERIFY`: **false**: Minio skip SSL verification available when STORAGE_TYPE is `minio`
860+
- `MINIO_CHECKSUM_ALGORITHM`: **default**: Minio checksum algorithm: `default` (for MinIO or AWS S3) or `md5` (for Cloudflare or Backblaze)
860861

861862
## Log (`log`)
862863

modules/lfs/content_store.go

+29-11
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,22 @@ func (s *ContentStore) Put(pointer Pointer, r io.Reader) error {
6060
return err
6161
}
6262

63-
// This shouldn't happen but it is sensible to test
64-
if written != pointer.Size {
65-
if err := s.Delete(p); err != nil {
66-
log.Error("Cleaning the LFS OID[%s] failed: %v", pointer.Oid, err)
63+
// check again whether there is any error during the Save operation
64+
// because some errors might be ignored by the Reader's caller
65+
if wrappedRd.lastError != nil && !errors.Is(wrappedRd.lastError, io.EOF) {
66+
err = wrappedRd.lastError
67+
} else if written != pointer.Size {
68+
err = ErrSizeMismatch
69+
}
70+
71+
// if the upload failed, try to delete the file
72+
if err != nil {
73+
if errDel := s.Delete(p); errDel != nil {
74+
log.Error("Cleaning the LFS OID[%s] failed: %v", pointer.Oid, errDel)
6775
}
68-
return ErrSizeMismatch
6976
}
7077

71-
return nil
78+
return err
7279
}
7380

7481
// Exists returns true if the object exists in the content store.
@@ -109,6 +116,17 @@ type hashingReader struct {
109116
expectedSize int64
110117
hash hash.Hash
111118
expectedHash string
119+
lastError error
120+
}
121+
122+
// recordError records the last error during the Save operation
123+
// Some callers of the Reader doesn't respect the returned "err"
124+
// For example, MinIO's Put will ignore errors if the written size could equal to expected size
125+
// So we must remember the error by ourselves,
126+
// and later check again whether ErrSizeMismatch or ErrHashMismatch occurs during the Save operation
127+
func (r *hashingReader) recordError(err error) error {
128+
r.lastError = err
129+
return err
112130
}
113131

114132
func (r *hashingReader) Read(b []byte) (int, error) {
@@ -118,22 +136,22 @@ func (r *hashingReader) Read(b []byte) (int, error) {
118136
r.currentSize += int64(n)
119137
wn, werr := r.hash.Write(b[:n])
120138
if wn != n || werr != nil {
121-
return n, werr
139+
return n, r.recordError(werr)
122140
}
123141
}
124142

125-
if err != nil && err == io.EOF {
143+
if errors.Is(err, io.EOF) || r.currentSize >= r.expectedSize {
126144
if r.currentSize != r.expectedSize {
127-
return n, ErrSizeMismatch
145+
return n, r.recordError(ErrSizeMismatch)
128146
}
129147

130148
shaStr := hex.EncodeToString(r.hash.Sum(nil))
131149
if shaStr != r.expectedHash {
132-
return n, ErrHashMismatch
150+
return n, r.recordError(ErrHashMismatch)
133151
}
134152
}
135153

136-
return n, err
154+
return n, r.recordError(err)
137155
}
138156

139157
func newHashingReader(expectedSize int64, expectedHash string, reader io.Reader) *hashingReader {

modules/setting/storage.go

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func getStorage(rootCfg ConfigProvider, name, typ string, targetSec *ini.Section
4242
sec.Key("MINIO_LOCATION").MustString("us-east-1")
4343
sec.Key("MINIO_USE_SSL").MustBool(false)
4444
sec.Key("MINIO_INSECURE_SKIP_VERIFY").MustBool(false)
45+
sec.Key("MINIO_CHECKSUM_ALGORITHM").MustString("default")
4546

4647
if targetSec == nil {
4748
targetSec, _ = rootCfg.NewSection(name)

modules/storage/minio.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package storage
66
import (
77
"context"
88
"crypto/tls"
9+
"fmt"
910
"io"
1011
"net/http"
1112
"net/url"
@@ -53,10 +54,12 @@ type MinioStorageConfig struct {
5354
BasePath string `ini:"MINIO_BASE_PATH"`
5455
UseSSL bool `ini:"MINIO_USE_SSL"`
5556
InsecureSkipVerify bool `ini:"MINIO_INSECURE_SKIP_VERIFY"`
57+
ChecksumAlgorithm string `ini:"MINIO_CHECKSUM_ALGORITHM"`
5658
}
5759

5860
// MinioStorage returns a minio bucket storage
5961
type MinioStorage struct {
62+
cfg *MinioStorageConfig
6063
ctx context.Context
6164
client *minio.Client
6265
bucket string
@@ -91,6 +94,10 @@ func NewMinioStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
9194
}
9295
config := configInterface.(MinioStorageConfig)
9396

97+
if config.ChecksumAlgorithm != "" && config.ChecksumAlgorithm != "default" && config.ChecksumAlgorithm != "md5" {
98+
return nil, fmt.Errorf("invalid minio checksum algorithm: %s", config.ChecksumAlgorithm)
99+
}
100+
94101
log.Info("Creating Minio storage at %s:%s with base path %s", config.Endpoint, config.Bucket, config.BasePath)
95102

96103
minioClient, err := minio.New(config.Endpoint, &minio.Options{
@@ -113,6 +120,7 @@ func NewMinioStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
113120
}
114121

115122
return &MinioStorage{
123+
cfg: &config,
116124
ctx: ctx,
117125
client: minioClient,
118126
bucket: config.Bucket,
@@ -124,7 +132,7 @@ func (m *MinioStorage) buildMinioPath(p string) string {
124132
return util.PathJoinRelX(m.basePath, p)
125133
}
126134

127-
// Open open a file
135+
// Open opens a file
128136
func (m *MinioStorage) Open(path string) (Object, error) {
129137
opts := minio.GetObjectOptions{}
130138
object, err := m.client.GetObject(m.ctx, m.bucket, m.buildMinioPath(path), opts)
@@ -134,15 +142,22 @@ func (m *MinioStorage) Open(path string) (Object, error) {
134142
return &minioObject{object}, nil
135143
}
136144

137-
// Save save a file to minio
145+
// Save saves a file to minio
138146
func (m *MinioStorage) Save(path string, r io.Reader, size int64) (int64, error) {
139147
uploadInfo, err := m.client.PutObject(
140148
m.ctx,
141149
m.bucket,
142150
m.buildMinioPath(path),
143151
r,
144152
size,
145-
minio.PutObjectOptions{ContentType: "application/octet-stream"},
153+
minio.PutObjectOptions{
154+
ContentType: "application/octet-stream",
155+
// some storages like:
156+
// * https://developers.cloudflare.com/r2/api/s3/api/
157+
// * https://www.backblaze.com/b2/docs/s3_compatible_api.html
158+
// do not support "x-amz-checksum-algorithm" header, so use legacy MD5 checksum
159+
SendContentMd5: m.cfg.ChecksumAlgorithm == "md5",
160+
},
146161
)
147162
if err != nil {
148163
return 0, convertMinioErr(err)

tests/pgsql.ini.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ MINIO_SECRET_ACCESS_KEY = 12345678
125125
MINIO_BUCKET = gitea
126126
MINIO_LOCATION = us-east-1
127127
MINIO_USE_SSL = false
128+
MINIO_CHECKSUM_ALGORITHM = md5
128129

129130
[packages]
130131
ENABLED = true

0 commit comments

Comments
 (0)