Skip to content

Commit 533fbdc

Browse files
authored
Merge pull request #456 from BitBagCommerce/feature/save-with-original-name
Add ability to save file while upload with original name instead of hash
2 parents fcf7fd1 + 5917c42 commit 533fbdc

17 files changed

Lines changed: 83 additions & 38 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
php: ["8.0", "7.4"]
2525
symfony: ["^4.4", "^5.2"]
2626
sylius: ["~1.9.0", "~1.10.0", "~1.11.0"]
27-
node: [">=12.13.0"]
27+
node: ["14.19"]
2828
mysql: ["8.0"]
2929

3030
exclude:

spec/Entity/MediaSpec.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@
1717
use Sylius\Component\Core\Model\ChannelInterface;
1818
use Sylius\Component\Core\Model\ProductInterface;
1919
use Sylius\Component\Resource\Model\ResourceInterface;
20-
use Symfony\Component\HttpFoundation\File\File;
20+
use Symfony\Component\HttpFoundation\File\UploadedFile;
2121

2222
final class MediaSpec extends ObjectBehavior
2323
{
24-
function it_is_initializable(): void
24+
public function it_is_initializable(): void
2525
{
2626
$this->shouldHaveType(Media::class);
2727
}
2828

29-
function it_is_a_resource(): void
29+
public function it_is_a_resource(): void
3030
{
3131
$this->shouldHaveType(ResourceInterface::class);
3232
}
3333

34-
function it_implements_media_interface(): void
34+
public function it_implements_media_interface(): void
3535
{
3636
$this->shouldHaveType(MediaInterface::class);
3737
}
3838

39-
function it_allows_access_via_properties(): void
39+
public function it_allows_access_via_properties(): void
4040
{
4141
$this->setCode('file');
4242
$this->getCode()->shouldReturn('file');
@@ -47,7 +47,7 @@ function it_allows_access_via_properties(): void
4747
$this->setPath('/media/video');
4848
$this->getPath()->shouldReturn('/media/video');
4949

50-
$file = new File(__DIR__ . '/MediaSpec.php');
50+
$file = new UploadedFile(__DIR__ . '/MediaSpec.php', 'originalName');
5151

5252
$this->setFile($file);
5353
$this->getFile()->shouldReturn($file);
@@ -56,7 +56,7 @@ function it_allows_access_via_properties(): void
5656
$this->getMimeType()->shouldReturn('video/mp4');
5757
}
5858

59-
function it_toggles(): void
59+
public function it_toggles(): void
6060
{
6161
$this->enable();
6262
$this->isEnabled()->shouldReturn(true);
@@ -65,7 +65,7 @@ function it_toggles(): void
6565
$this->isEnabled()->shouldReturn(false);
6666
}
6767

68-
function it_associates_products(ProductInterface $firstProduct, ProductInterface $secondProduct): void
68+
public function it_associates_products(ProductInterface $firstProduct, ProductInterface $secondProduct): void
6969
{
7070
$this->addProduct($firstProduct);
7171
$this->hasProduct($firstProduct)->shouldReturn(true);
@@ -77,7 +77,7 @@ function it_associates_products(ProductInterface $firstProduct, ProductInterface
7777
$this->hasProduct($firstProduct)->shouldReturn(false);
7878
}
7979

80-
function it_associates_sections(SectionInterface $firstSection, SectionInterface $secondSection): void
80+
public function it_associates_sections(SectionInterface $firstSection, SectionInterface $secondSection): void
8181
{
8282
$this->addSection($firstSection);
8383
$this->hasSection($firstSection)->shouldReturn(true);
@@ -89,7 +89,7 @@ function it_associates_sections(SectionInterface $firstSection, SectionInterface
8989
$this->hasSection($firstSection)->shouldReturn(false);
9090
}
9191

92-
function it_associates_channels(ChannelInterface $firstChannel, ChannelInterface $secondChannel): void
92+
public function it_associates_channels(ChannelInterface $firstChannel, ChannelInterface $secondChannel): void
9393
{
9494
$this->addChannel($firstChannel);
9595
$this->hasChannel($firstChannel)->shouldReturn(true);

spec/Media/Provider/FileProviderSpec.php renamed to spec/MediaProvider/GenericProviderSpec.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
use PhpSpec\ObjectBehavior;
1818
use Twig\Environment;
1919

20-
final class FileProviderSpec extends ObjectBehavior
20+
final class GenericProviderSpec extends ObjectBehavior
2121
{
22-
function let(
22+
public function let(
2323
MediaUploaderInterface $uploader,
2424
Environment $twigEngine
2525
) {
2626
$this->beConstructedWith($uploader, $twigEngine, '@Template', '/media/');
2727
}
2828

29-
function it_is_initializable(): void
29+
public function it_is_initializable(): void
3030
{
3131
$this->shouldHaveType(GenericProvider::class);
3232
}
3333

34-
function it_implements_provider_interface(): void
34+
public function it_implements_provider_interface(): void
3535
{
3636
$this->shouldHaveType(ProviderInterface::class);
3737
}
@@ -40,7 +40,7 @@ public function it_renders(MediaInterface $media, Environment $twigEngine): void
4040
{
4141
$twigEngine->render('@Template', ['media' => $media, 'config' => []])->willReturn('content');
4242

43-
$this->render($media, ['config' => []])->shouldReturn('content');
43+
$this->render($media, '@Template', ['config' => []])->shouldReturn('content');
4444
}
4545

4646
public function it_uploads(MediaInterface $media, MediaUploaderInterface $uploader): void

src/Downloader/ImageDownloader.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace BitBag\SyliusCmsPlugin\Downloader;
1212

1313
use Symfony\Component\Filesystem\Filesystem;
14-
use Symfony\Component\HttpFoundation\File\File;
14+
use Symfony\Component\HttpFoundation\File\UploadedFile;
1515
use Webmozart\Assert\Assert;
1616

1717
final class ImageDownloader implements ImageDownloaderInterface
@@ -24,18 +24,20 @@ public function __construct(Filesystem $filesystem)
2424
$this->filesystem = $filesystem;
2525
}
2626

27-
public function download(string $url): File
27+
public function download(string $url): UploadedFile
2828
{
2929
$path = rtrim(sys_get_temp_dir(), \DIRECTORY_SEPARATOR) . \DIRECTORY_SEPARATOR . md5(random_bytes(10));
3030
$pathInfo = pathinfo($url);
3131
$extension = $pathInfo['extension'] ?? null;
32+
$originalName = $pathInfo['basename'] ?? '';
33+
3234
if (null !== $extension) {
3335
$path .= '.' . $extension;
3436
}
3537
$contents = file_get_contents($url);
3638
Assert::string($contents, sprintf('Content of file in path %s is null', $path));
3739
$this->filesystem->dumpFile($path, $contents);
3840

39-
return new File($path);
41+
return new UploadedFile($path, $originalName);
4042
}
4143
}

src/Downloader/ImageDownloaderInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
namespace BitBag\SyliusCmsPlugin\Downloader;
1212

13-
use Symfony\Component\HttpFoundation\File\File;
13+
use Symfony\Component\HttpFoundation\File\UploadedFile;
1414

1515
interface ImageDownloaderInterface
1616
{
17-
public function download(string $url): File;
17+
public function download(string $url): UploadedFile;
1818
}

src/Entity/Media.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Sylius\Component\Resource\Model\ToggleableTrait;
1515
use Sylius\Component\Resource\Model\TranslatableTrait;
1616
use Sylius\Component\Resource\Model\TranslationInterface;
17-
use Symfony\Component\HttpFoundation\File\File;
17+
use Symfony\Component\HttpFoundation\File\UploadedFile;
1818
use Webmozart\Assert\Assert;
1919

2020
class Media implements MediaInterface
@@ -43,7 +43,7 @@ class Media implements MediaInterface
4343
/** @var string|null */
4444
protected $path;
4545

46-
/** @var File|null */
46+
/** @var UploadedFile|null */
4747
protected $file;
4848

4949
/** @var string|null */
@@ -58,6 +58,9 @@ class Media implements MediaInterface
5858
/** @var int|null */
5959
protected $height;
6060

61+
/** @var bool */
62+
protected $saveWithOriginalName = false;
63+
6164
public function __construct()
6265
{
6366
$this->initializeTranslationsCollection();
@@ -101,12 +104,12 @@ public function setPath(?string $path): void
101104
$this->path = $path;
102105
}
103106

104-
public function getFile(): ?File
107+
public function getFile(): ?UploadedFile
105108
{
106109
return $this->file;
107110
}
108111

109-
public function setFile(?File $file): void
112+
public function setFile(?UploadedFile $file): void
110113
{
111114
$this->file = $file;
112115
}
@@ -211,6 +214,16 @@ public function setHeight(?int $height): void
211214
$this->height = $height;
212215
}
213216

217+
public function getSaveWithOriginalName(): bool
218+
{
219+
return $this->saveWithOriginalName;
220+
}
221+
222+
public function setSaveWithOriginalName(bool $saveWithOriginalName): void
223+
{
224+
$this->saveWithOriginalName = $saveWithOriginalName;
225+
}
226+
214227
/**
215228
* @return MediaTranslationInterface|TranslationInterface
216229
*/

src/Entity/MediaInterface.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Sylius\Component\Resource\Model\ResourceInterface;
1515
use Sylius\Component\Resource\Model\ToggleableInterface;
1616
use Sylius\Component\Resource\Model\TranslatableInterface;
17-
use Symfony\Component\HttpFoundation\File\File;
17+
use Symfony\Component\HttpFoundation\File\UploadedFile;
1818

1919
interface MediaInterface extends
2020
ResourceInterface,
@@ -41,9 +41,9 @@ public function getPath(): ?string;
4141

4242
public function setPath(?string $path): void;
4343

44-
public function getFile(): ?File;
44+
public function getFile(): ?UploadedFile;
4545

46-
public function setFile(?File $file): void;
46+
public function setFile(?UploadedFile $file): void;
4747

4848
public function hasFile(): bool;
4949

@@ -80,4 +80,8 @@ public function setWidth(?int $width): void;
8080
public function getHeight(): ?int;
8181

8282
public function setHeight(?int $height): void;
83+
84+
public function getSaveWithOriginalName(): bool;
85+
86+
public function setSaveWithOriginalName(bool $saveWithOriginalName): void;
8387
}

src/Fixture/Factory/MediaFixtureFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
1919
use BitBag\SyliusCmsPlugin\Resolver\MediaProviderResolverInterface;
2020
use Sylius\Component\Resource\Factory\FactoryInterface;
21-
use Symfony\Component\HttpFoundation\File\File;
21+
use Symfony\Component\HttpFoundation\File\UploadedFile;
2222

2323
final class MediaFixtureFactory implements FixtureFactoryInterface
2424
{
@@ -88,7 +88,7 @@ private function createMedia(string $code, array $mediaData): void
8888
$media->setType($mediaData['type']);
8989
$media->setCode($code);
9090
$media->setEnabled($mediaData['enabled']);
91-
$media->setFile(new File($mediaData['path']));
91+
$media->setFile(new UploadedFile($mediaData['path'], $mediaData['original_name']));
9292

9393
$this->mediaProviderResolver->resolveProvider($media)->upload($media);
9494

src/Fixture/MediaFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
4545
->integerNode('number')->defaultNull()->end()
4646
->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
4747
->scalarNode('path')->isRequired()->cannotBeEmpty()->end()
48+
->scalarNode('original_name')->isRequired()->cannotBeEmpty()->end()
4849
->booleanNode('enabled')->defaultTrue()->end()
4950
->arrayNode('productCodes')->scalarPrototype()->end()->end()
5051
->arrayNode('sections')->scalarPrototype()->end()->end()

src/Form/Type/MediaType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
5959
->add('enabled', CheckboxType::class, [
6060
'label' => 'bitbag_sylius_cms_plugin.ui.enabled',
6161
])
62+
->add('saveWithOriginalName', CheckboxType::class, [
63+
'label' => 'bitbag_sylius_cms_plugin.ui.save_with_original_name',
64+
])
6265
->add('products', ProductAutocompleteChoiceType::class, [
6366
'label' => 'bitbag_sylius_cms_plugin.ui.products',
6467
'multiple' => true,

0 commit comments

Comments
 (0)