Image Download Function
Below is an example of a function you can use to set the Content-Disposition HTTP header when a URL's query string includes "dl=1", i.e. when you want a user's browser to download an image rather than display it.
var validSuffixes = [".jpg", ".png", ".jpeg", ".gif", ".tif", ".tiff"];
function handler(event) {
var queryString = event.request.querystring.dl;
var uri = event.request.uri;
var lastDot = uri.lastIndexOf(".");
var validSuffix = false;
if (lastDot !== -1) {
var uriSuffix = uri.substring(lastDot).toLowerCase();
validSuffix = validSuffixes.includes(uriSuffix);
}
if (queryString != null && queryString.value == "1" && validSuffix) {
var response = event.response;
response.headers["content-disposition"] = {
value: "attachment"
};
return response;
}
return event.response;
}