Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/source/media.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,27 @@ 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.

::

<?php
$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.

::

<?php
$newMedia = $media->copyTo('new/directory');
$newMedia = $media->copyTo('new/directory', 'new-filename');
$newMedia = $media->copyToDisk('uploads', 'new/directory', 'new-filename');

Deleting Media
---------------------
Expand Down
2 changes: 2 additions & 0 deletions src/MediaMover.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
64 changes: 62 additions & 2 deletions tests/integration/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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()
Expand Down