|
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\Attachments\Attachment; |
|
36
|
|
|
use App\Entity\Attachments\AttachmentContainingDBElement; |
|
37
|
|
|
use App\Entity\Attachments\AttachmentTypeAttachment; |
|
38
|
|
|
use App\Entity\Attachments\CategoryAttachment; |
|
39
|
|
|
use App\Entity\Attachments\CurrencyAttachment; |
|
40
|
|
|
use App\Entity\Attachments\DeviceAttachment; |
|
41
|
|
|
use App\Entity\Attachments\FootprintAttachment; |
|
42
|
|
|
use App\Entity\Attachments\GroupAttachment; |
|
43
|
|
|
use App\Entity\Attachments\ManufacturerAttachment; |
|
44
|
|
|
use App\Entity\Attachments\MeasurementUnitAttachment; |
|
45
|
|
|
use App\Entity\Attachments\PartAttachment; |
|
46
|
|
|
use App\Entity\Attachments\StorelocationAttachment; |
|
47
|
|
|
use App\Entity\Attachments\SupplierAttachment; |
|
48
|
|
|
use App\Entity\Attachments\UserAttachment; |
|
49
|
|
|
use App\Services\Attachments\AttachmentPathResolver; |
|
50
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
51
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* This service contains basic commonly used functions to work with attachments. |
|
55
|
|
|
* Especially this services gives you important infos about attachments, that can not be retrieved via the entities |
|
56
|
|
|
* (like filesize or if a file is existing). |
|
57
|
|
|
* |
|
58
|
|
|
* Special operations like getting attachment urls or handling file uploading/downloading are in their own services. |
|
59
|
|
|
* @package App\Services |
|
60
|
|
|
*/ |
|
61
|
|
|
class AttachmentManager |
|
62
|
|
|
{ |
|
63
|
|
|
|
|
64
|
|
|
protected $pathResolver; |
|
65
|
|
|
|
|
66
|
|
|
public function __construct(AttachmentPathResolver $pathResolver) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->pathResolver = $pathResolver; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Gets an SPLFileInfo object representing the file associated with the attachment. |
|
73
|
|
|
* @param Attachment $attachment The attachment for which the file should be generated |
|
74
|
|
|
* @return \SplFileInfo|null The fileinfo for the attachment file. Null, if the attachment is external or has |
|
75
|
|
|
* invalid file. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function attachmentToFile(Attachment $attachment) : ?\SplFileInfo |
|
78
|
|
|
{ |
|
79
|
|
|
if ($attachment->isExternal() || !$this->isFileExisting($attachment)) { |
|
80
|
|
|
return null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return new \SplFileInfo($this->toAbsoluteFilePath($attachment)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the absolute filepath of the attachment. Null is returned, if the attachment is externally saved, |
|
88
|
|
|
* or is not existing. |
|
89
|
|
|
* @param Attachment $attachment The attachment for which the filepath should be determined |
|
90
|
|
|
* @return string|null |
|
91
|
|
|
*/ |
|
92
|
|
|
public function toAbsoluteFilePath(Attachment $attachment): ?string |
|
93
|
|
|
{ |
|
94
|
|
|
if (empty($attachment->getPath())) { |
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($attachment->isExternal()) { |
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$path = $this->pathResolver->placeholderToRealPath($attachment->getPath()); |
|
103
|
|
|
|
|
104
|
|
|
//realpath does not work with null as argument |
|
105
|
|
|
if ($path === null) { |
|
106
|
|
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$tmp = realpath($path); |
|
110
|
|
|
//If path is not existing realpath returns false. |
|
111
|
|
|
if ($tmp === false) { |
|
112
|
|
|
return null; |
|
113
|
|
|
} |
|
114
|
|
|
return $tmp; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Checks if the file in this attachement is existing. This works for files on the HDD, and for URLs |
|
119
|
|
|
* (it's not checked if the ressource behind the URL is really existing, so for every external attachment true is returned). |
|
120
|
|
|
* |
|
121
|
|
|
* @param Attachment $attachment The attachment for which the existence should be checked |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool True if the file is existing. |
|
124
|
|
|
*/ |
|
125
|
|
|
public function isFileExisting(Attachment $attachment): bool |
|
126
|
|
|
{ |
|
127
|
|
|
if (empty($attachment->getPath())) { |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return file_exists($this->toAbsoluteFilePath($attachment)) || $attachment->isExternal(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Returns the filesize of the attachments in bytes. |
|
136
|
|
|
* For external attachments or not existing attachments, null is returned. |
|
137
|
|
|
* |
|
138
|
|
|
* @param Attachment $attachment The filesize for which the filesize should be calculated. |
|
139
|
|
|
* @return int|null |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getFileSize(Attachment $attachment): ?int |
|
142
|
|
|
{ |
|
143
|
|
|
if ($attachment->isExternal()) { |
|
144
|
|
|
return null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if (!$this->isFileExisting($attachment)) { |
|
148
|
|
|
return null; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$tmp = filesize($this->toAbsoluteFilePath($attachment)); |
|
152
|
|
|
return $tmp !== false ? $tmp : null; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Returns a human readable version of the attachment file size. |
|
157
|
|
|
* For external attachments, null is returned. |
|
158
|
|
|
* |
|
159
|
|
|
* @param Attachment $attachment |
|
160
|
|
|
* @param int $decimals The number of decimals numbers that should be printed |
|
161
|
|
|
* @return string|null A string like 1.3M |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getHumanFileSize(Attachment $attachment, $decimals = 2): ?string |
|
164
|
|
|
{ |
|
165
|
|
|
$bytes = $this->getFileSize($attachment); |
|
166
|
|
|
|
|
167
|
|
|
if ($bytes == null) { |
|
|
|
|
|
|
168
|
|
|
return null; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
//Format filesize for human reading |
|
172
|
|
|
//Taken from: https://www.php.net/manual/de/function.filesize.php#106569 and slightly modified |
|
173
|
|
|
|
|
174
|
|
|
$sz = 'BKMGTP'; |
|
175
|
|
|
$factor = (int) floor((strlen($bytes) - 1) / 3); |
|
176
|
|
|
return sprintf("%.{$decimals}f", $bytes / 1024 ** $factor) . @$sz[$factor]; |
|
177
|
|
|
} |
|
178
|
|
|
} |