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

[3.5] Add feature to fetch placeholder images from remote URLs #7368

Merged
merged 1 commit into from Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Storage/Database/Prefill/ApiClient.php
Expand Up @@ -3,6 +3,7 @@
namespace Bolt\Storage\Database\Prefill;

use GuzzleHttp\Client;
use Psr\Http\Message\StreamInterface;

/**
* Handles fetching prefill text content from an API service.
Expand Down Expand Up @@ -36,4 +37,18 @@ public function get($request, $base = 'http://loripsum.net/api/')

return $this->client->get($uri, ['timeout' => 10])->getBody();
}

/**
* Fetches an image from a remote source.
*
* @param string $url URL of the remote image to fetch
*
* @return StreamInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a use Psr\Http\Message\StreamInterface; added

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

*/
public function getImage($url)
{
$url = str_replace('__random__', rand(100000, 999999), $url);

return $this->client->get($url, ['timeout' => 10])->getBody();
}
}
46 changes: 42 additions & 4 deletions src/Storage/Database/Prefill/RecordContentGenerator.php
Expand Up @@ -3,11 +3,13 @@
namespace Bolt\Storage\Database\Prefill;

use Bolt\Collection\ImmutableBag;
use Bolt\Filesystem\Exception\IOException;
use Bolt\Filesystem\FilesystemInterface;
use Bolt\Storage\Collection;
use Bolt\Storage\Entity;
use Bolt\Storage\Repository\ContentRepository;
use Cocur\Slugify\Slugify;
use GuzzleHttp\Exception\RequestException;

/**
* Create a generated set of pre-filled records for a ContentType.
Expand Down Expand Up @@ -278,12 +280,14 @@ private function addText(Entity\Content $contentEntity, $fieldName, $type, array
*/
private function addJson(Entity\Content $contentEntity, $fieldName, $type)
{
$contentType = $this->repository->getEntityManager()->getContentType($contentEntity->getContenttype());
$placeholder = isset($contentType['fields'][$fieldName]['placeholder']) ? $contentType['fields'][$fieldName]['placeholder'] : null;
$value = null;
if ($type === 'image') {
$value = $this->getRandomImage($type);
$value = $this->getRandomImage($type, $placeholder);
} elseif ($type === 'imagelist') {
for ($i = 1; $i <= 3; $i++) {
$value[] = $this->getRandomImage($type);
$value[] = $this->getRandomImage($type, $placeholder);
}
} elseif ($type === 'filelist' || $type === 'templatefields') {
$value = [];
Expand Down Expand Up @@ -449,13 +453,19 @@ private function getRandomTags($count = 5)
/**
* Get a random image.
*
* @param string $type
* @param string $type
* @param string|null $placeholder
*
* @return array|null
*/
private function getRandomImage($type)
private function getRandomImage($type, $placeholder)
{
$pathKey = $type === 'image' ? 'file' : 'filename';

if ($placeholder && ($filename = $this->fetchPlaceholder($placeholder))) {
return [$pathKey => $filename, 'title' => 'placeholder', 'alt' => 'placeholder'];
}

$images = $this->getImageFiles();
if (empty($images)) {
return null;
Expand All @@ -467,4 +477,32 @@ private function getRandomImage($type)

return [$pathKey => $image->getPath(), 'title' => $title, 'alt' => $title];
}

/**
* Attempt to fetch a placeholder image from a remote URL.
*
* @param string $placeholder
*
* @return array|bool
*/
private function fetchPlaceholder($placeholder)
{
try {
$image = $this->apiClient->getImage($placeholder);
} catch (RequestException $e) {
// We couldn't fetch the file, fall back to default behaviour
return false;
}

$filename = sprintf('placeholder_%s.jpg', substr(md5(microtime()), 0, 12));

try {
$this->filesystem->put('files://' . $filename, $image);
} catch (IOException $e) {
// We couldn't save the file, fall back to default behaviour
return false;
}

return $filename;
}
}