|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* part-db version 0.1 |
|
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
|
6
|
|
|
* http://www.cl-projects.de/ |
|
7
|
|
|
* |
|
8
|
|
|
* part-db version 0.2+ |
|
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
|
10
|
|
|
* http://code.google.com/p/part-db/ |
|
11
|
|
|
* |
|
12
|
|
|
* Part-DB Version 0.4+ |
|
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
|
14
|
|
|
* https://github.com/jbtronics |
|
15
|
|
|
* |
|
16
|
|
|
* This program is free software; you can redistribute it and/or |
|
17
|
|
|
* modify it under the terms of the GNU General Public License |
|
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
19
|
|
|
* of the License, or (at your option) any later version. |
|
20
|
|
|
* |
|
21
|
|
|
* This program is distributed in the hope that it will be useful, |
|
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
24
|
|
|
* GNU General Public License for more details. |
|
25
|
|
|
* |
|
26
|
|
|
* You should have received a copy of the GNU General Public License |
|
27
|
|
|
* along with this program; if not, write to the Free Software |
|
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
namespace App\Services; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
use App\Entity\Attachment; |
|
36
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
37
|
|
|
use SebastianBergmann\CodeCoverage\Node\File; |
|
|
|
|
|
|
38
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
39
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
40
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
41
|
|
|
|
|
42
|
|
|
class AttachmentHelper |
|
43
|
|
|
{ |
|
44
|
|
|
|
|
45
|
|
|
protected $base_path; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct(ParameterBagInterface $params, KernelInterface $kernel) |
|
48
|
|
|
{ |
|
49
|
|
|
$tmp_base_path = $params->get('media_directory'); |
|
50
|
|
|
|
|
51
|
|
|
$fs = new Filesystem(); |
|
52
|
|
|
|
|
53
|
|
|
//Determine if it is an absolute path, or if we need to create a real absolute one out of it |
|
54
|
|
|
if ($fs->isAbsolutePath($tmp_base_path)) { |
|
55
|
|
|
$this->base_path = $tmp_base_path; |
|
56
|
|
|
} else { |
|
57
|
|
|
$this->base_path = realpath($kernel->getProjectDir() . $tmp_base_path); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns the absolute filepath of the attachment. Null is returned, if the attachment is externally saved. |
|
63
|
|
|
* @param Attachment $attachment The attachment for which the filepath should be determined |
|
64
|
|
|
* @return string|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public function toAbsoluteFilePath(Attachment $attachment): ?string |
|
67
|
|
|
{ |
|
68
|
|
|
if ($attachment->isExternal()) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$path = $attachment->getPath(); |
|
73
|
|
|
$path = str_replace("%BASE%", $this->base_path, $path); |
|
74
|
|
|
return realpath($path); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Checks if the file in this attachement is existing. This works for files on the HDD, and for URLs |
|
79
|
|
|
* (it's not checked if the ressource behind the URL is really existing, so for every external attachment true is returned). |
|
80
|
|
|
* |
|
81
|
|
|
* @param Attachment $attachment The attachment for which the existence should be checked |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool True if the file is existing. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function isFileExisting(Attachment $attachment): bool |
|
86
|
|
|
{ |
|
87
|
|
|
return file_exists($this->toAbsoluteFilePath($attachment)) || $attachment->isExternal(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns the filesize of the attachments in bytes. |
|
92
|
|
|
* For external attachments, null is returned. |
|
93
|
|
|
* |
|
94
|
|
|
* @param Attachment $attachment The filesize for which the filesize should be calculated. |
|
95
|
|
|
* @return int|null |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getFileSize(Attachment $attachment): ?int |
|
98
|
|
|
{ |
|
99
|
|
|
if ($attachment->isExternal()) { |
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return filesize($this->toAbsoluteFilePath($attachment)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns a human readable version of the attachment file size. |
|
108
|
|
|
* For external attachments, null is returned. |
|
109
|
|
|
* |
|
110
|
|
|
* @param Attachment $attachment |
|
111
|
|
|
* @param int $decimals The number of decimals numbers that should be printed |
|
112
|
|
|
* @return string|null A string like 1.3M |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getHumanFileSize(Attachment $attachment, $decimals = 2): ?string |
|
115
|
|
|
{ |
|
116
|
|
|
$bytes = $this->getFileSize($attachment); |
|
117
|
|
|
|
|
118
|
|
|
if ($bytes == null) { |
|
|
|
|
|
|
119
|
|
|
return null; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
//Format filesize for human reading |
|
123
|
|
|
//Taken from: https://www.php.net/manual/de/function.filesize.php#106569 and slightly modified |
|
124
|
|
|
|
|
125
|
|
|
$sz = 'BKMGTP'; |
|
126
|
|
|
$factor = (int) floor((strlen($bytes) - 1) / 3); |
|
127
|
|
|
return sprintf("%.{$decimals}f", $bytes / 1024 ** $factor) . @$sz[$factor]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths