Skip to content

Commit e1e4815

Browse files
risu729wxiaoguang
andauthored
Redirect to a presigned URL of HEAD for HEAD requests (#35088)
Resolves #35086. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 0d00ec7 commit e1e4815

File tree

42 files changed

+85
-72
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+85
-72
lines changed

modules/actions/artifacts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func IsArtifactV4(art *actions_model.ActionArtifact) bool {
2020

2121
func DownloadArtifactV4ServeDirectOnly(ctx *context.Base, art *actions_model.ActionArtifact) (bool, error) {
2222
if setting.Actions.ArtifactStorage.ServeDirect() {
23-
u, err := storage.ActionsArtifacts.URL(art.StoragePath, art.ArtifactPath, nil)
23+
u, err := storage.ActionsArtifacts.URL(art.StoragePath, art.ArtifactPath, ctx.Req.Method, nil)
2424
if u != nil && err == nil {
2525
ctx.Redirect(u.String(), http.StatusFound)
2626
return true, nil

modules/packages/content_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (s *ContentStore) ShouldServeDirect() bool {
3636
return setting.Packages.Storage.ServeDirect()
3737
}
3838

39-
func (s *ContentStore) GetServeDirectURL(key BlobHash256Key, filename string, reqParams url.Values) (*url.URL, error) {
40-
return s.store.URL(KeyToRelativePath(key), filename, reqParams)
39+
func (s *ContentStore) GetServeDirectURL(key BlobHash256Key, filename, method string, reqParams url.Values) (*url.URL, error) {
40+
return s.store.URL(KeyToRelativePath(key), filename, method, reqParams)
4141
}
4242

4343
// FIXME: Workaround to be removed in v1.20

modules/storage/azureblob.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func (a *AzureBlobStorage) Delete(path string) error {
247247
}
248248

249249
// URL gets the redirect URL to a file. The presigned link is valid for 5 minutes.
250-
func (a *AzureBlobStorage) URL(path, name string, reqParams url.Values) (*url.URL, error) {
250+
func (a *AzureBlobStorage) URL(path, name, _ string, reqParams url.Values) (*url.URL, error) {
251251
blobClient := a.getBlobClient(path)
252252

253253
startTime := time.Now()

modules/storage/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (s discardStorage) Delete(_ string) error {
3030
return fmt.Errorf("%s", s)
3131
}
3232

33-
func (s discardStorage) URL(_, _ string, _ url.Values) (*url.URL, error) {
33+
func (s discardStorage) URL(_, _, _ string, _ url.Values) (*url.URL, error) {
3434
return nil, fmt.Errorf("%s", s)
3535
}
3636

modules/storage/helper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Test_discardStorage(t *testing.T) {
3737
assert.Error(t, err, string(tt))
3838
}
3939
{
40-
got, err := tt.URL("path", "name", nil)
40+
got, err := tt.URL("path", "name", "GET", nil)
4141
assert.Nil(t, got)
4242
assert.Errorf(t, err, string(tt))
4343
}

modules/storage/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (l *LocalStorage) Delete(path string) error {
114114
}
115115

116116
// URL gets the redirect URL to a file
117-
func (l *LocalStorage) URL(path, name string, reqParams url.Values) (*url.URL, error) {
117+
func (l *LocalStorage) URL(path, name, _ string, reqParams url.Values) (*url.URL, error) {
118118
return nil, ErrURLNotSupported
119119
}
120120

modules/storage/minio.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,20 @@ func (m *MinioStorage) Delete(path string) error {
279279
}
280280

281281
// URL gets the redirect URL to a file. The presigned link is valid for 5 minutes.
282-
func (m *MinioStorage) URL(path, name string, serveDirectReqParams url.Values) (*url.URL, error) {
282+
func (m *MinioStorage) URL(path, name, method string, serveDirectReqParams url.Values) (*url.URL, error) {
283283
// copy serveDirectReqParams
284284
reqParams, err := url.ParseQuery(serveDirectReqParams.Encode())
285285
if err != nil {
286286
return nil, err
287287
}
288288
// TODO it may be good to embed images with 'inline' like ServeData does, but we don't want to have to read the file, do we?
289289
reqParams.Set("response-content-disposition", "attachment; filename=\""+quoteEscaper.Replace(name)+"\"")
290-
u, err := m.client.PresignedGetObject(m.ctx, m.bucket, m.buildMinioPath(path), 5*time.Minute, reqParams)
290+
expires := 5 * time.Minute
291+
if method == http.MethodHead {
292+
u, err := m.client.PresignedHeadObject(m.ctx, m.bucket, m.buildMinioPath(path), expires, reqParams)
293+
return u, convertMinioErr(err)
294+
}
295+
u, err := m.client.PresignedGetObject(m.ctx, m.bucket, m.buildMinioPath(path), expires, reqParams)
291296
return u, convertMinioErr(err)
292297
}
293298

modules/storage/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type ObjectStorage interface {
6363
Save(path string, r io.Reader, size int64) (int64, error)
6464
Stat(path string) (os.FileInfo, error)
6565
Delete(path string) error
66-
URL(path, name string, reqParams url.Values) (*url.URL, error)
66+
URL(path, name, method string, reqParams url.Values) (*url.URL, error)
6767
IterateObjects(path string, iterator func(path string, obj Object) error) error
6868
}
6969

routers/api/actions/artifacts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ func (ar artifactRoutes) getDownloadArtifactURL(ctx *ArtifactContext) {
428428
for _, artifact := range artifacts {
429429
var downloadURL string
430430
if setting.Actions.ArtifactStorage.ServeDirect() {
431-
u, err := ar.fs.URL(artifact.StoragePath, artifact.ArtifactName, nil)
431+
u, err := ar.fs.URL(artifact.StoragePath, artifact.ArtifactName, ctx.Req.Method, nil)
432432
if err != nil && !errors.Is(err, storage.ErrURLNotSupported) {
433433
log.Error("Error getting serve direct url: %v", err)
434434
}

routers/api/actions/artifactsv4.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ func (r *artifactV4Routes) getSignedArtifactURL(ctx *ArtifactContext) {
517517
respData := GetSignedArtifactURLResponse{}
518518

519519
if setting.Actions.ArtifactStorage.ServeDirect() {
520-
u, err := storage.ActionsArtifacts.URL(artifact.StoragePath, artifact.ArtifactPath, nil)
520+
u, err := storage.ActionsArtifacts.URL(artifact.StoragePath, artifact.ArtifactPath, ctx.Req.Method, nil)
521521
if u != nil && err == nil {
522522
respData.SignedUrl = u.String()
523523
}

0 commit comments

Comments
 (0)