Skip to content

Commit 25cb320

Browse files
committed
Allow to display embed images/pdfs when SERVE_DIRECT was enabled on MinIO storage
1 parent aaa8033 commit 25cb320

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

modules/storage/minio.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/url"
1313
"os"
1414
"path"
15+
"path/filepath"
1516
"strings"
1617
"time"
1718

@@ -285,8 +286,24 @@ func (m *MinioStorage) URL(path, name, method string, serveDirectReqParams url.V
285286
if err != nil {
286287
return nil, err
287288
}
288-
// 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?
289-
reqParams.Set("response-content-disposition", "attachment; filename=\""+quoteEscaper.Replace(name)+"\"")
289+
// Detect content type by extension name
290+
contentDisposition := "attachment; filename=\""+quoteEscaper.Replace(name)+"\""
291+
inlineExtMime := map[string]string {
292+
"png": "image/png",
293+
"jpg": "image/jpeg",
294+
"jpeg": "image/jpeg",
295+
"pdf": "application/pdf",
296+
"gif": "image/gif",
297+
"webp": "iamge/webp",
298+
}
299+
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(name), "."))
300+
mime, ok := inlineExtMime[ext]
301+
if ok {
302+
reqParams.Set("response-content-type", mime)
303+
contentDisposition = "inline"
304+
}
305+
306+
reqParams.Set("response-content-disposition", contentDisposition)
290307
expires := 5 * time.Minute
291308
if method == http.MethodHead {
292309
u, err := m.client.PresignedHeadObject(m.ctx, m.bucket, m.buildMinioPath(path), expires, reqParams)

0 commit comments

Comments
 (0)