-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileHandler.php
142 lines (110 loc) · 3.28 KB
/
FileHandler.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
<?php
namespace Rcsofttech85\FileHandler;
use finfo;
use Rcsofttech85\FileHandler\Exception\FileHandlerException;
use Rcsofttech85\FileHandler\Validator\FileValidatorTrait;
use ZipArchive;
class FileHandler
{
use FileValidatorTrait;
public const ARRAY_FORMAT = 'array';
/**
* @var array<resource>|null
*/
private null|array $files = [];
/**
* @throws FileHandlerException
*/
public function open(
string $filename,
string $mode = "w",
bool $include_path = false,
mixed $context = null
): self {
$filename = $this->sanitize($filename);
$file = fopen($filename, $mode, $include_path, $context);
if (!$file) {
throw new FileHandlerException('File not found');
}
$this->files[] = $file;
return $this;
}
/**
* @param string $data
* @param int<0, max>|null $length
* @return void
* @throws FileHandlerException
*/
public function write(string $data, ?int $length = null): void
{
if (!$this->files) {
throw new FileHandlerException('no files available to write');
}
foreach ($this->files as $file) {
$byteWritten = fwrite($file, $data, $length);
if (!$byteWritten) {
throw new FileHandlerException('Error writing to file');
}
}
}
/**
* @throws FileHandlerException
*/
public function compress(string $filename, string $zipFilename, int $flag = ZipArchive::CREATE): void
{
$filename = $this->validateFileName($filename);
$zip = new ZipArchive();
if (true !== $zip->open($zipFilename, $flag)) {
throw new FileHandlerException('Failed to create the ZIP archive.');
}
$zip->addFile($filename);
$zip->close();
}
/**
* @throws FileHandlerException
*/
public function getMimeType(string $filename): string|false
{
$filename = $this->validateFileName($filename);
$fileInfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $fileInfo->file($filename);
if ($mimeType === 'application/octet-stream') {
throw new FileHandlerException('unknown mime type');
}
return $mimeType;
}
/**
* @throws FileHandlerException
*/
public function decompress(string $zipFilename, string $extractPath = "./", int $flag = ZipArchive::CREATE): void
{
$zipFilename = $this->validateFileName($zipFilename);
$zip = new ZipArchive();
if (true !== $zip->open($zipFilename, $flag)) {
throw new FileHandlerException('Invalid or uninitialized Zip object');
}
if (!$zip->extractTo($extractPath)) {
throw new FileHandlerException('Failed to extract the ZIP archive.');
}
$zip->close();
}
public function close(): void
{
foreach ($this->files as $file) {
fclose($file);
}
$this->resetFiles();
}
public function resetFiles(): void
{
$this->files = null;
}
/**
* @throws FileHandlerException
*/
public function delete(string $filename): void
{
$filename = $this->validateFileName($filename);
unlink($filename);
}
}