|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yawik\Migration\Migrator\Version36; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
use Auth\Entity\User; |
|
9
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
10
|
|
|
use MongoDB\BSON\ObjectId; |
|
11
|
|
|
use MongoDB\Collection; |
|
12
|
|
|
use MongoDB\Database; |
|
13
|
|
|
use MongoDB\GridFS\Bucket; |
|
14
|
|
|
use Mpdf\Tag\P; |
|
15
|
|
|
use Psr\Container\ContainerInterface; |
|
16
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
18
|
|
|
use Yawik\Migration\Contracts\ProcessorInterface; |
|
19
|
|
|
use Yawik\Migration\Exception\MigrationException; |
|
20
|
|
|
use Yawik\Migration\Util\MongoUtilTrait; |
|
21
|
|
|
|
|
22
|
|
|
class FileProcessor implements ProcessorInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use MongoUtilTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var DocumentManager |
|
28
|
|
|
*/ |
|
29
|
|
|
private DocumentManager $dm; |
|
30
|
|
|
|
|
31
|
|
|
private string $document; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var OutputInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private OutputInterface $output; |
|
37
|
|
|
|
|
38
|
|
|
protected Database $database; |
|
39
|
|
|
|
|
40
|
|
|
protected Collection $collection; |
|
41
|
|
|
|
|
42
|
|
|
private Bucket $bucket; |
|
43
|
|
|
|
|
44
|
|
|
private string $bucketName; |
|
45
|
|
|
|
|
46
|
|
|
private string $fieldName; |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
public function __construct( |
|
49
|
|
|
DocumentManager $dm, |
|
50
|
|
|
OutputInterface $output, |
|
51
|
|
|
string $bucketName |
|
52
|
|
|
) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->dm = $dm; |
|
55
|
|
|
$this->output = $output; |
|
56
|
|
|
$this->bucketName = $bucketName; |
|
57
|
|
|
|
|
58
|
|
|
$this->database = $dm->getDocumentDatabase(User::class); |
|
59
|
|
|
$this->bucket = $this->database->selectGridFSBucket(['bucketName' => $bucketName]); |
|
60
|
|
|
$this->collection = $this->database->selectCollection($bucketName.'.files'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function process(): bool |
|
64
|
|
|
{ |
|
65
|
|
|
$bucket = $this->bucket; |
|
66
|
|
|
$count = $this->collection->countDocuments(); |
|
67
|
|
|
$progressBar = new ProgressBar($this->output, $count); |
|
68
|
|
|
$progressBar->setFormat( |
|
69
|
|
|
"<info>processing <comment>{$this->bucketName}</comment> bucket</info> [%current%/%max%]" |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
$cursor = $bucket->find(); |
|
73
|
|
|
foreach($cursor as $current){ |
|
74
|
|
|
$progressBar->advance(); |
|
75
|
|
|
try{ |
|
76
|
|
|
$this->processFile($current['_id']); |
|
77
|
|
|
}catch (\Exception $exception){ |
|
78
|
|
|
$progressBar->finish(); |
|
79
|
|
|
throw new MigrationException($exception->getMessage()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
$progressBar->finish(); |
|
84
|
|
|
$this->output->writeln(""); |
|
85
|
|
|
$this->output->writeln(""); |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function processFile(ObjectId $fileId) |
|
90
|
|
|
{ |
|
91
|
|
|
$database = $this->database; |
|
92
|
|
|
$bucketName = $this->bucketName; |
|
93
|
|
|
|
|
94
|
|
|
$fcol = $database->selectCollection($bucketName.'.files'); |
|
95
|
|
|
$oldMeta = $fcol->findOne(['_id'=>$fileId]); |
|
96
|
|
|
if(is_null($oldMeta)){ |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$metaMap = [ |
|
101
|
|
|
'user' => 'metadata.user', |
|
102
|
|
|
'permissions' => 'metadata.permissions', |
|
103
|
|
|
'mimetype' => 'metadata.contentType', |
|
104
|
|
|
'belongsTo' => 'metadata.belongsTo', |
|
105
|
|
|
'key' => 'metadata.key', |
|
106
|
|
|
'md5' => 'metadata.md5', |
|
107
|
|
|
'filename' => 'metadata.name', |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
$options = []; |
|
111
|
|
|
$set = []; |
|
112
|
|
|
$unset = []; |
|
113
|
|
|
|
|
114
|
|
|
foreach($metaMap as $from => $to){ |
|
115
|
|
|
$exp = explode('.', $from); |
|
116
|
|
|
$fromKey = $exp[0]; |
|
117
|
|
|
$value = $this->getNamespacedValue($from, $oldMeta); |
|
|
|
|
|
|
118
|
|
|
if(isset($oldMeta[$from])){ |
|
119
|
|
|
$set[$to] = $value; |
|
120
|
|
|
$unset[$fromKey] = true; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if(!is_null($oldMeta['name'])){ |
|
125
|
|
|
$set['metadata.name'] = $oldMeta['name']; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
//'uploadedDate' => 'uploadDate', |
|
129
|
|
|
//'dateuploaded.date' => 'uploadDate', |
|
130
|
|
|
//'dateUploaded.date' => 'uploadDate' |
|
131
|
|
|
$dateMap = [ |
|
132
|
|
|
'uploadedDate', |
|
133
|
|
|
'dateuploaded', |
|
134
|
|
|
'dateUploaded' |
|
135
|
|
|
]; |
|
136
|
|
|
foreach($dateMap as $key){ |
|
137
|
|
|
if(!is_null($date = $oldMeta[$key])){ |
|
138
|
|
|
$set['uploadDate'] = $date; |
|
139
|
|
|
$unset[$key] = true; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if(!empty($set)){ |
|
144
|
|
|
$options['$set'] = $set; |
|
145
|
|
|
} |
|
146
|
|
|
if(!empty($unset)){ |
|
147
|
|
|
$options['$unset'] = $unset; |
|
148
|
|
|
} |
|
149
|
|
|
if(!empty($options)){ |
|
150
|
|
|
$fcol->updateOne( |
|
151
|
|
|
['_id' => $fileId], |
|
152
|
|
|
$options |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |