-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
MediaFolders.php
215 lines (184 loc) · 6.73 KB
/
MediaFolders.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
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype\Foundation\Media;
use Atomastic\Arrays\Arrays;
use Atomastic\Macroable\Macroable;
use Slim\Http\Environment;
use Slim\Http\Uri;
use function arrays;
use function filesystem;
use function filter;
use function flextype;
use function str_replace;
use function strings;
class MediaFolders
{
use Macroable;
/**
* Create a Media Folders Meta instance.
*/
public function meta(): MediaFoldersMeta
{
return new MediaFoldersMeta();
}
/**
* Fetch.
*
* @param string $id The path to folder.
* @param array $options Options array.
*
* @return Arrays Returns instance of The Arrays class.
*
* @access public
*/
public function fetch(string $id, array $options = []): Arrays
{
// Run event: onEntriesFetch
emitter()->emit('onMediaFoldersFetch');
// Single fetch helper
$single = static function ($id, $options) {
$result = [];
if (filesystem()->directory(flextype('media')->folders()->meta()->getDirectoryMetaLocation($id))->exists()) {
$id = trim($id, '/');
$result['basename'] = basename($id);
$result['dirname'] = dirname($id);
$result['path'] = $id;
$result['full_path'] = str_replace('/.meta', '', flextype('media')->folders()->meta()->getDirectoryMetaLocation($id));
$result['url'] = 'project/media/' . $id;
if (registry()->has('flextype.settings.url') && registry()->get('flextype.settings.url') !== '') {
$fullUrl = registry()->get('flextype.settings.url');
} else {
$fullUrl = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
}
$result['full_url'] = $fullUrl . '/project/media/' . $id;
}
$result = filter($result, $options);
return arrays($result);
};
if (
isset($options['collection']) &&
strings($options['collection'])->isTrue()
) {
$result = [];
foreach (filesystem()->find()->directories()->depth(0)->in(flextype('media')->folders()->meta()->getDirectoryMetaLocation($id)) as $folder) {
$result[$folder->getFilename()] = $single($id . '/' . $folder->getFilename(), [])->toArray();
}
$result = filter($result, $options);
return arrays($result);
}
return $single($id, $options);
}
/**
* Create folder
*
* @param string $id Unique identifier of the folder.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function create(string $id): bool
{
if (
! filesystem()->directory($this->getDirectoryLocation($id))->exists() &&
! filesystem()->directory(flextype('media')->folders()->meta()->getDirectoryMetaLocation($id))->exists()
) {
return filesystem()->directory($this->getDirectoryLocation($id))->create(0755, true) &&
filesystem()->directory(flextype('media')->folders()->meta()->getDirectoryMetaLocation($id))->create(0755, true);
}
return false;
}
/**
* Move folder
*
* @param string $id Unique identifier of the folder.
* @param string $newID New Unique identifier of the folder.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function move(string $id, string $newID): bool
{
if (
(filesystem()->directory($this->getDirectoryLocation($newID))->exists() === false &&
filesystem()->directory(flextype('media')->folders()->meta()->getDirectoryMetaLocation($newID))->exists() === false)
) {
return filesystem()->directory($this->getDirectoryLocation($id))->move($this->getDirectoryLocation($newID)) &&
filesystem()->directory(flextype('media')->folders()->meta()->getDirectoryMetaLocation($id))->move(flextype('media')->folders()->meta()->getDirectoryMetaLocation($newID));
}
return false;
}
/**
* Copy folder
*
* @param string $id Unique identifier of the folder.
* @param string $newID New Unique identifier of the folder.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function copy(string $id, string $newID): bool
{
if (
(filesystem()->directory($this->getDirectoryLocation($newID))->exists() === false &&
filesystem()->directory(flextype('media')->folders()->meta()->getDirectoryMetaLocation($newID))->exists() === false)
) {
filesystem()
->directory($this->getDirectoryLocation($id))
->copy($this->getDirectoryLocation($newID));
filesystem()
->directory(flextype('media')->folders()->meta()->getDirectoryMetaLocation($id))
->copy(flextype('media')->folders()->meta()->getDirectoryMetaLocation($newID));
return filesystem()->directory($this->getDirectoryLocation($newID))->exists() &&
filesystem()->directory(flextype('media')->folders()->meta()->getDirectoryMetaLocation($newID))->exists();
}
return false;
}
/**
* Delete folder
*
* @param string $id Unique identifier of the file.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function delete(string $id): bool
{
return filesystem()->directory($this->getDirectoryLocation($id))->delete() &&
filesystem()->directory(flextype('media')->folders()->meta()->getDirectoryMetaLocation($id))->delete();
}
/**
* Check whether a folder exists.
*
* @param string $id Unique identifier of the folder.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function has(string $id): bool
{
return filesystem()->directory($this->getDirectoryLocation($id))->exists() &&
filesystem()->directory(flextype('media')->folders()->meta()->getDirectoryMetaLocation($id))->exists();
}
/**
* Get files directory location
*
* @param string $id Unique identifier of the folder.
*
* @return string entry directory location
*
* @access public
*/
public function getDirectoryLocation(string $id): string
{
return PATH['project'] . '/media/' . $id;
}
}