-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathRepairFileDataCommand.php
More file actions
128 lines (91 loc) · 4.55 KB
/
Copy pathRepairFileDataCommand.php
File metadata and controls
128 lines (91 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Iphp\FileStoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCommand;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineEntityGenerator;
use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class RepairFileDataCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('iphp:filestore:repair')
->addOption('entity', null, InputOption::VALUE_REQUIRED, 'The entity class name (shortcut notation)')
->addOption('field', null, InputOption::VALUE_REQUIRED, 'The field with file data')
->addOption('maxresults', null, InputOption::VALUE_OPTIONAL, 'Max results limitation')
->addOption('webdir', null, InputOption::VALUE_OPTIONAL, 'Web dir for searching file')
->addOption('force', null, InputOption::VALUE_OPTIONAL, 'Force reupload and rename files');
}
protected function getPropertyMappingFactory()
{
return $this->getContainer()->get('iphp.filestore.mapping.factory');
}
protected function getWebDir(InputInterface $input)
{
$webDir = $input->getOption('webdir');
if (!$webDir && $this->getContainer()->has('iphp.web_dir'))
$webDir = $this->getContainer()->getParameter('iphp.web_dir');
if (!$webDir) $webDir = str_replace('\\', '/', realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../web/'));
if (!$webDir) throw new \InvalidArgumentException ('
For resolving IphpFileStoreBundle uploaded files need to set --webdir option');
return $webDir;
}
function getMaxResults(InputInterface $input)
{
$maxResults = $input->getOption('maxresults');
if (!$maxResults || !is_numeric($maxResults)) $maxResults = 1000;
return $maxResults;
}
function getEntityManager()
{
return $this->getContainer()->get('doctrine')->getManager();
}
function getMappingFromField($entity, $field)
{
return $this->getPropertyMappingFactory()->getMappingFromField($entity, new \ReflectionClass($entity), $field);
}
function getRepository($entityFullName)
{
return $this->getEntityManager()->getRepository($entityFullName);
}
function getEntityIds($entityFullName, $maxResults)
{
return $this->getEntityManager()->createQuery(
"SELECT e.id FROM " . $entityFullName . " e ORDER BY e.id ASC"
)->setMaxResults($maxResults)->getArrayResult();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$entityFullName = $input->getOption('entity');
$field = $input->getOption('field');
$force = $input->getOption('force') ? true : false;
$webDir = $this->getWebDir($input);
foreach ($this->getEntityIds($entityFullName, $this->getMaxResults($input)) as $pos => $e) {
$toFlush = false;
$entity = $this->getRepository($entityFullName)->findOneById($e['id']);
$fileData = $entity->{'get' . ucfirst($field)}();
if (!$fileData) continue;
$fileNameByWebPath = $webDir . $fileData['path'];
$fileNameByWebPathExists = file_exists($fileNameByWebPath);
$resolvedFileName = $this->getMappingFromField($entity, $field)->resolveFileName($fileData['fileName']);
$resolvedFileNameExists = file_exists($resolvedFileName) ? 'exists' : 'NO';
if (!$fileNameByWebPathExists && !$resolvedFileNameExists) {
$output->writeln("can't find file ");
continue;
}
if ($fileNameByWebPathExists && $resolvedFileNameExists && !$force) continue;
//uploadedFile because need to move to new destination (not copy)
$file = new UploadedFile ($fileNameByWebPathExists ? $fileNameByWebPath : $resolvedFileNameExists,
$fileData['originalName'], $fileData['mimeType'], null, null, true);
$entity->{'set' . ucfirst($field)} ($file);
$this->getEntityManager()->persist($entity);
$toFlush = true;
if ($pos % 20 == 0 && $toFlush) $this->getEntityManager()->flush();
if ($pos % 100 == 0) $this->getEntityManager()->clear();
}
$this->getEntityManager()->flush();
}
}