Skip to content

Commit

Permalink
delete existing images
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed Jun 16, 2021
1 parent b5c96d5 commit c6ecf4f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Instagram Feed Changelog

## 1.1.5 - 2021-06-16

### Fixed
- If you use a volume to store the images, the first time displaying the images failed.
- In a scenario, where the image files already exists on the volume but without an asset entry in Craft, the files on the volume will be deleted and downloaded again.

## 1.1.4 - 2021-06-11

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "codemonauts/craft-instagram-feed",
"description": "Craft CMS plugin to receive Instragram feed data as variable in templates.",
"version": "1.1.4",
"version": "1.1.5",
"type": "craft-plugin",
"keywords": [
"craft",
Expand Down
46 changes: 27 additions & 19 deletions src/services/InstagramService.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,31 +436,38 @@ private function storeImages(array $items): array
foreach ($items as $key => $item) {
try {
if ($settings->useVolume) {
// Origin image
$response = $client->get($item['imageSource'], $guzzleOptions);
$filename = FileHelper::sanitizeFilename($item['shortcode']) . '.jpg';
// Check if asset exists in database
$existingAsset = Asset::findOne(['folderId' => $folderId, 'filename' => $filename]);
if ($existingAsset) {
$items[$key]['assetId'] = $existingAsset->id;
} else {
$tempFilePath = $tempPath . '/' . $filename;
file_put_contents($tempFilePath, (string)$response->getBody());

$asset = new Asset();
$asset->tempFilePath = $tempFilePath;
$asset->filename = $filename;
$asset->newFolderId = $folderId;
$asset->volumeId = $volume->id;
$asset->avoidFilenameConflicts = false;
$asset->setScenario(Asset::SCENARIO_CREATE);

if (!Craft::$app->getElements()->saveElement($asset)) {
Craft::error('Could not save Instagram image to volume: ' . implode(', ', $asset->getErrorSummary(true)));
continue;
}
continue;
}

$items[$key]['assetId'] = $asset->id;
// Check if asset exists on volume and delete it
if ($volume->fileExists($subpath . '/' . $filename)) {
$volume->deleteFile($subpath . '/' . $filename);
}

// Fetch origin and store on volume
$response = $client->get($item['imageSource'], $guzzleOptions);
$tempFilePath = $tempPath . '/' . $filename;
file_put_contents($tempFilePath, (string)$response->getBody());

$asset = new Asset();
$asset->tempFilePath = $tempFilePath;
$asset->filename = $filename;
$asset->newFolderId = $folderId;
$asset->volumeId = $volume->id;
$asset->avoidFilenameConflicts = false;
$asset->setScenario(Asset::SCENARIO_CREATE);

if (!Craft::$app->getElements()->saveElement($asset)) {
Craft::error('Could not save Instagram image to volume: ' . implode(', ', $asset->getErrorSummary(true)));
continue;
}

$items[$key]['assetId'] = $asset->id;
} else {
// Origin image
$filename = $item['shortcode'] . '_full.jpg';
Expand All @@ -470,6 +477,7 @@ private function storeImages(array $items): array
}
$response = $client->get($item['imageSource'], $guzzleOptions);
FileHelper::writeToFile($filePath, (string)$response->getBody());

// Thumbnail
$filename = $item['shortcode'] . '_thumb.jpg';
$filePath = $folder . DIRECTORY_SEPARATOR . $filename;
Expand Down

0 comments on commit c6ecf4f

Please sign in to comment.