Skip to content

Commit

Permalink
S3 pre-signed URL: error with unicode filename catalyst#455
Browse files Browse the repository at this point in the history
  • Loading branch information
PhucNguyen0311 authored and marxjohnson committed Jun 15, 2022
1 parent a277b86 commit 9a79b34
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions classes/local/store/s3/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,10 @@ public function generate_presigned_url($contenthash, $headers = array()) {
/**
* @param string $contenthash
* @param array $headers
* @param bool $nicefilename
* @return signed_url
*/
private function generate_presigned_url_s3($contenthash, $headers) {
private function generate_presigned_url_s3($contenthash, array $headers = [], $nicefilename = true) {
$contentdisposition = manager::get_header($headers, 'Content-Disposition');
if ($contentdisposition !== '') {
$params['ResponseContentDisposition'] = $contentdisposition;
Expand All @@ -503,14 +504,12 @@ private function generate_presigned_url_s3($contenthash, $headers) {
$params['Bucket'] = $this->bucket;
$params['Key'] = $this->bucketkeyprefix . $key;

$contentdisposition = manager::get_header($headers, 'Content-Disposition');
if ($contentdisposition !== '') {
$params['ResponseContentDisposition'] = $contentdisposition;
}

$contenttype = manager::get_header($headers, 'Content-Type');
if ($contenttype !== '') {
$params['ResponseContentType'] = $contenttype;
if ($nicefilename) {
$result = $this->get_nice_filename($headers);
if (!empty($result)) {
$params['ResponseContentDisposition'] = $result['Content-Disposition'] . '; ' . $result['filename'];
$params['ResponseContentType'] = $result['Content-Type'];
}
}

$command = $this->client->getCommand('GetObject', $params);
Expand All @@ -534,7 +533,14 @@ private function generate_presigned_url_cloudfront($contenthash, array $headers
$expires = $this->get_expiration_time(time(), manager::get_header($headers, 'Expires'));

if ($nicefilename) {
$key .= $this->get_nice_filename($headers);
$result = $this->get_nice_filename($headers);
if (!empty($result)) {
$key .= '?response-content-disposition=' .
rawurlencode($result['Content-Disposition'] . ';' . $result['filename']) .
'&response-content-type=' . rawurlencode($result['Content-Type']);
} else {
$key .= '';
}
}
$resource = $this->config->cloudfrontresourcedomain . '/' . $key;
// This is the id of the Cloudfront key pair you generated.
Expand Down Expand Up @@ -572,11 +578,12 @@ private function generate_presigned_url_cloudfront($contenthash, array $headers

/**
* @param $headers
* @return string
* @return array
*/
private function get_nice_filename($headers) {
// We are trying to deliver original filename rather than hash filename to client.
$originalfilename = '';
$result = [];
$contentdisposition = trim(manager::get_header($headers, 'Content-Disposition'));
$originalcontenttype = trim(manager::get_header($headers, 'Content-Type'));

Expand All @@ -596,12 +603,12 @@ private function get_nice_filename($headers) {
}

if (!empty($originalfilename)) {
return '?response-content-disposition=' .
rawurlencode($contentdisposition . ';filename="' . utf8_encode($originalfilename) . '"') .
'&response-content-type=' . rawurlencode($originalcontenttype);
$result['Content-Disposition'] = $contentdisposition;
$result['filename'] = 'filename="' . utf8_encode($originalfilename) . '"';
$result['Content-Type'] = $originalcontenttype;
}
}
return '';
return $result;
}

/**
Expand Down

0 comments on commit 9a79b34

Please sign in to comment.