Skip to content

Commit

Permalink
Move changeExtension logic into manipulate method #6715
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora committed Oct 3, 2024
1 parent 4938fa2 commit 90674e8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Cms/FileActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ public function changeTemplate(string|null $template): static

$file = $file->update(['template' => $template]);

// rename and/or resize the file if configured by new blueprint
// resize the file if configured by new blueprint
$create = $file->blueprint()->create();
$file = $file->manipulate($create);
$file = $file->changeExtension($file, $create['format'] ?? null);
$file = $file->manipulate($create);

return $file;
});
Expand Down Expand Up @@ -286,7 +285,6 @@ public static function create(array $props, bool $move = false): File

// resize the file on upload if configured
$file = $file->manipulate($create);
$file = $file->changeExtension($file, $create['format'] ?? null);

// store the content if necessary
// (always create files in the default language)
Expand Down Expand Up @@ -339,7 +337,14 @@ public function manipulate(array|null $options = []): static
// generate image file and overwrite it in place
$this->kirby()->thumb($this->root(), $this->root(), $options);

return $this->clone([]);
$file = $this->clone();

// change the file extension if format option configured
if ($format = $options['format'] ?? null) {
$file = $file->changeExtension($file, $format);
}

return $file;
}

/**
Expand Down Expand Up @@ -388,7 +393,6 @@ public function replace(string $source, bool $move = false): static
// apply the resizing/crop options from the blueprint
$create = $file->blueprint()->create();
$file = $file->manipulate($create);
$file = $file->changeExtension($file, $create['format'] ?? null);

// return a fresh clone
return $file->clone();
Expand Down
51 changes: 51 additions & 0 deletions tests/Cms/Files/FileActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,57 @@ public function testManipulate($parent)
$this->assertSame(100, $replacedFile->height());
}

/**
* @dataProvider parentProvider
*/
public function testManipulateValidFormat($parent)
{
$original = static::FIXTURES . '/test.jpg';

$originalFile = File::create([
'filename' => 'test.jpg',
'source' => $original,
'parent' => $parent
]);

$this->assertSame(128, $originalFile->width());
$this->assertSame(128, $originalFile->height());

$replacedFile = $originalFile->manipulate([
'width' => 100,
'height' => 100,
'format' => 'webp'
]);

$this->assertSame('webp', $replacedFile->extension());
$this->assertSame(100, $replacedFile->width());
$this->assertSame(100, $replacedFile->height());
}

/**
* @dataProvider parentProvider
*/
public function testManipulateInvalidValidFormat($parent)
{
$original = static::FIXTURES . '/test.mp4';

$originalFile = File::create([
'filename' => 'test.mp4',
'source' => $original,
'parent' => $parent
]);

$replacedFile = $originalFile->manipulate([
'width' => 100,
'height' => 100,
'format' => 'webp'
]);

// proves strictly that both are the same object
$this->assertSame($originalFile, $replacedFile);
$this->assertSame('mp4', $replacedFile->extension());
}

public function testChangeNameHooks()
{
$calls = 0;
Expand Down
Binary file added tests/Cms/Files/fixtures/files/test.mp4
Binary file not shown.

0 comments on commit 90674e8

Please sign in to comment.