forked from vitiko/IphpFileStoreBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepairFileDataCommand.php
More file actions
99 lines (68 loc) · 3.92 KB
/
Copy pathRepairFileDataCommand.php
File metadata and controls
99 lines (68 loc) · 3.92 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
<?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 execute(InputInterface $input, OutputInterface $output)
{
$entity = $input->getOption('entity');
$field = $input->getOption('field');
$force = $input->getOption('force') ? true : false;
$maxResults = $input->getOption('maxresults');
if (!$maxResults || !is_numeric($maxResults)) $maxResults = 1000;
$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/'));
$propertyMappingFactory = $this->getContainer()->get('iphp.filestore.mapping.factory');
if (!$webDir) throw new \InvalidArgumentException ('
For resolving IphpFileStoreBundle uploaded files need to set --webdir option');
list($bundle, $entity) = explode(':', $entity);
$repo = $this->getContainer()->get('doctrine')->getRepository($bundle . ":" . $entity);
$em = $this->getContainer()->get('doctrine')->getManager();
$ids = $em->createQuery(
"SELECT e.id FROM " . $bundle . ":" . $entity . " e ORDER BY e.id ASC"
)->setMaxResults($maxResults)->getArrayResult();
foreach ($ids as $pos => $e) {
$toFlush = false;
$entity = $repo->findOneById($e['id']);
$fileData = $entity->{'get' . ucfirst($field)}();
if (!$fileData) continue;
$fullFileNameByWebPath = $webDir . $fileData['path'];
$fullFileNameByWebPathExists = file_exists($fullFileNameByWebPath);
$propertyMapping = $propertyMappingFactory->getMappingFromField(
$entity, new \ReflectionClass($entity), $field);
$resolvedFileName = $propertyMapping->resolveFileName($fileData['fileName']);
$resolvedFileNameExists = file_exists($resolvedFileName) ? 'exists' : 'NO';
if (!$fullFileNameByWebPathExists && !$resolvedFileNameExists) {
die ("can't find file ");
}
if ($fullFileNameByWebPathExists && $resolvedFileNameExists && !$force) continue;
//uploadedFile because need to move to new destination (not copy)
$file = new UploadedFile ($fullFileNameByWebPathExists ? $fullFileNameByWebPath : $resolvedFileNameExists,
$fileData['originalName'], $fileData['mimeType'], null, null, true);
$entity->{'set' . ucfirst($field)} ($file);
$em->persist($entity);
$toFlush = true;
if ($pos % 20 == 0 && $toFlush) $em->flush();
if ($pos % 100 == 0) $em->clear();
}
$em->flush();
}
}