mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-09-28 20:43:54 +02:00
Better Content-Disposition formatting for imagetype
This commit is contained in:
@@ -23,7 +23,10 @@ const (
|
|||||||
TIFF
|
TIFF
|
||||||
)
|
)
|
||||||
|
|
||||||
const contentDispositionFilenameFallback = "image"
|
const (
|
||||||
|
contentDispositionFilenameFallback = "image"
|
||||||
|
contentDispositionsFmt = "%s; filename=\"%s%s\""
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Types = map[string]Type{
|
Types = map[string]Type{
|
||||||
@@ -53,17 +56,17 @@ var (
|
|||||||
TIFF: "image/tiff",
|
TIFF: "image/tiff",
|
||||||
}
|
}
|
||||||
|
|
||||||
contentDispositionsFmt = map[Type]string{
|
extensions = map[Type]string{
|
||||||
JPEG: "%s; filename=\"%s.jpg\"",
|
JPEG: ".jpg",
|
||||||
PNG: "%s; filename=\"%s.png\"",
|
PNG: ".png",
|
||||||
WEBP: "%s; filename=\"%s.webp\"",
|
WEBP: ".webp",
|
||||||
GIF: "%s; filename=\"%s.gif\"",
|
GIF: ".gif",
|
||||||
ICO: "%s; filename=\"%s.ico\"",
|
ICO: ".ico",
|
||||||
SVG: "%s; filename=\"%s.svg\"",
|
SVG: ".svg",
|
||||||
HEIC: "%s; filename=\"%s.heic\"",
|
HEIC: ".heic",
|
||||||
AVIF: "%s; filename=\"%s.avif\"",
|
AVIF: ".avif",
|
||||||
BMP: "%s; filename=\"%s.bmp\"",
|
BMP: ".bmp",
|
||||||
TIFF: "%s; filename=\"%s.tiff\"",
|
TIFF: ".tiff",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -77,6 +80,11 @@ func ByMime(mime string) Type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (it Type) String() string {
|
func (it Type) String() string {
|
||||||
|
// JPEG has two names, we should use only the full one
|
||||||
|
if it == JPEG {
|
||||||
|
return "jpeg"
|
||||||
|
}
|
||||||
|
|
||||||
for k, v := range Types {
|
for k, v := range Types {
|
||||||
if v == it {
|
if v == it {
|
||||||
return k
|
return k
|
||||||
@@ -85,6 +93,13 @@ func (it Type) String() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (it Type) Ext() string {
|
||||||
|
if ext, ok := extensions[it]; ok {
|
||||||
|
return ext
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func (it Type) MarshalJSON() ([]byte, error) {
|
func (it Type) MarshalJSON() ([]byte, error) {
|
||||||
for k, v := range Types {
|
for k, v := range Types {
|
||||||
if v == it {
|
if v == it {
|
||||||
@@ -103,18 +118,7 @@ func (it Type) Mime() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (it Type) ContentDisposition(filename string, returnAttachment bool) string {
|
func (it Type) ContentDisposition(filename string, returnAttachment bool) string {
|
||||||
disposition := "inline"
|
return ContentDisposition(filename, it.Ext(), returnAttachment)
|
||||||
|
|
||||||
if returnAttachment {
|
|
||||||
disposition = "attachment"
|
|
||||||
}
|
|
||||||
|
|
||||||
format, ok := contentDispositionsFmt[it]
|
|
||||||
if !ok {
|
|
||||||
return disposition
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf(format, disposition, strings.ReplaceAll(filename, `"`, "%22"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it Type) ContentDispositionFromURL(imageURL string, returnAttachment bool) string {
|
func (it Type) ContentDispositionFromURL(imageURL string, returnAttachment bool) string {
|
||||||
@@ -149,3 +153,13 @@ func (it Type) SupportsColourProfile() bool {
|
|||||||
func (it Type) SupportsThumbnail() bool {
|
func (it Type) SupportsThumbnail() bool {
|
||||||
return it == HEIC || it == AVIF
|
return it == HEIC || it == AVIF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ContentDisposition(filename, ext string, returnAttachment bool) string {
|
||||||
|
disposition := "inline"
|
||||||
|
|
||||||
|
if returnAttachment {
|
||||||
|
disposition = "attachment"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf(contentDispositionsFmt, disposition, strings.ReplaceAll(filename, `"`, "%22"), ext)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user