1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace AloiaCms; |
5
|
|
|
|
6
|
|
|
use Illuminate\Http\UploadedFile; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\Config; |
9
|
|
|
use Illuminate\Support\Facades\File; |
10
|
|
|
|
11
|
|
|
class FileManager |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/**@var string $uploaded_files_folder_path*/ |
15
|
|
|
private $uploaded_files_folder_path; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* FileManager constructor. |
19
|
|
|
*/ |
20
|
4 |
|
private function __construct() |
21
|
|
|
{ |
22
|
4 |
|
$this->uploaded_files_folder_path = Config::get('aloiacms.uploaded_files.folder_path'); |
23
|
|
|
|
24
|
4 |
|
$this->assertFolderPathExists(); |
25
|
4 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Open a new FileManager instance |
29
|
|
|
* |
30
|
|
|
* @return FileManager |
31
|
|
|
*/ |
32
|
4 |
|
public static function open(): FileManager |
33
|
|
|
{ |
34
|
4 |
|
return new static(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Move the UploadedFile to the chosen destination folder |
39
|
|
|
* |
40
|
|
|
* @param UploadedFile $file |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function upload(UploadedFile $file): bool |
44
|
|
|
{ |
45
|
|
|
$file->move($this->uploaded_files_folder_path, $file->getClientOriginalName()); |
46
|
|
|
|
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get all uploaded files in the destination folder |
52
|
|
|
* |
53
|
|
|
* @return Collection |
54
|
|
|
*/ |
55
|
4 |
|
public function get(): Collection |
56
|
|
|
{ |
57
|
4 |
|
return Collection::make(File::allFiles($this->uploaded_files_folder_path)) |
58
|
4 |
|
->map(function (string $file_path) { |
59
|
1 |
|
return \AloiaCms\DataSource\File::forFilePath($file_path); |
60
|
4 |
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get all uploaded files in the destination folder |
65
|
|
|
* |
66
|
|
|
* @return Collection |
67
|
|
|
*/ |
68
|
4 |
|
public static function all(): Collection |
69
|
|
|
{ |
70
|
4 |
|
return self::open()->get(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Delete the file for the given file name |
75
|
|
|
* |
76
|
|
|
* @param string $file_name |
77
|
|
|
*/ |
78
|
|
|
public function deleteForFilename(string $file_name): void |
79
|
|
|
{ |
80
|
|
|
$file_path = "{$this->uploaded_files_folder_path}/{$file_name}"; |
81
|
|
|
|
82
|
|
|
File::delete($file_path); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Delete the file for the given file name |
87
|
|
|
* |
88
|
|
|
* @param string $file_name |
89
|
|
|
*/ |
90
|
|
|
public static function delete(string $file_name): void |
91
|
|
|
{ |
92
|
|
|
self::open()->deleteForFilename($file_name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Assert the upload folder exists, otherwise create it |
97
|
|
|
*/ |
98
|
4 |
|
private function assertFolderPathExists() |
99
|
|
|
{ |
100
|
4 |
|
if (!is_dir($this->uploaded_files_folder_path)) { |
101
|
2 |
|
mkdir($this->uploaded_files_folder_path); |
102
|
|
|
} |
103
|
4 |
|
} |
104
|
|
|
} |
105
|
|
|
|