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

Method deleteTrashFiles

src/FileRise/Domain/FileModel.php:3334–3399  ·  view source on GitHub ↗

* Deletes trash items based on an array of trash file identifiers. * * @param array $filesToDelete An array of trash file names (identifiers). * @return array An associative array containing "deleted" (array of deleted items) and optionally "error" (error message). */

(array $filesToDelete)

Source from the content-addressed store, hash-verified

3332 * @return array An associative array containing "deleted" (array of deleted items) and optionally "error" (error message).
3333 */
3334 public static function deleteTrashFiles(array $filesToDelete)
3335 {
3336 $storage = self::storage();
3337 // Setup trash directory and metadata file.
3338 $trashDir = rtrim(self::trashRoot(), '/\\') . DIRECTORY_SEPARATOR;
3339 if ($storage->stat($trashDir) === null) {
3340 $storage->mkdir($trashDir, 0755, true);
3341 }
3342 $trashMetadataFile = $trashDir . "trash.json";
3343
3344 // Load trash metadata into an associative array keyed by trashName.
3345 $trashData = [];
3346 $trashJson = $storage->read($trashMetadataFile);
3347 if ($trashJson !== false) {
3348 $tempData = json_decode($trashJson, true);
3349 if (is_array($tempData)) {
3350 foreach ($tempData as $item) {
3351 if (isset($item['trashName'])) {
3352 $trashData[$item['trashName']] = $item;
3353 }
3354 }
3355 }
3356 }
3357
3358 $deletedFiles = [];
3359 $errors = [];
3360
3361 // Define a safe file name pattern.
3362 $safeFileNamePattern = REGEX_FILE_NAME;
3363
3364 // Process each file identifier in the $filesToDelete array.
3365 foreach ($filesToDelete as $trashName) {
3366 $trashName = trim($trashName);
3367 if (!preg_match($safeFileNamePattern, $trashName)) {
3368 $errors[] = "$trashName has an invalid format.";
3369 continue;
3370 }
3371 if (!isset($trashData[$trashName])) {
3372 $errors[] = "Trash item $trashName not found.";
3373 continue;
3374 }
3375 // Build the full path to the trash file.
3376 $filePath = $trashDir . $trashName;
3377 if ($storage->stat($filePath) !== null) {
3378 if ($storage->delete($filePath)) {
3379 $deletedFiles[] = $trashName;
3380 unset($trashData[$trashName]);
3381 } else {
3382 $errors[] = "Failed to delete $trashName.";
3383 }
3384 } else {
3385 // If the file doesn't exist, remove its metadata.
3386 unset($trashData[$trashName]);
3387 $deletedFiles[] = $trashName;
3388 }
3389 }
3390
3391 // Save the updated trash metadata back as an indexed array.

Callers 1

Calls 7

storageMethod · 0.95
trashRootMethod · 0.95
statMethod · 0.45
mkdirMethod · 0.45
readMethod · 0.45
deleteMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected