diff --git a/src/Exceptions/UrlCouldNotBeOpened.php b/src/Exceptions/UrlCouldNotBeOpened.php new file mode 100644 index 000000000..e04bcea64 --- /dev/null +++ b/src/Exceptions/UrlCouldNotBeOpened.php @@ -0,0 +1,9 @@ +create($this, $file); } + public function addMediaFromUrl($url) + { + if ($stream = @fopen($url, 'r')) { + $tmpFile = tempnam(sys_get_temp_dir(), 'media-library'); + file_put_contents($tmpFile, $stream); + + return app(FileAdderFactory::class)->create($this, $tmpFile); + } + + throw new UrlCouldNotBeOpened; + + } + /** * Copy a file to the medialibrary. * diff --git a/tests/FileAdder/IntegrationTest.php b/tests/FileAdder/IntegrationTest.php index a6e977436..1ef0de27b 100644 --- a/tests/FileAdder/IntegrationTest.php +++ b/tests/FileAdder/IntegrationTest.php @@ -132,6 +132,23 @@ public function it_can_add_an_upload_to_the_medialibrary() $this->assertFileExists($this->getMediaDirectory($media->id.'/'.$media->file_name)); } + /** + * @test + */ + public function it_can_add_a_remote_file_to_the_medialibrary() + { + $url = $this->getTestUrl(); + $filename = basename(parse_url($url, PHP_URL_PATH)); + + $media = $this->testModel->addMediaFromUrl($url) + ->usingName('test') + ->usingFileName($filename) + ->toMediaLibrary(); + + $this->assertEquals('test', $media->name); + $this->assertFileExists($this->getMediaDirectory($media->id.'/'.$filename)); + } + /** * @test */ diff --git a/tests/TestCase.php b/tests/TestCase.php index 5d664d85e..7caede05b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -131,4 +131,9 @@ public function getTestJpg() { return $this->getTestFilesDirectory('test.jpg'); } + + public function getTestUrl() + { + return 'http://medialibrary.spatie.be/assets/images/mountain.jpg'; + } }