MCPcopy Create free account
hub / github.com/error311/FileRise / resolveFolderPathForAdapter

Method resolveFolderPathForAdapter

src/FileRise/Domain/FileModel.php:187–231  ·  view source on GitHub ↗
(
        StorageAdapterInterface $storage,
        string $root,
        string $folder,
        bool $create = true
    )

Source from the content-addressed store, hash-verified

185 }
186
187 private static function resolveFolderPathForAdapter(
188 StorageAdapterInterface $storage,
189 string $root,
190 string $folder,
191 bool $create = true
192 ): array {
193 $folder = trim($folder) ?: 'root';
194
195 if (strtolower($folder) !== 'root' && !preg_match(REGEX_FOLDER_NAME, $folder)) {
196 return [null, "Invalid folder name."];
197 }
198
199 $isLocal = $storage->isLocal();
200 $base = $isLocal ? realpath($root) : rtrim($root, '/\\');
201 if ($base === false || $base === '') {
202 return [null, "Server misconfiguration."];
203 }
204
205 if (!$isLocal && strpos($folder, '..') !== false) {
206 return [null, "Invalid folder name."];
207 }
208
209 $dir = (strtolower($folder) === 'root')
210 ? $base
211 : $base . DIRECTORY_SEPARATOR . trim($folder, "/\\ ");
212
213 if ($create) {
214 $st = $storage->stat($dir);
215 if ($st === null || $st['type'] !== 'dir') {
216 if (!$storage->mkdir($dir, 0775, true)) {
217 return [null, "Cannot create destination folder"];
218 }
219 }
220 }
221
222 if ($isLocal) {
223 $real = realpath($dir);
224 if ($real === false || strpos($real, $base) !== 0) {
225 return [null, "Invalid folder path."];
226 }
227 return [$real, null];
228 }
229
230 return [$dir, null];
231 }
232
233 /**
234 * Copies files from a source folder to a destination folder, updating metadata if available.

Callers 2

Calls 3

isLocalMethod · 0.45
statMethod · 0.45
mkdirMethod · 0.45

Tested by

no test coverage detected