I added https://github.com/morawskim/faker-images to laravel 9 site and when I try get image from this lib and to upload it with Medialibrary
$faker = \Faker\Factory::create();
$faker->addProvider(new \Mmo\Faker\PicsumProvider($faker));
$faker->addProvider(new \Mmo\Faker\LoremSpaceProvider($faker));
$pageUploadedImageFile= $faker->picsumUrl(400, 400);
$imageMedia = $this->modelItem
->addMediaFromUrl($pageUploadedImageFile)
->usingFileName($imageFilename)
->toMediaCollection('media');
where $pageUploadedImageFile has value like :
https://picsum.photos/400/400?random=83611
which is readable in my browser
But I got error :
Spatie\MediaLibrary\MediaCollections\Exceptions\UnreachableUrl
Url `https://picsum.photos/400/400?random=83611` cannot be reached
at vendor/spatie/laravel-medialibrary/src/MediaCollections/Exceptions/UnreachableUrl.php:9
5▕ class UnreachableUrl extends FileCannotBeAdded
6▕ {
7▕ public static function create(string $url): self
8▕ {
➜ 9▕ return new static("Url `{$url}` cannot be reached");
10▕ }
11▕ }
12▕
What is wrong and how that can be fixed ?
"laravel/framework": "^9.26.1",
"laravel/tinker": "^2.7.2",
"spatie/laravel-medialibrary": "^10.4.4",
"spatie/laravel-permission": "^5.5.5",
"squizlabs/php_codesniffer": "*"
"barryvdh/laravel-ide-helper": "^2.12",
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.0.1",
"mmo/faker-images": "^0.6.0",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^6.1",
UPDATED PART : I tried to debug source of the package and I found that error israised in file src/Downloaders/DefaultDownloader.php:
<?php
namespace Spatie\MediaLibrary\Downloaders;
use Spatie\MediaLibrary\MediaCollections\Exceptions\UnreachableUrl;
class DefaultDownloader implements Downloader
{
public function getTempFile(string $url): string
{
\Log::info( ' getTempFile $url::');
\Log::info( $url);
$stream = fopen($url, 'r');
\Log::info( ' getTempFile $stream::');
\Log::info( $stream);
if (! $stream ) {
throw UnreachableUrl::create($url);
}
$temporaryFile = tempnam(sys_get_temp_dir(), 'media-library');
file_put_contents($temporaryFile, $stream);
return $temporaryFile;
}
}
and I see in log
[2022-09-29 04:10:51] local.INFO: getTempFile $url::
[2022-09-29 04:10:51] local.INFO: https://picsum.photos/800/600?random=86375
[2022-09-29 04:10:51] local.ERROR: fopen(https://picsum.photos/800/600?random=86375): Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
{"userId":7,"exception":"[object] (ErrorException(code: 0): fopen(https://picsum.photos/800/600?random=86375): Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
When I copypaste code from https://picsum.photos/800/600?random=86375 into browser I got valid image. Is it some protection on side of Spatie\MediaLibrary ?
Thanks in advance!