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