diff --git a/docs/source/media.rst b/docs/source/media.rst index 53c9f293..e0a4c3f9 100644 --- a/docs/source/media.rst +++ b/docs/source/media.rst @@ -66,7 +66,7 @@ Moving Media You should taking caution if manually changing a media record's attributes, as your record and file could go out of sync. -You can change the location of a media file on disk. You cannot move a media to a different disk this way. +You can change the location of a media file on disk. :: @@ -74,17 +74,19 @@ You can change the location of a media file on disk. You cannot move a media to $media->move('new/directory'); $media->move('new/directory', 'new-filename'); $media->rename('new-filename'); + $media->moveToDisk('uploads', 'new/directory', 'new-filename'); Copying Media --------------------- -You can duplicate a media file to a different location on disk with the ``copyTo()`` method. Doing so will create a new `Media` record for the new file. If a filename is not provided, the new file will copy the original filename. +You can duplicate a media file to a different location on disk with the ``copyTo()`` method. Doing so will create a new `Media` record for the new file. If a filename is not provided, the new file will copy the original filename. :: copyTo('new/directory'); $newMedia = $media->copyTo('new/directory', 'new-filename'); + $newMedia = $media->copyToDisk('uploads', 'new/directory', 'new-filename'); Deleting Media --------------------- diff --git a/src/MediaMover.php b/src/MediaMover.php index f0a7a4d3..8ac9980b 100644 --- a/src/MediaMover.php +++ b/src/MediaMover.php @@ -87,6 +87,7 @@ public function moveToDisk(Media $media, string $disk, string $directory, string try { $targetStorage->put($targetPath, $currentStorage->readStream($media->getDiskPath())); + $targetStorage->setVisibility($targetPath, $currentStorage->getVisibility($media->getDiskPath())); $currentStorage->delete($media->getDiskPath()); } catch (FileNotFoundException $e) { throw MediaMoveException::fileNotFound($media->disk, $media->getDiskPath(), $e); @@ -172,6 +173,7 @@ public function copyToDisk(Media $media, string $disk, string $directory, string try { $targetStorage->put($targetPath, $currentStorage->readStream($media->getDiskPath())); + $targetStorage->setVisibility($targetPath, $currentStorage->getVisibility($media->getDiskPath())); } catch (FileNotFoundException $e) { throw MediaMoveException::fileNotFound($media->disk, $media->getDiskPath(), $e); } diff --git a/tests/integration/MediaTest.php b/tests/integration/MediaTest.php index fcb2b976..15264a05 100644 --- a/tests/integration/MediaTest.php +++ b/tests/integration/MediaTest.php @@ -282,7 +282,7 @@ public function test_it_throws_an_exception_if_moving_to_existing_file() $media1->move('', 'bar.baz'); } - public function test_it_can_be_moved_to_another_disk() + public function test_it_can_be_moved_to_another_disk_public() { $this->useFilesystem('tmp'); $this->useFilesystem('uploads'); @@ -297,15 +297,72 @@ public function test_it_can_be_moved_to_another_disk() ]); $original_path = $media->getAbsolutePath(); $this->seedFileForMedia($media); + $media->makePublic(); + + $media->moveToDisk('uploads', 'alpha/beta', 'gamma'); + $this->assertEquals('uploads', $media->disk); + $this->assertEquals('alpha/beta/gamma.baz', $media->getDiskPath()); + $this->assertTrue($media->fileExists()); + $this->assertFalse(file_exists($original_path)); + $this->assertTrue($media->isVisible()); + } + + public function test_it_can_be_moved_to_another_disk_private() + { + $this->useFilesystem('tmp'); + $this->useFilesystem('uploads'); + + $this->useDatabase(); + + $media = $this->makeMedia([ + 'disk' => 'tmp', + 'directory' => 'foo', + 'filename' => 'bar', + 'extension' => 'baz' + ]); + $original_path = $media->getAbsolutePath(); + $this->seedFileForMedia($media); + $media->makePrivate(); $media->moveToDisk('uploads', 'alpha/beta', 'gamma'); $this->assertEquals('uploads', $media->disk); $this->assertEquals('alpha/beta/gamma.baz', $media->getDiskPath()); $this->assertTrue($media->fileExists()); $this->assertFalse(file_exists($original_path)); + $this->assertFalse($media->isVisible()); + } + + public function test_it_can_be_copied_to_another_disk_public() + { + $this->useFilesystem('tmp'); + $this->useFilesystem('uploads'); + + $this->useDatabase(); + + $media = $this->makeMedia([ + 'disk' => 'tmp', + 'directory' => 'foo', + 'filename' => 'bar', + 'extension' => 'baz' + ]); + $original_path = $media->getAbsolutePath(); + $this->seedFileForMedia($media); + $media->makePublic(); + + $newMedia = $media->copyToDisk('uploads', 'alpha/beta', 'gamma'); + $this->assertEquals('uploads', $newMedia->disk); + $this->assertEquals('alpha/beta/gamma.baz', $newMedia->getDiskPath()); + $this->assertTrue($newMedia->fileExists()); + $this->assertTrue($newMedia->isVisible()); + + //original should be unchanged + $this->assertEquals('tmp', $media->disk); + $this->assertEquals('foo/bar.baz', $media->getDiskPath()); + $this->assertTrue($media->fileExists()); + $this->assertTrue($media->isVisible()); } - public function test_it_can_be_copied_to_another_disk() + public function test_it_can_be_copied_to_another_disk_private() { $this->useFilesystem('tmp'); $this->useFilesystem('uploads'); @@ -320,16 +377,19 @@ public function test_it_can_be_copied_to_another_disk() ]); $original_path = $media->getAbsolutePath(); $this->seedFileForMedia($media); + $media->makePrivate(); $newMedia = $media->copyToDisk('uploads', 'alpha/beta', 'gamma'); $this->assertEquals('uploads', $newMedia->disk); $this->assertEquals('alpha/beta/gamma.baz', $newMedia->getDiskPath()); $this->assertTrue($newMedia->fileExists()); + $this->assertFalse($newMedia->isVisible()); //original should be unchanged $this->assertEquals('tmp', $media->disk); $this->assertEquals('foo/bar.baz', $media->getDiskPath()); $this->assertTrue($media->fileExists()); + $this->assertFalse($media->isVisible()); } public function test_it_can_access_file_contents()