|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the eZ Publish Kernel package. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Publish\Core\IO; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
|
12
|
|
|
use eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException; |
|
13
|
|
|
use eZ\Publish\Core\IO\Exception\InvalidBinaryAbsolutePathException; |
|
14
|
|
|
use eZ\Publish\Core\IO\Values\BinaryFile; |
|
15
|
|
|
use eZ\Publish\Core\IO\Values\MissingBinaryFile; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* An extended IOService that tolerates physically missing files. |
|
20
|
|
|
* |
|
21
|
|
|
* Meant to be used on a "broken" instance where the storage directory isn't in sync with the database. |
|
22
|
|
|
* |
|
23
|
|
|
* Note that it will still return false when exists() is used. |
|
24
|
|
|
*/ |
|
25
|
|
|
class TolerantIOService extends IOService |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
|
28
|
|
|
protected $logger; |
|
29
|
|
|
|
|
30
|
|
|
public function setLogger(LoggerInterface $logger = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->logger = $logger; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Deletes $binaryFile. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \eZ\Publish\Core\IO\Values\BinaryFile $binaryFile |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue If the binary file is invalid |
|
41
|
|
|
* @throws \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException If the binary file isn't found |
|
42
|
|
|
*/ |
|
43
|
|
|
public function deleteBinaryFile(BinaryFile $binaryFile) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->checkBinaryFileId($binaryFile->id); |
|
46
|
|
|
$spiUri = $this->getPrefixedUri($binaryFile->id); |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
|
|
$this->metadataHandler->delete($spiUri); |
|
50
|
|
|
} catch (BinaryFileNotFoundException $e) { |
|
51
|
|
|
$this->logMissingFile($binaryFile->uri); |
|
52
|
|
|
$logged = true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
try { |
|
56
|
|
|
$this->binarydataHandler->delete($spiUri); |
|
57
|
|
|
} catch (BinaryFileNotFoundException $e) { |
|
58
|
|
|
if (!isset($logged)) { |
|
59
|
|
|
$this->logMissingFile($binaryFile->uri); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Loads the binary file with $binaryFileId. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $binaryFileId |
|
68
|
|
|
* |
|
69
|
|
|
* @return \eZ\Publish\Core\IO\Values\BinaryFile|\eZ\Publish\Core\IO\Values\MissingBinaryFile |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \eZ\Publish\Core\IO\Exception\InvalidBinaryAbsolutePathException |
|
72
|
|
|
*/ |
|
73
|
|
|
public function loadBinaryFile($binaryFileId) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->checkBinaryFileId($binaryFileId); |
|
76
|
|
|
|
|
77
|
|
|
if ($this->isAbsolutePath($binaryFileId)) { |
|
78
|
|
|
throw new InvalidBinaryAbsolutePathException($binaryFileId); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
try { |
|
82
|
|
|
$spiBinaryFile = $this->metadataHandler->load($this->getPrefixedUri($binaryFileId)); |
|
83
|
|
|
} catch (BinaryFileNotFoundException $e) { |
|
84
|
|
|
$this->logMissingFile($binaryFileId); |
|
85
|
|
|
|
|
86
|
|
|
return new MissingBinaryFile([ |
|
87
|
|
|
'id' => $binaryFileId, |
|
88
|
|
|
'uri' => $this->binarydataHandler->getUri($this->getPrefixedUri($binaryFileId)), |
|
89
|
|
|
]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (!isset($spiBinaryFile->uri)) { |
|
93
|
|
|
$spiBinaryFile->uri = $this->binarydataHandler->getUri($spiBinaryFile->id); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->buildDomainBinaryFileObject($spiBinaryFile); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function loadBinaryFileByUri($binaryFileUri) |
|
100
|
|
|
{ |
|
101
|
|
|
$binaryFileId = $this->binarydataHandler->getIdFromUri($binaryFileUri); |
|
102
|
|
|
try { |
|
103
|
|
|
$binaryFileId = $this->removeUriPrefix($binaryFileId); |
|
104
|
|
|
} catch (InvalidArgumentException $e) { |
|
105
|
|
|
$this->logMissingFile($binaryFileUri); |
|
106
|
|
|
|
|
107
|
|
|
return new MissingBinaryFile([ |
|
108
|
|
|
'id' => $binaryFileId, |
|
109
|
|
|
'uri' => $binaryFileUri, |
|
110
|
|
|
]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
try { |
|
114
|
|
|
return $this->loadBinaryFile($binaryFileId); |
|
115
|
|
|
} catch (BinaryFileNotFoundException $e) { |
|
116
|
|
|
$this->logMissingFile($binaryFileUri); |
|
117
|
|
|
|
|
118
|
|
|
return new MissingBinaryFile([ |
|
119
|
|
|
'id' => $binaryFileId, |
|
120
|
|
|
'uri' => $this->binarydataHandler->getUri($this->getPrefixedUri($binaryFileId)), |
|
121
|
|
|
]); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function logMissingFile($id) |
|
126
|
|
|
{ |
|
127
|
|
|
if (!isset($this->logger)) { |
|
128
|
|
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
$this->logger->info("BinaryFile with id $id not found"); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|