|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Command; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Media; |
|
6
|
|
|
use Sonata\MediaBundle\Command\UpdateCdnStatusCommand; |
|
7
|
|
|
use Sonata\MediaBundle\Provider\BaseProvider; |
|
8
|
|
|
use Sonata\MediaBundle\Provider\MediaProviderInterface; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
10
|
|
|
use Symfony\Component\Console\Command\Command; |
|
11
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
|
17
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
21
|
|
|
|
|
22
|
|
|
class MidiaPushToCdnCommand extends Command implements ContainerAwareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
protected static $defaultName = 'midia:push-to-cdn'; |
|
25
|
|
|
|
|
26
|
|
|
private ContainerInterface $container; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
public function configure() |
|
29
|
|
|
{ |
|
30
|
|
|
$this |
|
31
|
|
|
->setDescription('Add a short description for your command') |
|
32
|
|
|
; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int |
|
36
|
|
|
{ |
|
37
|
|
|
$io = new SymfonyStyle($input, $output); |
|
38
|
|
|
|
|
39
|
|
|
$imageProviderName = 'sonata.media.provider.image'; |
|
40
|
|
|
$em = $this->container->get('doctrine.orm.entity_manager'); |
|
41
|
|
|
$medias = $em |
|
42
|
|
|
->getRepository(Media::class) |
|
43
|
|
|
->findBy(['providerName' => $imageProviderName, 'description' => null]); |
|
44
|
|
|
$mediaPool = $this->container->get('sonata.media.pool'); |
|
45
|
|
|
$fileProvider = $this->container->get($imageProviderName); |
|
46
|
|
|
$rootDir = $this->container->getParameter('kernel.project_dir'); |
|
47
|
|
|
$imageProvider = $mediaPool->getProvider($imageProviderName); |
|
48
|
|
|
|
|
49
|
|
|
$io->progressStart(count($medias)); |
|
50
|
|
|
|
|
51
|
|
|
/** @var Media $media */ |
|
52
|
|
|
foreach ($medias as $media) { |
|
53
|
|
|
$filePath = sprintf('%s/public/uploads/%s', $rootDir, $imageProvider->getReferenceImage($media)); |
|
54
|
|
|
|
|
55
|
|
|
$file = new File($filePath); |
|
56
|
|
|
$media->setBinaryContent($file); |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
|
|
$fileProvider->postPersist($media); |
|
60
|
|
|
} catch (\Throwable $e) { |
|
61
|
|
|
$io->error(sprintf('Error "%s" while send media with %s id and "%s" path', $e->getMessage(), $media->getId(), $filePath)); |
|
62
|
|
|
$io->comment($e->getTraceAsString()); |
|
63
|
|
|
return 1; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$media->setDescription(''); |
|
67
|
|
|
$em->flush(); |
|
68
|
|
|
|
|
69
|
|
|
$io->progressAdvance(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$io->progressFinish(); |
|
73
|
|
|
$io->success('Done'); |
|
74
|
|
|
|
|
75
|
|
|
return 0; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritDoc |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setContainer(ContainerInterface $container = null) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->container = $container; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|