-
Notifications
You must be signed in to change notification settings - Fork 208
/
TolerantIOService.php
132 lines (114 loc) · 4.13 KB
/
TolerantIOService.php
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
<?php
/**
* This file is part of the eZ Publish Kernel package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\IO;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException;
use eZ\Publish\Core\IO\Exception\InvalidBinaryAbsolutePathException;
use eZ\Publish\Core\IO\Values\BinaryFile;
use eZ\Publish\Core\IO\Values\MissingBinaryFile;
use Psr\Log\LoggerInterface;
/**
* An extended IOService that tolerates physically missing files.
*
* Meant to be used on a "broken" instance where the storage directory isn't in sync with the database.
*
* Note that it will still return false when exists() is used.
*/
class TolerantIOService extends IOService
{
/** @var \Psr\Log\LoggerInterface */
protected $logger;
public function setLogger(LoggerInterface $logger = null)
{
$this->logger = $logger;
}
/**
* Deletes $binaryFile.
*
* @param \eZ\Publish\Core\IO\Values\BinaryFile $binaryFile
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue If the binary file is invalid
* @throws \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException If the binary file isn't found
*/
public function deleteBinaryFile(BinaryFile $binaryFile)
{
$this->checkBinaryFileId($binaryFile->id);
$spiUri = $this->getPrefixedUri($binaryFile->id);
try {
$this->metadataHandler->delete($spiUri);
} catch (BinaryFileNotFoundException $e) {
$this->logMissingFile($binaryFile->uri);
$logged = true;
}
try {
$this->binarydataHandler->delete($spiUri);
} catch (BinaryFileNotFoundException $e) {
if (!isset($logged)) {
$this->logMissingFile($binaryFile->uri);
}
}
}
/**
* Loads the binary file with $binaryFileId.
*
* @param string $binaryFileId
*
* @return \eZ\Publish\Core\IO\Values\BinaryFile|\eZ\Publish\Core\IO\Values\MissingBinaryFile
*
* @throws \eZ\Publish\Core\IO\Exception\InvalidBinaryAbsolutePathException
*/
public function loadBinaryFile($binaryFileId)
{
$this->checkBinaryFileId($binaryFileId);
if ($this->isAbsolutePath($binaryFileId)) {
throw new InvalidBinaryAbsolutePathException($binaryFileId);
}
try {
$spiBinaryFile = $this->metadataHandler->load($this->getPrefixedUri($binaryFileId));
} catch (BinaryFileNotFoundException $e) {
$this->logMissingFile($binaryFileId);
return new MissingBinaryFile([
'id' => $binaryFileId,
'uri' => $this->binarydataHandler->getUri($this->getPrefixedUri($binaryFileId)),
]);
}
if (!isset($spiBinaryFile->uri)) {
$spiBinaryFile->uri = $this->binarydataHandler->getUri($spiBinaryFile->id);
}
return $this->buildDomainBinaryFileObject($spiBinaryFile);
}
public function loadBinaryFileByUri($binaryFileUri)
{
$binaryFileId = $this->binarydataHandler->getIdFromUri($binaryFileUri);
try {
$binaryFileId = $this->removeUriPrefix($binaryFileId);
} catch (InvalidArgumentException $e) {
$this->logMissingFile($binaryFileUri);
return new MissingBinaryFile([
'id' => $binaryFileId,
'uri' => $binaryFileUri,
]);
}
try {
return $this->loadBinaryFile($binaryFileId);
} catch (BinaryFileNotFoundException $e) {
$this->logMissingFile($binaryFileUri);
return new MissingBinaryFile([
'id' => $binaryFileId,
'uri' => $this->binarydataHandler->getUri($this->getPrefixedUri($binaryFileId)),
]);
}
}
private function logMissingFile($id)
{
if (!isset($this->logger)) {
return;
}
$this->logger->info("BinaryFile with id $id not found");
}
}