mirror of
https://github.com/imgproxy/imgproxy.git
synced 2024-11-24 08:12:38 +02:00
Add Content-Disposition filename customization
This commit is contained in:
parent
bf64a48783
commit
2a92243e47
@ -76,21 +76,25 @@ func (it imageType) Mime() string {
|
||||
return "application/octet-stream"
|
||||
}
|
||||
|
||||
func (it imageType) ContentDisposition(imageURL string) string {
|
||||
func (it imageType) ContentDisposition(filename string) string {
|
||||
format, ok := contentDispositionsFmt[it]
|
||||
if !ok {
|
||||
return "inline"
|
||||
}
|
||||
|
||||
return fmt.Sprintf(format, filename)
|
||||
}
|
||||
|
||||
func (it imageType) ContentDispositionFromURL(imageURL string) string {
|
||||
url, err := url.Parse(imageURL)
|
||||
if err != nil {
|
||||
return fmt.Sprintf(format, contentDispositionFilenameFallback)
|
||||
return it.ContentDisposition(contentDispositionFilenameFallback)
|
||||
}
|
||||
|
||||
_, filename := filepath.Split(url.Path)
|
||||
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) {
|
||||
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("Cache-Control", fmt.Sprintf("max-age=%d, public", conf.TTL))
|
||||
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 {
|
||||
rw.Header().Set("Vary", headerVaryValue)
|
||||
|
@ -125,6 +125,8 @@ type processingOptions struct {
|
||||
PreferWebP bool
|
||||
EnforceWebP bool
|
||||
|
||||
Filename string
|
||||
|
||||
UsedPresets []string
|
||||
}
|
||||
|
||||
@ -635,6 +637,16 @@ func applyCacheBusterOption(po *processingOptions, args []string) error {
|
||||
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 {
|
||||
switch name {
|
||||
case "format", "f", "ext":
|
||||
@ -709,6 +721,10 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
|
||||
if err := applyCacheBusterOption(po, args); err != nil {
|
||||
return err
|
||||
}
|
||||
case "filename", "fn":
|
||||
if err := applyFilenameOption(po, args); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("Unknown processing option: %s", name)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user