Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix renaming files with custom model #3707

Merged
merged 2 commits into from
Nov 8, 2024
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
2 changes: 1 addition & 1 deletion src/MediaCollections/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,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';
}