Browse Source

Allow encoding `filename` with Base64

DarthSim 2 years ago
parent
commit
ee0cb9feda
3 changed files with 18 additions and 4 deletions
  1. 1 0
      CHANGELOG.md
  2. 6 3
      docs/generating_the_url.md
  3. 11 1
      options/processing_options.go

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@
 ## [Unreleased]
 ### Add
 - Add support for `Sec-CH-DPR` and `Sec-CH-Width` client hints.
+- Add support for Base64-encoded `filename` processing option values.
 
 ### Fix
 - Fix detection of dead HTTP/2 connections.

+ 6 - 3
docs/generating_the_url.md

@@ -765,11 +765,14 @@ Default: empty
 ### Filename
 
 ```
-filename:%string
-fn:%string
+filename:%filename:%encoded
+fn:%filename:%encoded
 ```
 
-Defines a filename for the `Content-Disposition` header. When not specified, imgproxy will get the filename from the source url.
+Defines a filename for the `Content-Disposition` header. When not specified, imgproxy will get the filename from the source URL.
+
+* `filename`: escaped or URL-safe Base64-encoded filename to be used in the `Content-Disposition` header
+* `encoded`: _(optionsl)_ identifies if `filename` is Base64-encoded. Set it to `1`, `t`, or `true` if you encoded the `filename` value with URL-safe Base64 encoding.
 
 Default: empty
 

+ 11 - 1
options/processing_options.go

@@ -1,6 +1,7 @@
 package options
 
 import (
+	"encoding/base64"
 	"errors"
 	"fmt"
 	"net/http"
@@ -805,12 +806,21 @@ func applyRawOption(po *ProcessingOptions, args []string) error {
 }
 
 func applyFilenameOption(po *ProcessingOptions, args []string) error {
-	if len(args) > 1 {
+	if len(args) > 2 {
 		return fmt.Errorf("Invalid filename arguments: %v", args)
 	}
 
 	po.Filename = args[0]
 
+	if len(args) > 1 && parseBoolOption(args[1]) {
+		decoded, err := base64.RawURLEncoding.DecodeString(po.Filename)
+		if err != nil {
+			return fmt.Errorf("Invalid filename encoding: %s", err)
+		}
+
+		po.Filename = string(decoded)
+	}
+
 	return nil
 }