mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-09 19:52:30 +02:00
Extract processing handler and imageType functions from server.go
This commit is contained in:
96
image_type.go
Normal file
96
image_type.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -s -w
|
||||
#include "vips.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type imageType int
|
||||
|
||||
const (
|
||||
imageTypeUnknown = imageType(C.UNKNOWN)
|
||||
imageTypeJPEG = imageType(C.JPEG)
|
||||
imageTypePNG = imageType(C.PNG)
|
||||
imageTypeWEBP = imageType(C.WEBP)
|
||||
imageTypeGIF = imageType(C.GIF)
|
||||
imageTypeICO = imageType(C.ICO)
|
||||
imageTypeSVG = imageType(C.SVG)
|
||||
imageTypeHEIC = imageType(C.HEIC)
|
||||
|
||||
contentDispositionFilenameFallback = "image"
|
||||
)
|
||||
|
||||
var (
|
||||
imageTypes = map[string]imageType{
|
||||
"jpeg": imageTypeJPEG,
|
||||
"jpg": imageTypeJPEG,
|
||||
"png": imageTypePNG,
|
||||
"webp": imageTypeWEBP,
|
||||
"gif": imageTypeGIF,
|
||||
"ico": imageTypeICO,
|
||||
"svg": imageTypeSVG,
|
||||
"heic": imageTypeHEIC,
|
||||
}
|
||||
|
||||
mimes = map[imageType]string{
|
||||
imageTypeJPEG: "image/jpeg",
|
||||
imageTypePNG: "image/png",
|
||||
imageTypeWEBP: "image/webp",
|
||||
imageTypeGIF: "image/gif",
|
||||
imageTypeICO: "image/x-icon",
|
||||
imageTypeHEIC: "image/heif",
|
||||
}
|
||||
|
||||
contentDispositionsFmt = map[imageType]string{
|
||||
imageTypeJPEG: "inline; filename=\"%s.jpg\"",
|
||||
imageTypePNG: "inline; filename=\"%s.png\"",
|
||||
imageTypeWEBP: "inline; filename=\"%s.webp\"",
|
||||
imageTypeGIF: "inline; filename=\"%s.gif\"",
|
||||
imageTypeICO: "inline; filename=\"%s.ico\"",
|
||||
imageTypeHEIC: "inline; filename=\"%s.heic\"",
|
||||
}
|
||||
)
|
||||
|
||||
func (it imageType) String() string {
|
||||
for k, v := range imageTypes {
|
||||
if v == it {
|
||||
return k
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (it imageType) Mime() string {
|
||||
if mime, ok := mimes[it]; ok {
|
||||
return mime
|
||||
} else {
|
||||
return "application/octet-stream"
|
||||
}
|
||||
}
|
||||
|
||||
func (it imageType) ContentDisposition(imageURL string) string {
|
||||
format, ok := contentDispositionsFmt[it]
|
||||
if !ok {
|
||||
return "inline"
|
||||
}
|
||||
|
||||
url, err := url.Parse(imageURL)
|
||||
if err != nil {
|
||||
return fmt.Sprintf(format, contentDispositionFilenameFallback)
|
||||
}
|
||||
|
||||
_, filename := filepath.Split(url.Path)
|
||||
if len(filename) == 0 {
|
||||
return fmt.Sprintf(format, contentDispositionFilenameFallback)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(format, strings.TrimSuffix(filename, filepath.Ext(filename)))
|
||||
}
|
Reference in New Issue
Block a user