/
AttachedFile.php
255 lines (217 loc) · 6.77 KB
/
AttachedFile.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2018-2022 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Core\Model;
use FacturaScripts\Core\Base\FileManager;
use FacturaScripts\Core\Base\MyFilesToken;
use finfo;
/**
* Class to manage attached files.
*
* @author Carlos García Gómez <carlos@facturascripts.com>
* @author Francesc Pineda Segarra <francesc.pineda.segarra@gmail.com>
*/
class AttachedFile extends Base\ModelOnChangeClass
{
use Base\ModelTrait;
const MAX_FILENAME_LEN = 100;
/**
* Date.
*
* @var string
*/
public $date;
/**
* Contains the file name.
*
* @var string
*/
public $filename;
/**
* Hour.
*
* @var string
*/
public $hour;
/**
* Primary key.
*
* @var int
*/
public $idfile;
/**
* Content the mime content type.
*
* @var string
*/
public $mimetype;
/**
* Contains the relative path to file.
*
* @var string
*/
public $path;
/**
* The size of the file in bytes.
*
* @var int
*/
public $size;
public function clear()
{
parent::clear();
$this->date = date(self::DATE_STYLE);
$this->hour = date(self::HOUR_STYLE);
$this->size = 0;
}
public function delete(): bool
{
$fullPath = $this->getFullPath();
if (file_exists($fullPath) && false === unlink($fullPath)) {
$this->toolBox()->i18nLog()->warning('cant-delete-file', ['%fileName%' => $this->path]);
return false;
}
return parent::delete();
}
public function getExtension(): string
{
$parts = explode('.', strtolower($this->filename));
return count($parts) > 1 ? end($parts) : '';
}
public function getFullPath(): string
{
return FS_FOLDER . '/' . $this->path;
}
public function getStorageLimit(): int
{
return defined('FS_STORAGE_LIMIT') ? (int)FS_STORAGE_LIMIT : 0;
}
public function getStorageUsed(array $exclude = []): int
{
$sql = 'SELECT SUM(size) as size FROM ' . static::tableName();
if ($exclude) {
$sql .= ' WHERE idfile NOT IN (' . implode(',', $exclude) . ')';
}
foreach (static::$dataBase->select($sql) as $row) {
return (int)$row ['size'];
}
return 0;
}
public static function primaryColumn(): string
{
return 'idfile';
}
public function primaryDescriptionColumn(): string
{
return 'filename';
}
public static function tableName(): string
{
return 'attached_files';
}
public function test(): bool
{
if (empty($this->idfile)) {
$this->idfile = $this->newCode();
return $this->setFile() && parent::test();
}
$this->filename = self::toolBox()::utils()::noHtml($this->filename);
$this->mimetype = self::toolBox()::utils()::noHtml($this->mimetype);
$this->path = self::toolBox()::utils()::noHtml($this->path);
return parent::test();
}
public function url(string $type = 'auto', string $list = 'List'): string
{
switch ($type) {
case 'download':
return $this->path . '?myft=' . MyFilesToken::get($this->path, false);
case 'download-permanent':
return $this->path . '?myft=' . MyFilesToken::get($this->path, true);
default:
return parent::url($type, $list);
}
}
protected function fixFileName(string $original): string
{
$fixed = self::toolBox()::utils()::noHtml($original);
if (strlen($fixed) <= static::MAX_FILENAME_LEN) {
return empty($fixed) ? '' : strtolower($fixed);
}
$parts = explode('.', strtolower($fixed));
$extension = count($parts) > 1 ? end($parts) : '';
$name = substr($fixed, 0, static::MAX_FILENAME_LEN - strlen('.' . $extension));
return $name . '.' . $extension;
}
/**
* @param string $field
*
* @return bool
*/
protected function onChange($field)
{
switch ($field) {
case 'path':
if ($this->previousData['path']) {
// remove old file
unlink(\FS_FOLDER . '/' . $this->previousData['path']);
}
return $this->setFile();
default:
return parent::onChange($field);
}
}
/**
* Examine and move new file set.
*
* @return bool
*/
protected function setFile(): bool
{
$this->filename = $this->fixFileName($this->path);
$newFolder = 'MyFiles/' . date('Y/m', strtotime($this->date));
$newFolderPath = FS_FOLDER . '/' . $newFolder;
if (false === FileManager::createFolder($newFolderPath, true)) {
$this->toolBox()->i18nLog()->critical('cant-create-folder', ['%folderName%' => $newFolder]);
return false;
}
$currentPath = FS_FOLDER . '/MyFiles/' . $this->path;
if ($this->getStorageLimit() > 0 &&
filesize($currentPath) + $this->getStorageUsed([$this->idfile]) > $this->getStorageLimit()) {
$this->toolBox()->i18nLog()->critical('storage-limit-reached');
unlink($currentPath);
return false;
}
if (empty($this->path) ||
false === rename($currentPath, $newFolderPath . '/' . $this->idfile . '.' . $this->getExtension())) {
return false;
}
$this->path = $newFolder . '/' . $this->idfile . '.' . $this->getExtension();
$this->size = filesize($this->getFullPath());
$info = new finfo();
$this->mimetype = $info->file($this->getFullPath(), FILEINFO_MIME_TYPE);
if (strlen($this->mimetype) > 100) {
$this->mimetype = substr($this->mimetype, 0, 100);
}
return true;
}
protected function setPreviousData(array $fields = [])
{
$more = ['path'];
parent::setPreviousData(array_merge($more, $fields));
}
}