|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gvera\Helpers\http; |
|
4
|
|
|
|
|
5
|
|
|
use Gvera\Helpers\config\Config; |
|
6
|
|
|
use Gvera\Helpers\fileSystem\File; |
|
7
|
|
|
use Gvera\Exceptions\NotFoundException; |
|
8
|
|
|
use Gvera\Exceptions\InvalidFileTypeException; |
|
9
|
|
|
|
|
10
|
|
|
class FileManager |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
private array $files = []; |
|
|
|
|
|
|
14
|
|
|
private Config $config; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(Config $config) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->config = $config; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param $source |
|
23
|
|
|
* @param string|null $replaceName |
|
24
|
|
|
*/ |
|
25
|
|
|
public function buildFilesFromSource($source, ?string $replaceName = null) |
|
26
|
|
|
{ |
|
27
|
|
|
foreach ($source as $fileKey => $file) { |
|
28
|
|
|
$imageFileType = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); |
|
29
|
|
|
$name = $replaceName ? $replaceName . "." . $imageFileType : $file['name']; |
|
30
|
|
|
if ($imageFileType === "") { |
|
31
|
|
|
$name = ""; |
|
32
|
|
|
} |
|
33
|
|
|
$newFile = new File(); |
|
34
|
|
|
$newFile->setName($name); |
|
35
|
|
|
$newFile->setSize($file['size']); |
|
36
|
|
|
$newFile->setTemporaryName($file['tmp_name']); |
|
37
|
|
|
$newFile->setError($file['error']); |
|
38
|
|
|
$newFile->settype($file['type']); |
|
39
|
|
|
$this->files[$fileKey] = $newFile; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $name |
|
45
|
|
|
* @return File |
|
46
|
|
|
* @throws NotFoundException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getByName($name): File |
|
49
|
|
|
{ |
|
50
|
|
|
$file = $this->files[$name]; |
|
51
|
|
|
if (null === $file) { |
|
52
|
|
|
throw new NotFoundException("The file you are trying to get is not uploaded"); |
|
53
|
|
|
} |
|
54
|
|
|
return $file; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $targetDirectory |
|
59
|
|
|
* @param File $file |
|
60
|
|
|
* @throws InvalidFileTypeException |
|
61
|
|
|
* @throws NotFoundException |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public function saveToFileSystem(string $targetDirectory, File $file): bool |
|
65
|
|
|
{ |
|
66
|
|
|
if ($file->getError() === 4) { |
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (!in_array($file, $this->files)) { |
|
71
|
|
|
throw new NotFoundException("The file you are trying to move is not uploaded"); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!in_array($file->getType(), $this->config->getConfigItem('allowed_upload_file_types'))) { |
|
75
|
|
|
throw new InvalidFileTypeException( |
|
76
|
|
|
"The file you are trying to move does not match the server's requirement" |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$uploadPath = $targetDirectory . $file->getName(); |
|
81
|
|
|
return move_uploaded_file($file->getTemporaryName(), $uploadPath); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function recursiveRemove($dir) |
|
85
|
|
|
{ |
|
86
|
|
|
$structure = glob(rtrim($dir, "/").'/*'); |
|
87
|
|
|
if (is_array($structure)) { |
|
88
|
|
|
foreach ($structure as $file) { |
|
89
|
|
|
if (is_dir($file)) { |
|
90
|
|
|
$this->recursiveRemove($file); |
|
91
|
|
|
} elseif (is_file($file)) { |
|
92
|
|
|
unlink($file); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
rmdir($dir); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|