-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the HttpFacadeDownloader (#3505)
* Adds the HttpFacadeDownloader * Added documentation
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Spatie\MediaLibrary\Downloaders; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use Spatie\MediaLibrary\MediaCollections\Exceptions\UnreachableUrl; | ||
|
||
class HttpFacadeDownloader implements Downloader | ||
{ | ||
public function getTempFile(string $url): string | ||
{ | ||
$temporaryFile = tempnam(sys_get_temp_dir(), 'media-library'); | ||
|
||
Http::withUserAgent('Spatie MediaLibrary') | ||
->throw(fn () => throw new UnreachableUrl($url)) | ||
->sink($temporaryFile) | ||
->get($url); | ||
|
||
return $temporaryFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
use Illuminate\Http\Client\Request; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
it('can save a url to a temp location', function () { | ||
$url = 'https://example.com'; | ||
|
||
\Illuminate\Support\Facades\Http::shouldReceive('withUserAgent') | ||
->with('Spatie MediaLibrary') | ||
->once() | ||
->andReturnSelf() | ||
->getMock() | ||
->shouldReceive('throw') | ||
->once() | ||
->andReturnSelf() | ||
->getMock() | ||
->shouldReceive('sink') | ||
->once() | ||
->andReturnSelf() | ||
->getMock() | ||
->shouldReceive('get') | ||
->with($url) | ||
->once(); | ||
|
||
$downloader = new \Spatie\MediaLibrary\Downloaders\HttpFacadeDownloader(); | ||
|
||
$result = $downloader->getTempFile($url); | ||
|
||
expect($result)->toBeString(); | ||
}); | ||
|
||
it('can be mocked easily for tests', function () { | ||
$url = 'https://example.com'; | ||
|
||
Http::fake([ | ||
// Stub a JSON response for GitHub endpoints... | ||
'https://example.com' => Http::response('::file::'), | ||
]); | ||
|
||
$downloader = new \Spatie\MediaLibrary\Downloaders\HttpFacadeDownloader(); | ||
|
||
$result = $downloader->getTempFile($url); | ||
|
||
expect($result) | ||
->toBeString() | ||
->and($result) | ||
->toBeFile() | ||
->and(\Illuminate\Support\Facades\File::get($result)) | ||
->toBe('::file::'); | ||
|
||
Http::assertSent(function (Request $request) { | ||
return $request->url() == 'https://example.com'; | ||
}); | ||
}); |