Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete renamed old files at the end of request. #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 72 additions & 22 deletions Model/MediaStorage/File/Storage/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class S3 extends DataObject
*/
private $objects = [];

public static $scheduledForDeletion = [];

/**
* @param DataHelper $helper
* @param \Magento\MediaStorage\Helper\File\Media $mediaHelper
Expand Down Expand Up @@ -101,6 +103,9 @@ public function __construct(
}

$this->client = new \Aws\S3\S3Client($options);

// We will delete renamed old files at the end of request.
register_shutdown_function([$this, 'deleteFilesScheduledForDeletion']);
}

/**
Expand Down Expand Up @@ -166,7 +171,10 @@ public function loadByFilename($filename)
} catch (S3Exception $e) {
$fail = true;

$this->logger->critical($e->getMessage());
$this->logger->error('Failed to load file from S3.', [
$filename,
$e->getMessage(),
]);
}

if ($fail) {
Expand Down Expand Up @@ -287,7 +295,10 @@ public function importFiles(array $files = [])
]));
} catch (\Exception $e) {
$this->errors[] = $e->getMessage();
$this->logger->critical($e);
$this->logger->error('Failed to put file in S3.', [
$file['filename'],
$e->getMessage(),
]);
}
}

Expand All @@ -303,12 +314,19 @@ public function saveFile($filename)
{
$file = $this->mediaHelper->collectFileInfo($this->getMediaBaseDirectory(), $filename);

$this->client->putObject($this->getAllParams([
'Body' => $file['content'],
'Bucket' => $this->getBucket(),
'ContentType' => \GuzzleHttp\Psr7\mimetype_from_filename($file['filename']),
'Key' => $filename,
]));
try{
$this->client->putObject($this->getAllParams([
'Body' => $file['content'],
'Bucket' => $this->getBucket(),
'ContentType' => \GuzzleHttp\Psr7\mimetype_from_filename($file['filename']),
'Key' => $filename,
]));
} catch (S3Exception $e) {
$this->logger->error('Failed to save file in S3.', [
$filename,
$e->getMessage(),
]);
}

return $this;
}
Expand Down Expand Up @@ -371,11 +389,19 @@ public function fileExists($filename)
*/
public function copyFile($oldFilePath, $newFilePath)
{
$this->client->copyObject($this->getAllParams([
'Bucket' => $this->getBucket(),
'Key' => $newFilePath,
'CopySource' => $this->getBucket() . '/' . $oldFilePath,
]));
try{
$this->client->copyObject($this->getAllParams([
'Bucket' => $this->getBucket(),
'Key' => $newFilePath,
'CopySource' => $this->getBucket() . '/' . $oldFilePath,
]));
} catch (S3Exception $e) {
$this->logger->error('Failed to copy file in S3.', [
$oldFilePath,
$newFilePath,
$e->getMessage(),
]);
}

return $this;
}
Expand All @@ -387,16 +413,23 @@ public function copyFile($oldFilePath, $newFilePath)
*/
public function renameFile($oldFilePath, $newFilePath)
{
$this->client->copyObject($this->getAllParams([
'Bucket' => $this->getBucket(),
'Key' => $newFilePath,
'CopySource' => $this->getBucket() . '/' . $oldFilePath,
]));
try{
$this->client->copyObject($this->getAllParams([
'Bucket' => $this->getBucket(),
'Key' => $newFilePath,
'CopySource' => $this->getBucket() . '/' . $oldFilePath,
]));

$this->client->deleteObject([
'Bucket' => $this->getBucket(),
'Key' => $oldFilePath,
]);
// This file can be used for other configurable product as well.
// We will delete it at the end of request.
self::$scheduledForDeletion[$oldFilePath] = $oldFilePath;
} catch (S3Exception $e) {
$this->logger->error('Failed to rename file in S3.', [
$oldFilePath,
$newFilePath,
$e->getMessage(),
]);
}

return $this;
}
Expand Down Expand Up @@ -521,4 +554,21 @@ public function getMediaBaseDirectory()

return $this->mediaBaseDirectory;
}

/**
* Process the renamed old files scheduled for deletion.
*/
public function deleteFilesScheduledForDeletion() {
foreach (self::$scheduledForDeletion as $file) {
try {
$this->deleteFile($file);
unset(self::$scheduledForDeletion[$file]);
}
catch (\Exception $e) {
$this->logger->error('Failed to delete file scheduled for deletion.', [
$file
]);
}
}
}
}