-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.php
More file actions
602 lines (524 loc) · 17 KB
/
Copy pathFile.php
File metadata and controls
602 lines (524 loc) · 17 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
<?php
namespace Pet\File;
use Pet\File\Exception\FileException;
/**
* Объектное представление файла в файловой системе.
*
* Предоставляет удобный интерфейс для выполнения операций с отдельным файлом:
* чтение, запись, копирование, перемещение, удаление, получение метаданных.
*
* @package Pet\File
*/
class File
{
/** @var string Полный путь к файлу */
private string $path;
/** @var string|null Кешированный MIME-тип */
private ?string $mimeType = null;
/** @var int|null Кешированный размер файла в байтах */
private ?int $size = null;
/** @var string|null Кешированный хеш файла */
private ?string $hash = null;
/**
* @param string $path Полный путь к файлу.
*/
public function __construct(string $path)
{
$this->path = $path;
}
/**
* Создаёт экземпляр File из массива загруженного файла ($_FILES).
*
* @param array $file Элемент из глобального массива $_FILES.
* @return self
*
* @throws FileException Если при загрузке произошла ошибка.
*/
public static function fromUpload(array $file): self
{
if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
throw FileException::uploadError(self::uploadErrorMessage($file['error'] ?? UPLOAD_ERR_NO_FILE));
}
return new self($file['tmp_name']);
}
/**
* Возвращает полный путь к файлу.
*
* @return string
*/
public function path(): string
{
return $this->path;
}
/**
* Проверяет существование файла в файловой системе.
*
* @return bool
*/
public function exists(): bool
{
return file_exists($this->path);
}
/**
* Проверяет, является ли путь обычным файлом.
*
* @return bool
*/
public function isFile(): bool
{
return is_file($this->path);
}
/**
* Проверяет, доступен ли файл для чтения.
*
* @return bool
*/
public function isReadable(): bool
{
return is_readable($this->path);
}
/**
* Проверяет, доступен ли файл для записи.
*
* @return bool
*/
public function isWritable(): bool
{
return is_writable($this->path);
}
/**
* Возвращает имя файла с расширением (basename).
*
* @return string
*/
public function name(): string
{
return pathinfo($this->path, PATHINFO_BASENAME);
}
/**
* Возвращает имя файла без расширения.
*
* @return string
*/
public function filename(): string
{
return pathinfo($this->path, PATHINFO_FILENAME);
}
/**
* Возвращает расширение файла.
*
* @return string
*/
public function extension(): string
{
return pathinfo($this->path, PATHINFO_EXTENSION);
}
/**
* Возвращает путь к директории файла.
*
* @return string
*/
public function dirname(): string
{
return pathinfo($this->path, PATHINFO_DIRNAME);
}
/**
* Возвращает размер файла в байтах.
*
* @return int
*
* @throws FileException Если файл не существует.
*/
public function size(): int
{
if ($this->size === null) {
$this->ensureExists();
$this->size = filesize($this->path);
}
return $this->size;
}
/**
* Возвращает размер файла в человекочитаемом формате (B, KB, MB, GB, TB).
*
* @return string
*/
public function sizeFormatted(): string
{
$bytes = $this->size();
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$i = 0;
while ($bytes >= 1024 && $i < count($units) - 1) {
$bytes /= 1024;
$i++;
}
return round($bytes, 2) . ' ' . $units[$i];
}
/**
* Возвращает MIME-тип файла.
*
* @return string
*
* @throws FileException Если файл не существует.
*/
public function mimeType(): string
{
if ($this->mimeType === null) {
$this->ensureExists();
$detected = mime_content_type($this->path);
$this->mimeType = $detected ?: 'application/octet-stream';
}
return $this->mimeType;
}
/**
* Возвращает хеш файла (по умолчанию md5).
*
* @param string $algo Алгоритм хеширования (md5, sha1, sha256 и т.д.).
* @return string
*
* @throws FileException Если файл не существует.
*/
public function hash(string $algo = 'md5'): string
{
$key = $algo . '_hash';
if ($this->hash === null) {
$this->ensureExists();
$this->hash = hash_file($algo, $this->path) ?: '';
}
return $this->hash;
}
/**
* Читает содержимое файла в строку.
*
* @return string
*
* @throws FileException Если файл не существует или недоступен для чтения.
*/
public function content(): string
{
$this->ensureExists();
$this->ensureReadable();
$content = file_get_contents($this->path);
if ($content === false) {
throw FileException::notReadable($this->path);
}
return $content;
}
/**
* Записывает данные в файл (перезаписывает содержимое).
*
* @param string $content Данные для записи.
* @return self
*
* @throws FileException Если файл недоступен для записи.
*/
public function put(string $content): self
{
$bytes = file_put_contents($this->path, $content);
if ($bytes === false) {
throw FileException::notWritable($this->path);
}
$this->size = $bytes;
$this->hash = null;
$this->mimeType = null;
return $this;
}
/**
* Дописывает данные в конец файла.
*
* @param string $content Данные для добавления.
* @return self
*
* @throws FileException Если файл недоступен для записи.
*/
public function append(string $content): self
{
$bytes = file_put_contents($this->path, $content, FILE_APPEND);
if ($bytes === false) {
throw FileException::notWritable($this->path);
}
$this->size = null;
$this->hash = null;
return $this;
}
/**
* Добавляет данные в начало файла (препендит).
*
* @param string $content Данные для добавления.
* @return self
*/
public function prepend(string $content): self
{
$existing = $this->exists() ? $this->content() : '';
return $this->put($content . $existing);
}
/**
* Копирует файл в указанное место.
*
* @param string $destination Путь назначения.
* @return self Новый экземпляр File для скопированного файла.
*
* @throws FileException Если исходный файл не существует или не удалось скопировать.
*/
public function copy(string $destination): self
{
$this->ensureExists();
$dir = dirname($destination);
$this->ensureDir($dir);
if (!copy($this->path, $destination)) {
throw new FileException("Не удалось скопировать файл в {$destination}");
}
return new self($destination);
}
/**
* Перемещает файл в указанное место.
*
* @param string $destination Путь назначения.
* @return self Тот же экземпляр с обновлённым путём.
*
* @throws FileException Если исходный файл не существует или не удалось переместить.
*/
public function move(string $destination): self
{
$this->ensureExists();
$dir = dirname($destination);
$this->ensureDir($dir);
if (!rename($this->path, $destination)) {
throw new FileException("Не удалось переместить файл в {$destination}");
}
$this->path = $destination;
$this->size = null;
$this->hash = null;
$this->mimeType = null;
return $this;
}
/**
* Переименовывает файл в той же директории.
*
* @param string $newName Новое имя файла.
* @return self
*/
public function rename(string $newName): self
{
$dir = $this->dirname();
return $this->move($dir . DIRECTORY_SEPARATOR . $newName);
}
/**
* Удаляет файл.
*
* @return bool true если файл был удалён, false если файл не существовал.
*/
public function delete(): bool
{
if (!$this->exists()) {
return false;
}
return unlink($this->path);
}
/**
* Проверяет, является ли файл изображением.
*
* @return bool
*/
public function isImage(): bool
{
return str_starts_with($this->mimeType(), 'image/');
}
/**
* Проверяет, является ли файл текстовым.
*
* @return bool
*/
public function isText(): bool
{
return str_starts_with($this->mimeType(), 'text/');
}
/**
* Проверяет, является ли файл архивом.
*
* @return bool
*/
public function isArchive(): bool
{
return in_array($this->mimeType(), [
'application/zip',
'application/x-rar-compressed',
'application/gzip',
'application/x-7z-compressed',
'application/x-tar',
], true);
}
/**
* Проверяет, является ли файл PDF-документом.
*
* @return bool
*/
public function isPdf(): bool
{
return $this->mimeType() === 'application/pdf';
}
/**
* Читает файл построчно, пропуская пустые строки.
*
* @return string[]
*/
public function lines(): array
{
return file($this->path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [];
}
/**
* Возвращает время последней модификации файла (Unix timestamp).
*
* @return int
*
* @throws FileException Если файл не существует.
*/
public function lastModified(): int
{
$this->ensureExists();
return filemtime($this->path);
}
/**
* Возвращает права доступа к файлу в восьмеричном формате (например, 0644).
*
* @return string
*
* @throws FileException Если файл не существует.
*/
public function permissions(): string
{
$this->ensureExists();
return substr(sprintf('%o', fileperms($this->path)), -4);
}
/**
* Возвращает UID владельца файла.
*
* @return int
*
* @throws FileException Если файл не существует.
*/
public function owner(): int
{
$this->ensureExists();
return fileowner($this->path);
}
/**
* Возвращает GID группы файла.
*
* @return int
*
* @throws FileException Если файл не существует.
*/
public function group(): int
{
$this->ensureExists();
return filegroup($this->path);
}
/**
* Преобразует информацию о файле в ассоциативный массив.
*
* @return array{path: string, name: string, filename: string, extension: string, dirname: string, size: int, size_formatted: string, mime_type: string, is_image: bool, is_text: bool, last_modified: int, permissions: string}
*/
public function toArray(): array
{
return [
'path' => $this->path,
'name' => $this->name(),
'filename' => $this->filename(),
'extension' => $this->extension(),
'dirname' => $this->dirname(),
'size' => $this->size(),
'size_formatted' => $this->sizeFormatted(),
'mime_type' => $this->mimeType(),
'is_image' => $this->isImage(),
'is_text' => $this->isText(),
'last_modified' => $this->lastModified(),
'permissions' => $this->permissions(),
];
}
/**
* Конвертирует изображение текущего файла в WebP.
*
* Сохраняет .webp файл рядом с оригиналом.
*
* @param int $quality Качество WebP (0-100)
* @return self Новый экземпляр File для созданного .webp файла
*
* @throws FileException Если исходный файл не существует или не удалось конвертировать.
*/
public function convertImage(int $quality = 100): self
{
$this->ensureExists();
$image = new Image($this->path);
$image->convertToWebp($quality);
$dest = $this->dirname() . DIRECTORY_SEPARATOR . $this->filename() . '.webp';
return new self($dest);
}
/**
* Возвращает путь к файлу при строковом преобразовании.
*
* @return string
*/
public function __toString(): string
{
return $this->path;
}
/**
* Проверяет существование файла и выбрасывает исключение, если его нет.
*
* @return void
*
* @throws FileException
*/
private function ensureExists(): void
{
if (!$this->exists()) {
throw FileException::notFound($this->path);
}
}
/**
* Проверяет доступность файла для чтения.
*
* @return void
*
* @throws FileException
*/
private function ensureReadable(): void
{
if (!$this->isReadable()) {
throw FileException::notReadable($this->path);
}
}
/**
* Создаёт директорию, если она не существует.
*
* @param string $dir Путь к директории.
* @return void
*
* @throws FileException Если не удалось создать директорию.
*/
private function ensureDir(string $dir): void
{
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
throw FileException::directoryNotCreated($dir);
}
}
/**
* Преобразует код ошибки загрузки в человекочитаемое сообщение.
*
* @param int $error Код ошибки UPLOAD_ERR_*.
* @return string
*/
private static function uploadErrorMessage(int $error): string
{
return match ($error) {
UPLOAD_ERR_INI_SIZE => 'Размер файла превышает максимально допустимый (upload_max_filesize)',
UPLOAD_ERR_FORM_SIZE => 'Размер файла превышает максимально допустимый (MAX_FILE_SIZE)',
UPLOAD_ERR_PARTIAL => 'Файл был загружен только частично',
UPLOAD_ERR_NO_FILE => 'Файл не был загружен',
UPLOAD_ERR_NO_TMP_DIR => 'Отсутствует временная директория для загрузки',
UPLOAD_ERR_CANT_WRITE => 'Не удалось записать файл на диск',
UPLOAD_ERR_EXTENSION => 'Загрузка файла была остановлена расширением',
default => 'Неизвестная ошибка загрузки файла',
};
}
}