|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace eZ\Bundle\EzPublishIOBundle\Migration\FileMigrator; |
|
8
|
|
|
|
|
9
|
|
|
use eZ\Bundle\EzPublishIOBundle\Migration\FileMigratorInterface; |
|
10
|
|
|
use eZ\Bundle\EzPublishIOBundle\Migration\MigrationHandler; |
|
11
|
|
|
use eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException; |
|
12
|
|
|
use eZ\Publish\SPI\IO\BinaryFile; |
|
13
|
|
|
use eZ\Publish\SPI\IO\BinaryFileCreateStruct; |
|
14
|
|
|
|
|
15
|
|
|
final class FileMigrator extends MigrationHandler implements FileMigratorInterface |
|
16
|
|
|
{ |
|
17
|
|
|
public function migrateFile(BinaryFile $binaryFile) |
|
18
|
|
|
{ |
|
19
|
|
|
try { |
|
20
|
|
|
$binaryFileResource = $this->fromBinarydataHandler->getResource($binaryFile->id); |
|
21
|
|
|
} catch (BinaryFileNotFoundException $e) { |
|
22
|
|
|
$this->logError("Cannot load binary data for: '{$binaryFile->id}'. Error: " . $e->getMessage()); |
|
23
|
|
|
|
|
24
|
|
|
return false; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$binaryFileCreateStruct = new BinaryFileCreateStruct(); |
|
28
|
|
|
$binaryFileCreateStruct->id = $binaryFile->id; |
|
29
|
|
|
$binaryFileCreateStruct->setInputStream($binaryFileResource); |
|
30
|
|
|
|
|
31
|
|
|
try { |
|
32
|
|
|
$this->toBinarydataHandler->create($binaryFileCreateStruct); |
|
33
|
|
|
} catch (\RuntimeException $e) { |
|
34
|
|
|
$this->logError("Cannot migrate binary data for: '{$binaryFile->id}'. Error: " . $e->getMessage()); |
|
35
|
|
|
|
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$metadataCreateStruct = new BinaryFileCreateStruct(); |
|
40
|
|
|
$metadataCreateStruct->id = $binaryFile->id; |
|
41
|
|
|
$metadataCreateStruct->size = $binaryFile->size; |
|
42
|
|
|
$metadataCreateStruct->mtime = $binaryFile->mtime; |
|
43
|
|
|
$metadataCreateStruct->mimeType = $this->fromMetadataHandler->getMimeType($binaryFile->id); |
|
44
|
|
|
|
|
45
|
|
|
try { |
|
46
|
|
|
$this->toMetadataHandler->create($metadataCreateStruct); |
|
47
|
|
|
} catch (\RuntimeException $e) { |
|
48
|
|
|
$this->logError("Cannot migrate metadata for: '{$binaryFile->id}'. Error: " . $e->getMessage() . $e->getPrevious()->getMessage()); |
|
49
|
|
|
|
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->logInfo("Successfully migrated: '{$binaryFile->id}'"); |
|
54
|
|
|
|
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|