1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* balloon |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Balloon\App\Preview; |
13
|
|
|
|
14
|
|
|
use Balloon\Filesystem\Node\File; |
15
|
|
|
use MongoDB\BSON\ObjectId; |
16
|
|
|
use MongoDB\Database; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
|
19
|
|
|
class Preview |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Logger. |
23
|
|
|
* |
24
|
|
|
* @var LoggerInterface |
25
|
|
|
*/ |
26
|
|
|
protected $logger; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Database. |
30
|
|
|
* |
31
|
|
|
* @var Database |
32
|
|
|
*/ |
33
|
|
|
protected $db; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
* |
38
|
|
|
* @param Database $db |
39
|
|
|
* @param LoggerInterface $logger |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Database $db, LoggerInterface $logger) |
42
|
|
|
{ |
43
|
|
|
$this->db = $db; |
44
|
|
|
$this->logger = $logger; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get preview. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getPreview(File $file): string |
53
|
|
|
{ |
54
|
|
|
$preview = $file->getAppAttribute(__NAMESPACE__, 'preview'); |
55
|
|
|
if ($preview instanceof ObjectId) { |
|
|
|
|
56
|
|
|
try { |
57
|
|
|
$stream = $this->db->selectGridFSBucket(['bucketName' => 'thumbnail']) |
58
|
|
|
->openDownloadStream($preview); |
59
|
|
|
$contents = stream_get_contents($stream); |
60
|
|
|
fclose($stream); |
61
|
|
|
|
62
|
|
|
return $contents; |
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
$this->logger->warning('failed download preview from gridfs for file ['.$file->getId().']', [ |
65
|
|
|
'category' => get_class($this), |
66
|
|
|
'exception' => $e, |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new Exception\PreviewNotFound('preview does not exists'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Delete preview. |
76
|
|
|
* |
77
|
|
|
* @param File $file |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function deletePreview(File $file): bool |
82
|
|
|
{ |
83
|
|
|
$bucket = $this->db->selectGridFSBucket(['bucketName' => 'thumbnail']); |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$preview = $file->getAppAttribute(__NAMESPACE__, 'preview'); |
87
|
|
|
if ($preview instanceof ObjectId) { |
|
|
|
|
88
|
|
|
$references = $this->db->{'thumbnail.files'}->count([ |
89
|
|
|
'apps' => [__NAMESPACE__ => ['preview' => $preview]], |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
if (1 === $references) { |
93
|
|
|
$this->logger->debug('delete preview ['.$preview.'] from file ['.$file->getId().']', [ |
94
|
|
|
'category' => get_class($this), |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
$bucket->delete($preview); |
98
|
|
|
} else { |
99
|
|
|
$this->logger->debug('do not remove preview blob ['.$preview.'] from file ['.$file->getId().'], there are still other references left', [ |
100
|
|
|
'category' => get_class($this), |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return false; |
108
|
|
|
} catch (\Exception $e) { |
109
|
|
|
$this->logger->error('failed to remove preview from file ['.$file->getId().']', [ |
110
|
|
|
'category' => get_class($this), |
111
|
|
|
'exception' => $e, |
112
|
|
|
]); |
113
|
|
|
|
114
|
|
|
throw $e; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.