Skip to content

Commit f8fd2ac

Browse files
author
marekrzytki
committed
Change File type in Media to UploadedFile
1 parent 4cbce8e commit f8fd2ac

7 files changed

Lines changed: 24 additions & 14 deletions

File tree

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: 4 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 */
@@ -104,12 +104,12 @@ public function setPath(?string $path): void
104104
$this->path = $path;
105105
}
106106

107-
public function getFile(): ?File
107+
public function getFile(): ?UploadedFile
108108
{
109109
return $this->file;
110110
}
111111

112-
public function setFile(?File $file): void
112+
public function setFile(?UploadedFile $file): void
113113
{
114114
$this->file = $file;
115115
}

src/Entity/MediaInterface.php

Lines changed: 3 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

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()

tests/Application/config/packages/bitbag_sylius_cms_plugin.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ sylius_fixtures:
130130
homepage_header_image:
131131
type: image
132132
path: "%fixtures_dir%/homepage_header.jpeg"
133+
original_name: "homepage_header.jpeg"
133134
channels:
134135
- "FASHION_WEB"
135136
translations:
@@ -147,6 +148,7 @@ sylius_fixtures:
147148
homepage_video:
148149
type: video
149150
path: "%fixtures_dir%/homepage_video.mp4"
151+
original_name: "homepage_video.mp4"
150152
channels:
151153
- "FASHION_WEB"
152154
translations:
@@ -164,6 +166,7 @@ sylius_fixtures:
164166
homepage_pdf:
165167
type: file
166168
path: "%fixtures_dir%/BitBagOffer.pdf"
169+
original_name: "BitBagOffer.pdf"
167170
channels:
168171
- "FASHION_WEB"
169172
translations:
@@ -181,16 +184,19 @@ sylius_fixtures:
181184
- "FASHION_WEB"
182185
type: image
183186
path: "%fixtures_dir%/size_table.jpeg"
187+
original_name: "size_table.jpeg"
184188
sale:
185189
channels:
186190
- "FASHION_WEB"
187191
type: image
188192
path: "%fixtures_dir%/sale.jpeg"
193+
original_name: "sale.jpeg"
189194
sections:
190195
- "products"
191196
media_with_products:
192197
type: image
193198
path: "%fixtures_dir%/homepage_header.jpeg"
199+
original_name: "homepage_header.jpeg"
194200
channels:
195201
- "FASHION_WEB"
196202
productCodes:
@@ -202,6 +208,7 @@ sylius_fixtures:
202208
- "FASHION_WEB"
203209
type: image
204210
path: "%fixtures_dir%/homepage_header.jpeg"
211+
original_name: "homepage_header.jpeg"
205212
translations:
206213
en_US:
207214
name: Custom media template

0 commit comments

Comments
 (0)