Skip to content

Commit

Permalink
Resolved #3335
Browse files Browse the repository at this point in the history
  • Loading branch information
andris-sevcenko committed Oct 11, 2018
1 parent 7adee0d commit c6ac3a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added
- Added `craft\helpers\MigrationHelper::findForeignKey()`.
- Added the `cp.globals.edit` and `cp.globals.edit.content` template hooks to the Edit Global Set page. ([#3356](https://github.com/craftcms/cms/pull/3356))
- Added garbage collecting when generating fetching local sources for remote assets. ([#3335](https://github.com/craftcms/cms/pull/3335))

### Changed
- It’s now possible to load a Create Entry page with a specific user preselected in the Author field, using a new `authorId` query string param. ([#3326](https://github.com/craftcms/cms/pull/3326))
Expand Down
36 changes: 26 additions & 10 deletions src/services/AssetTransforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,29 +847,45 @@ public function getLocalImageSource(Asset $asset): string
Craft::warning("Unable to delete the file \"{$imageSourcePath}\": " . $e->getMessage(), __METHOD__);
}

$tempFilename = uniqid(pathinfo($asset->filename, PATHINFO_FILENAME), true) . '.' . $asset->getExtension();
$tempPath = Craft::$app->getPath()->getTempPath() . DIRECTORY_SEPARATOR . $tempFilename;

$volume->saveFileLocally($asset->getPath(), $tempPath);
$prefix = pathinfo($asset->filename, PATHINFO_FILENAME) . '.delimiter.';
$extension = $asset->getExtension();
$tempFilename = uniqid($prefix, true) . '.' . $extension;
$tempPath = Craft::$app->getPath()->getTempPath();
$tempFilePath = $tempPath . DIRECTORY_SEPARATOR . $tempFilename;

// Fetch a list of existing temp files for this image.
$files = FileHelper::findFiles($tempPath, [
'only' => [
$prefix . '*' . '.' . $extension
]
]);

// And clean them up.
if (!empty($files)) {
foreach ($files as $filePath) {
FileHelper::unlink($filePath);
}
}
$volume->saveFileLocally($asset->getPath(), $tempFilePath);

if (!is_file($tempPath) || filesize($tempPath) === 0) {
if (!is_file($tempFilePath) || filesize($tempFilePath) === 0) {
try {
FileHelper::unlink($tempPath);
FileHelper::unlink($tempFilePath);
} catch (ErrorException $e) {
Craft::warning("Unable to delete the file \"{$tempPath}\": " . $e->getMessage(), __METHOD__);
Craft::warning("Unable to delete the file \"{$tempFilePath}\": " . $e->getMessage(), __METHOD__);
}
throw new VolumeException(Craft::t('app', 'Tried to download the source file for image “{file}”, but it was 0 bytes long.',
['file' => $asset->filename]));
}

$this->storeLocalSource($tempPath, $imageSourcePath);
$this->storeLocalSource($tempFilePath, $imageSourcePath);

// Delete the leftover data.
$this->queueSourceForDeletingIfNecessary($imageSourcePath);
try {
FileHelper::unlink($tempPath);
FileHelper::unlink($tempFilePath);
} catch (ErrorException $e) {
Craft::warning("Unable to delete the file \"{$tempPath}\": " . $e->getMessage(), __METHOD__);
Craft::warning("Unable to delete the file \"{$tempFilePath}\": " . $e->getMessage(), __METHOD__);
}
}
}
Expand Down

0 comments on commit c6ac3a3

Please sign in to comment.