Skip to content

Commit

Permalink
Add direct support for Expires and Cache-Control headers
Browse files Browse the repository at this point in the history
  • Loading branch information
thaiphan committed Jun 6, 2018
1 parent 54558e0 commit c80a29c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
10 changes: 10 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public function getBucket()
return $this->scopeConfig->getValue('thai_s3/general/bucket');
}

public function getExpires()
{
return $this->scopeConfig->getValue('thai_s3/headers/expires');
}

public function getCacheControl()
{
return $this->scopeConfig->getValue('thai_s3/headers/cache_control');
}

public function getCustomHeaders()
{
return $this->scopeConfig->getValue('thai_s3/headers/custom_headers');
Expand Down
44 changes: 28 additions & 16 deletions Model/MediaStorage/File/Storage/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,12 @@ public function importFiles(array $files = [])
{
foreach ($files as $file) {
try {
$this->client->putObject([
'ACL' => 'public-read',
$this->client->putObject($this->getAllParams([
'Body' => $file['content'],
'Bucket' => $this->getBucket(),
'ContentType' => \GuzzleHttp\Psr7\mimetype_from_filename($file['filename']),
'Key' => $file['directory'] . '/' . $file['filename'],
'Metadata' => $this->getMetadata(),
]);
]));
} catch (\Exception $e) {
$this->errors[] = $e->getMessage();
$this->logger->critical($e);
Expand All @@ -224,18 +222,36 @@ public function saveFile($filename)
{
$file = $this->mediaHelper->collectFileInfo($this->getMediaBaseDirectory(), $filename);

$this->client->putObject([
'ACL' => 'public-read',
$this->client->putObject($this->getAllParams([
'Body' => $file['content'],
'Bucket' => $this->getBucket(),
'ContentType' => \GuzzleHttp\Psr7\mimetype_from_filename($file['filename']),
'Key' => $filename,
'Metadata' => $this->getMetadata(),
]);
]));

return $this;
}

public function getAllParams(array $headers = [])
{
$headers['ACL'] = 'public-read';

$metadata = $this->getMetadata();
if (count($metadata) > 0) {
$headers['Metadata'] = $metadata;
}

if ($this->helper->getExpires()) {
$headers['Expires'] = $this->helper->getExpires();
}

if ($this->helper->getCacheControl()) {
$headers['CacheControl'] = $this->helper->getCacheControl();
}

return $headers;
}

public function getMetadata()
{
$meta = [];
Expand All @@ -258,26 +274,22 @@ public function fileExists($filename)

public function copyFile($oldFilePath, $newFilePath)
{
$this->client->copyObject([
$this->client->copyObject($this->getAllParams([
'Bucket' => $this->getBucket(),
'Key' => $newFilePath,
'CopySource' => $this->getBucket() . '/' . $oldFilePath,
'ACL' => 'public-read',
'Metadata' => $this->getMetadata(),
]);
]));

return $this;
}

public function renameFile($oldFilePath, $newFilePath)
{
$this->client->copyObject([
$this->client->copyObject($this->getAllParams([
'Bucket' => $this->getBucket(),
'Key' => $newFilePath,
'CopySource' => $this->getBucket() . '/' . $oldFilePath,
'ACL' => 'public-read',
'Metadata' => $this->getMetadata(),
]);
]));

$this->client->deleteObject([
'Bucket' => $this->getBucket(),
Expand Down

0 comments on commit c80a29c

Please sign in to comment.