Skip to content

Commit 8adf84c

Browse files
authored
Merge pull request #188 from plank/movetodisk-copy-visibility
moveToDisk and copyToDisk now transfer visibility
2 parents 8e67113 + b909cde commit 8adf84c

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

docs/source/media.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,27 @@ Moving Media
6666

6767
You should taking caution if manually changing a media record's attributes, as your record and file could go out of sync.
6868

69-
You can change the location of a media file on disk. You cannot move a media to a different disk this way.
69+
You can change the location of a media file on disk.
7070

7171
::
7272

7373
<?php
7474
$media->move('new/directory');
7575
$media->move('new/directory', 'new-filename');
7676
$media->rename('new-filename');
77+
$media->moveToDisk('uploads', 'new/directory', 'new-filename');
7778

7879
Copying Media
7980
---------------------
8081

81-
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.
82+
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.
8283

8384
::
8485

8586
<?php
8687
$newMedia = $media->copyTo('new/directory');
8788
$newMedia = $media->copyTo('new/directory', 'new-filename');
89+
$newMedia = $media->copyToDisk('uploads', 'new/directory', 'new-filename');
8890

8991
Deleting Media
9092
---------------------

src/MediaMover.php

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function moveToDisk(Media $media, string $disk, string $directory, string
8787

8888
try {
8989
$targetStorage->put($targetPath, $currentStorage->readStream($media->getDiskPath()));
90+
$targetStorage->setVisibility($targetPath, $currentStorage->getVisibility($media->getDiskPath()));
9091
$currentStorage->delete($media->getDiskPath());
9192
} catch (FileNotFoundException $e) {
9293
throw MediaMoveException::fileNotFound($media->disk, $media->getDiskPath(), $e);
@@ -172,6 +173,7 @@ public function copyToDisk(Media $media, string $disk, string $directory, string
172173

173174
try {
174175
$targetStorage->put($targetPath, $currentStorage->readStream($media->getDiskPath()));
176+
$targetStorage->setVisibility($targetPath, $currentStorage->getVisibility($media->getDiskPath()));
175177
} catch (FileNotFoundException $e) {
176178
throw MediaMoveException::fileNotFound($media->disk, $media->getDiskPath(), $e);
177179
}

tests/integration/MediaTest.php

+62-2
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public function test_it_throws_an_exception_if_moving_to_existing_file()
282282
$media1->move('', 'bar.baz');
283283
}
284284

285-
public function test_it_can_be_moved_to_another_disk()
285+
public function test_it_can_be_moved_to_another_disk_public()
286286
{
287287
$this->useFilesystem('tmp');
288288
$this->useFilesystem('uploads');
@@ -297,15 +297,72 @@ public function test_it_can_be_moved_to_another_disk()
297297
]);
298298
$original_path = $media->getAbsolutePath();
299299
$this->seedFileForMedia($media);
300+
$media->makePublic();
301+
302+
$media->moveToDisk('uploads', 'alpha/beta', 'gamma');
303+
$this->assertEquals('uploads', $media->disk);
304+
$this->assertEquals('alpha/beta/gamma.baz', $media->getDiskPath());
305+
$this->assertTrue($media->fileExists());
306+
$this->assertFalse(file_exists($original_path));
307+
$this->assertTrue($media->isVisible());
308+
}
309+
310+
public function test_it_can_be_moved_to_another_disk_private()
311+
{
312+
$this->useFilesystem('tmp');
313+
$this->useFilesystem('uploads');
314+
315+
$this->useDatabase();
316+
317+
$media = $this->makeMedia([
318+
'disk' => 'tmp',
319+
'directory' => 'foo',
320+
'filename' => 'bar',
321+
'extension' => 'baz'
322+
]);
323+
$original_path = $media->getAbsolutePath();
324+
$this->seedFileForMedia($media);
325+
$media->makePrivate();
300326

301327
$media->moveToDisk('uploads', 'alpha/beta', 'gamma');
302328
$this->assertEquals('uploads', $media->disk);
303329
$this->assertEquals('alpha/beta/gamma.baz', $media->getDiskPath());
304330
$this->assertTrue($media->fileExists());
305331
$this->assertFalse(file_exists($original_path));
332+
$this->assertFalse($media->isVisible());
333+
}
334+
335+
public function test_it_can_be_copied_to_another_disk_public()
336+
{
337+
$this->useFilesystem('tmp');
338+
$this->useFilesystem('uploads');
339+
340+
$this->useDatabase();
341+
342+
$media = $this->makeMedia([
343+
'disk' => 'tmp',
344+
'directory' => 'foo',
345+
'filename' => 'bar',
346+
'extension' => 'baz'
347+
]);
348+
$original_path = $media->getAbsolutePath();
349+
$this->seedFileForMedia($media);
350+
$media->makePublic();
351+
352+
$newMedia = $media->copyToDisk('uploads', 'alpha/beta', 'gamma');
353+
$this->assertEquals('uploads', $newMedia->disk);
354+
$this->assertEquals('alpha/beta/gamma.baz', $newMedia->getDiskPath());
355+
$this->assertTrue($newMedia->fileExists());
356+
$this->assertTrue($newMedia->isVisible());
357+
358+
//original should be unchanged
359+
$this->assertEquals('tmp', $media->disk);
360+
$this->assertEquals('foo/bar.baz', $media->getDiskPath());
361+
$this->assertTrue($media->fileExists());
362+
$this->assertTrue($media->isVisible());
306363
}
307364

308-
public function test_it_can_be_copied_to_another_disk()
365+
public function test_it_can_be_copied_to_another_disk_private()
309366
{
310367
$this->useFilesystem('tmp');
311368
$this->useFilesystem('uploads');
@@ -320,16 +377,19 @@ public function test_it_can_be_copied_to_another_disk()
320377
]);
321378
$original_path = $media->getAbsolutePath();
322379
$this->seedFileForMedia($media);
380+
$media->makePrivate();
323381

324382
$newMedia = $media->copyToDisk('uploads', 'alpha/beta', 'gamma');
325383
$this->assertEquals('uploads', $newMedia->disk);
326384
$this->assertEquals('alpha/beta/gamma.baz', $newMedia->getDiskPath());
327385
$this->assertTrue($newMedia->fileExists());
386+
$this->assertFalse($newMedia->isVisible());
328387

329388
//original should be unchanged
330389
$this->assertEquals('tmp', $media->disk);
331390
$this->assertEquals('foo/bar.baz', $media->getDiskPath());
332391
$this->assertTrue($media->fileExists());
392+
$this->assertFalse($media->isVisible());
333393
}
334394

335395
public function test_it_can_access_file_contents()

0 commit comments

Comments
 (0)