mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-11 04:32:29 +02:00
Add Content-Disposition filename customization
This commit is contained in:
@@ -76,21 +76,25 @@ func (it imageType) Mime() string {
|
|||||||
return "application/octet-stream"
|
return "application/octet-stream"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it imageType) ContentDisposition(imageURL string) string {
|
func (it imageType) ContentDisposition(filename string) string {
|
||||||
format, ok := contentDispositionsFmt[it]
|
format, ok := contentDispositionsFmt[it]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "inline"
|
return "inline"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf(format, filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (it imageType) ContentDispositionFromURL(imageURL string) string {
|
||||||
url, err := url.Parse(imageURL)
|
url, err := url.Parse(imageURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Sprintf(format, contentDispositionFilenameFallback)
|
return it.ContentDisposition(contentDispositionFilenameFallback)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, filename := filepath.Split(url.Path)
|
_, filename := filepath.Split(url.Path)
|
||||||
if len(filename) == 0 {
|
if len(filename) == 0 {
|
||||||
return fmt.Sprintf(format, contentDispositionFilenameFallback)
|
return it.ContentDisposition(contentDispositionFilenameFallback)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf(format, strings.TrimSuffix(filename, filepath.Ext(filename)))
|
return it.ContentDisposition(strings.TrimSuffix(filename, filepath.Ext(filename)))
|
||||||
}
|
}
|
||||||
|
@@ -46,10 +46,17 @@ func initProcessingHandler() {
|
|||||||
func respondWithImage(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter, data []byte) {
|
func respondWithImage(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter, data []byte) {
|
||||||
po := getProcessingOptions(ctx)
|
po := getProcessingOptions(ctx)
|
||||||
|
|
||||||
|
var contentDisposition string
|
||||||
|
if len(po.Filename) > 0 {
|
||||||
|
contentDisposition = po.Format.ContentDisposition(po.Filename)
|
||||||
|
} else {
|
||||||
|
contentDisposition = po.Format.ContentDispositionFromURL(getImageURL(ctx))
|
||||||
|
}
|
||||||
|
|
||||||
rw.Header().Set("Expires", time.Now().Add(time.Second*time.Duration(conf.TTL)).Format(http.TimeFormat))
|
rw.Header().Set("Expires", time.Now().Add(time.Second*time.Duration(conf.TTL)).Format(http.TimeFormat))
|
||||||
rw.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", conf.TTL))
|
rw.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", conf.TTL))
|
||||||
rw.Header().Set("Content-Type", po.Format.Mime())
|
rw.Header().Set("Content-Type", po.Format.Mime())
|
||||||
rw.Header().Set("Content-Disposition", po.Format.ContentDisposition(getImageURL(ctx)))
|
rw.Header().Set("Content-Disposition", contentDisposition)
|
||||||
|
|
||||||
if len(headerVaryValue) > 0 {
|
if len(headerVaryValue) > 0 {
|
||||||
rw.Header().Set("Vary", headerVaryValue)
|
rw.Header().Set("Vary", headerVaryValue)
|
||||||
|
@@ -125,6 +125,8 @@ type processingOptions struct {
|
|||||||
PreferWebP bool
|
PreferWebP bool
|
||||||
EnforceWebP bool
|
EnforceWebP bool
|
||||||
|
|
||||||
|
Filename string
|
||||||
|
|
||||||
UsedPresets []string
|
UsedPresets []string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -635,6 +637,16 @@ func applyCacheBusterOption(po *processingOptions, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func applyFilenameOption(po *processingOptions, args []string) error {
|
||||||
|
if len(args) > 1 {
|
||||||
|
return fmt.Errorf("Invalid filename arguments: %v", args)
|
||||||
|
}
|
||||||
|
|
||||||
|
po.Filename = args[0]
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func applyProcessingOption(po *processingOptions, name string, args []string) error {
|
func applyProcessingOption(po *processingOptions, name string, args []string) error {
|
||||||
switch name {
|
switch name {
|
||||||
case "format", "f", "ext":
|
case "format", "f", "ext":
|
||||||
@@ -709,6 +721,10 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
|
|||||||
if err := applyCacheBusterOption(po, args); err != nil {
|
if err := applyCacheBusterOption(po, args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
case "filename", "fn":
|
||||||
|
if err := applyFilenameOption(po, args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Unknown processing option: %s", name)
|
return fmt.Errorf("Unknown processing option: %s", name)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user