-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathWriteMetadatasWorker.php
More file actions
393 lines (322 loc) · 13.9 KB
/
Copy pathWriteMetadatasWorker.php
File metadata and controls
393 lines (322 loc) · 13.9 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
namespace Alchemy\Phrasea\WorkerManager\Worker;
use Alchemy\Phrasea\Application\Helper\ApplicationBoxAware;
use Alchemy\Phrasea\Application\Helper\DataboxLoggerAware;
use Alchemy\Phrasea\Application\Helper\DispatcherAware;
use Alchemy\Phrasea\Application\Helper\EntityManagerAware;
use Alchemy\Phrasea\Core\PhraseaTokens;
use Alchemy\Phrasea\Media\Subdef\Subdef;
use Alchemy\Phrasea\Metadata\TagFactory;
use Alchemy\Phrasea\Model\Entities\WorkerRunningJob;
use Alchemy\Phrasea\Model\Repositories\WorkerRunningJobRepository;
use Alchemy\Phrasea\WorkerManager\Event\SubdefinitionWritemetaEvent;
use Alchemy\Phrasea\WorkerManager\Event\WorkerEvents;
use Alchemy\Phrasea\WorkerManager\Queue\MessagePublisher;
use DateTime;
use Exception;
use Monolog\Logger;
use PHPExiftool\Driver\Metadata\Metadata;
use PHPExiftool\Driver\Metadata\MetadataBag;
use PHPExiftool\Driver\Tag;
use PHPExiftool\Driver\Value\Mono;
use PHPExiftool\Driver\Value\Multi;
use PHPExiftool\Exception\TagUnknown;
use PHPExiftool\Writer;
use Psr\Log\LoggerInterface;
use record_adapter;
class WriteMetadatasWorker implements WorkerInterface
{
use ApplicationBoxAware;
use DispatcherAware;
use EntityManagerAware;
use DataboxLoggerAware;
/** @var Logger */
private $logger;
/** @var MessagePublisher $messagePublisher */
private $messagePublisher;
/** @var Writer $writer */
private $writer;
private $repoWorker;
public function __construct(
Writer $writer,
LoggerInterface $logger,
MessagePublisher $messagePublisher,
WorkerRunningJobRepository $repoWorker
)
{
$this->writer = $writer;
$this->logger = $logger;
$this->messagePublisher = $messagePublisher;
$this->repoWorker = $repoWorker;
}
public function process(array $payload)
{
// mandatory args
if (!isset($payload['recordId']) || !isset($payload['databoxId']) || !isset($payload['subdefName'])) {
// bad payload
$this->logger->error(sprintf("%s (%s) : bad payload", __FILE__, __LINE__));
return 0;
}
$databoxId = $payload['databoxId'];
$recordId = $payload['recordId'];
$subdefName = $payload['subdefName'];
$MWG = $payload['MWG'] ?? false;
$clearDoc = $payload['clearDoc'] ?? false;
$databox = $this->findDataboxById($databoxId);
// try to "lock" the file, will return null if already locked
$workerRunningJobId = $this->repoWorker->canWriteMetadata($payload);
if (is_null($workerRunningJobId)) {
// the file is written by another worker, delay to retry later
$this->messagePublisher->publishDelayedMessage(
[
'message_type' => MessagePublisher::WRITE_METADATAS_TYPE,
'payload' => $payload
],
MessagePublisher::WRITE_METADATAS_TYPE
);
return 0;
}
// here we can work
try {
$record = $databox->get_record($recordId);
} catch (\Exception $e) {
$this->repoWorker->markFinished($workerRunningJobId, "error " . $e->getMessage());
return 0;
}
/** @var WorkerRunningJob $workerRunningJob */
$workerRunningJob = $this->repoWorker->find($workerRunningJobId);
$this->getDataboxLogger($databox)->initOrUpdateLogDocsFromWorker($record, $databox, $workerRunningJob, $subdefName, \Session_Logger::EVENT_WRITEMETADATAS);
if ($record->getMimeType() == 'image/svg+xml') {
$this->logger->error("Can't write meta on svg file!");
// tell that we have finished to work on this file ("unlock")
$this->repoWorker->markFinished($workerRunningJobId, "Can't write meta on svg file!");
$this->getDataboxLogger($databox)->initOrUpdateLogDocsFromWorker($record, $databox, $workerRunningJob, $subdefName, \Session_Logger::EVENT_WRITEMETADATAS, new \DateTime('now'), WorkerRunningJob::ERROR);
return 0;
}
$this->repoWorker->reconnect();
try {
$subdef = $record->get_subdef($subdefName);
}
catch (Exception $e) {
$workerMessage = "Exception catched when try to get subdef " .$subdefName. " from DB for the recordID: " .$recordId;
$this->logger->error($workerMessage);
$count = isset($payload['count']) ? $payload['count'] + 1 : 2 ;
/** @uses \Alchemy\Phrasea\WorkerManager\Subscriber\RecordSubscriber::onSubdefinitionWritemeta() */
$this->dispatch(WorkerEvents::SUBDEFINITION_WRITE_META, new SubdefinitionWritemetaEvent(
$record,
$subdefName,
SubdefinitionWritemetaEvent::FAILED,
$workerMessage,
$count,
$workerRunningJobId
));
// the subscriber will mark the job as errored, no need to do it here
return 0;
}
if (!$subdef->is_physically_present()) {
$count = isset($payload['count']) ? $payload['count'] + 1 : 2 ;
/** @uses \Alchemy\Phrasea\WorkerManager\Subscriber\RecordSubscriber::onSubdefinitionWritemeta() */
$this->dispatch(WorkerEvents::SUBDEFINITION_WRITE_META, new SubdefinitionWritemetaEvent(
$record,
$subdefName,
SubdefinitionWritemetaEvent::FAILED,
'Subdef is not physically present!',
$count,
$workerRunningJobId
));
// the subscriber will mark the job as errored, no need to do it here
return 0;
}
// here we can try to do the job
$metadata = new MetadataBag();
// add Uuid in metadatabag
if ($record->getUuid()) {
$metadata->add(
new Metadata(
new Tag\XMPExif\ImageUniqueID(),
new Mono($record->getUuid())
)
);
$metadata->add(
new Metadata(
new Tag\ExifIFD\ImageUniqueID(),
new Mono($record->getUuid())
)
);
$metadata->add(
new Metadata(
new Tag\IPTC\UniqueDocumentID(),
new Mono($record->getUuid())
)
);
}
// read document fields and add to metadatabag
$caption = $record->get_caption();
foreach ($databox->get_meta_structure() as $fieldStructure) {
$tagName = $fieldStructure->get_tag()->getTagname();
$fieldName = $fieldStructure->get_name();
// skip fields with no src
if ($tagName == '' || $tagName == 'Phraseanet:no-source') {
continue;
}
// check exiftool known tags to skip Phraseanet:tf-*
try {
$tag = TagFactory::getFromRDFTagname($tagName);
if(!$tag->isWritable()) {
continue;
}
} catch (TagUnknown $e) {
continue;
}
try {
$field = $caption->get_field($fieldName);
$fieldValues = $field->get_values();
if ($fieldStructure->is_multi()) {
$values = array();
foreach ($fieldValues as $value) {
$values[] = $this->removeNulChar($value->getValue());
}
$value = new Multi($values);
} else {
$fieldValue = array_pop($fieldValues);
$value = $this->removeNulChar($fieldValue->getValue());
// fix the dates edited into phraseanet
if($fieldStructure->get_type() === $fieldStructure::TYPE_DATE) {
try {
$value = self::fixDate($value); // will return NULL if the date is not valid
}
catch (Exception $e) {
$value = null; // do NOT write back to iptc
}
}
if($value !== null) { // do not write invalid dates
$value = new Mono($value);
}
}
}
catch(Exception $e) {
// the field is not set in the record, erase it
if ($fieldStructure->is_multi()) {
$value = new Multi(array(''));
}
else {
$value = new Mono('');
}
}
if($value !== null) { // do not write invalid data
$metadata->add(
new Metadata($fieldStructure->get_tag(), $value)
);
}
}
$this->writer->reset();
if ($MWG) {
$this->writer->setModule(Writer::MODULE_MWG, true);
}
$this->writer->erase($subdef->get_name() != 'document' || $clearDoc, true);
$resolutionXY = [];
if ($subdef->get_name() != 'document') {
$subdefType = $subdef->getDataboxSubdef()->getSubdefType();
// get the resolution DPI option from the admin configuration if the media type has one
if (in_array($subdefType->getType(), [Subdef::TYPE_IMAGE, Subdef::TYPE_ANIMATION, Subdef::TYPE_UNKNOWN])) {
$resolution = $subdefType->getOption('resolution')->getValue();
$resolutionXY = [$resolution, $resolution];
}
}
// write meta in file
try {
$this->writer->write($subdef->getRealPath(), $metadata, null, $resolutionXY);
$this->messagePublisher->pushLog(sprintf('metadatas written %s databoxname=%s databoxid=%d recordid=%d',
$subdef->get_name(), $databox->get_viewname(), $databox->get_sbas_id(), $recordId), 'info');
}
catch (Exception $e) {
// try to interpret the reason of the failure, to see if we retry or stop
$msg = $e->getMessage();
$matches = null;
$stopInfo = false;
if( preg_match('/\\(looks more like a .*\\)/', $msg,$matches) ) {
$stopInfo = $matches[0];
}
$workerMessage = sprintf('meta NOT written for databoxId=%1$d - recordId=%2$d (%3$s) because "%4$s"',
$databox->get_sbas_id(),
$recordId,
$subdef->get_name(),
$msg
);
$this->logger->error($workerMessage);
if(!$stopInfo) {
// we don't know what failed so we retry
$count = isset($payload['count']) ? $payload['count'] + 1 : 2;
/** @uses \Alchemy\Phrasea\WorkerManager\Subscriber\RecordSubscriber::onSubdefinitionWritemeta() */
$this->dispatch(WorkerEvents::SUBDEFINITION_WRITE_META, new SubdefinitionWritemetaEvent(
$record,
$subdefName,
SubdefinitionWritemetaEvent::FAILED,
$workerMessage,
$count,
$workerRunningJobId
));
// the subscriber will "unlock" the row, no need to do it here
}
else {
// failed with known error message like "Not a valid JPG (looks more like a PSD)"
// mark write metas finished
$this->updateJeton($record);
// tell that we have finished to work on this file (=unlock)
$this->repoWorker->markFinished($workerRunningJobId, $stopInfo);
$this->getDataboxLogger($databox)->initOrUpdateLogDocsFromWorker($record, $databox, $workerRunningJob, $subdefName, \Session_Logger::EVENT_WRITEMETADATAS, new \DateTime('now'), WorkerRunningJob::ERROR);
}
return 0;
}
// mark write metas finished
$this->updateJeton($record);
// tell that we have finished to work on this file (=unlock)
$this->repoWorker->markFinished($workerRunningJobId);
$this->getDataboxLogger($databox)->initOrUpdateLogDocsFromWorker($record, $databox, $workerRunningJob, $subdefName, \Session_Logger::EVENT_WRITEMETADATAS, new \DateTime('now'), WorkerRunningJob::FINISHED);
return 0;
}
private function removeNulChar($value)
{
return str_replace("\0", "", $value);
}
private function updateJeton(record_adapter $record)
{
$connection = $record->getDatabox()->get_connection();
$connection->beginTransaction();
$stmt = $connection->prepare('UPDATE record SET jeton=(jeton & ~(:token)), moddate=NOW() WHERE record_id = :record_id');
$stmt->execute([
':record_id' => $record->getRecordId(),
':token' => PhraseaTokens::WRITE_META,
]);
$connection->commit();
$stmt->closeCursor();
}
/**
* re-format a phraseanet date for iptc writing
* return NULL if the date is not valid
*
* @param string $value
* @return string|null
*/
private static function fixDate($value)
{
$date = null;
try {
$a = explode(';', preg_replace('/\D+/', ';', trim($value)));
switch (count($a)) {
case 3: // yyyy;mm;dd
$date = new DateTime($a[0] . '-' . $a[1] . '-' . $a[2]);
$date = $date->format('Y-m-d H:i:s');
break;
case 6: // yyyy;mm;dd;hh;mm;ss
$date = new DateTime($a[0] . '-' . $a[1] . '-' . $a[2] . ' ' . $a[3] . ':' . $a[4] . ':' . $a[5]);
$date = $date->format('Y-m-d H:i:s');
break;
}
}
catch (Exception $e) {
$date = null;
}
return $date;
}
}