Skip to content

Commit

Permalink
Merge pull request #3707 from esadewater/patch-1
Browse files Browse the repository at this point in the history
Fix renaming files with custom model
  • Loading branch information
timvandijck authored Nov 8, 2024
2 parents a4c097d + 9f2bac4 commit 39b7b54
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/MediaCollections/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ protected function renameMediaFile(Media $media): void

protected function renameConversionFiles(Media $media): void
{
$mediaWithOldFileName = config('media-library.media_model')::find($media->id);
$mediaWithOldFileName = config('media-library.media_model')::find($media->getKey());
$mediaWithOldFileName->file_name = $mediaWithOldFileName->getOriginal('file_name');

$conversionDirectory = $this->getConversionDirectory($media);
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/Media/RenameTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use Spatie\MediaLibrary\MediaLibraryServiceProvider;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestCustomMediaWithCustomKeyName;

it('will rename the file if it is changed on the media object', function () {
$testFile = $this->getTestFilesDirectory('test.jpg');

Expand All @@ -14,6 +17,26 @@
$this->assertFileExists($this->getMediaDirectory($media->id.'/test-new-name.jpg'));
});

it('will rename the file with a custom model with custom key name', function () {
config()->set('media-library.media_model', TestCustomMediaWithCustomKeyName::class);

(new MediaLibraryServiceProvider(app()))->register()->boot();

$this->setUpDatabaseCustomKeyName();

$testFile = $this->getTestFilesDirectory('test.jpg');

$media = $this->testModel->addMedia($testFile)->toMediaCollection();

$this->assertFileExists($this->getMediaDirectory($media->getKey().'/test.jpg'));

$media->file_name = 'test-new-name.jpg';
$media->save();

$this->assertFileDoesNotExist($this->getMediaDirectory($media->getKey().'/test.jpg'));
$this->assertFileExists($this->getMediaDirectory($media->getKey().'/test-new-name.jpg'));
});

it('will rename conversions', function () {
$testFile = $this->getTestFilesDirectory('test.jpg');

Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use CreateTemporaryUploadsTable;
use Dotgetenv\Dotgetenv;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\File;
use Orchestra\Testbench\TestCase as Orchestra;
use Schema;
use Spatie\MediaLibrary\MediaLibraryServiceProvider;
use Spatie\MediaLibrary\Support\MediaLibraryPro;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModel;
Expand Down Expand Up @@ -153,6 +155,21 @@ protected function setUpDatabase($app)
$mediaTableMigration->up();
}

protected function setUpDatabaseCustomKeyName()
{
$customKeyNameMigration = new class extends Migration
{
public function up()
{
Schema::table('media', function (Blueprint $table) {
$table->renameColumn('id', 'custom_key_id');
});
}
};

$customKeyNameMigration->up();
}

protected function setUpTempTestFiles()
{
$this->initializeDirectory($this->getTestFilesDirectory());
Expand Down
12 changes: 12 additions & 0 deletions tests/TestSupport/TestModels/TestCustomMediaWithCustomKeyName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Spatie\MediaLibrary\Tests\TestSupport\TestModels;

use Spatie\MediaLibrary\MediaCollections\Models\Media;

class TestCustomMediaWithCustomKeyName extends Media
{
protected $table = 'media';

protected $primaryKey = 'custom_key_id';
}

0 comments on commit 39b7b54

Please sign in to comment.