/** * Permanently delete given folders and associated files from db and disk. * * @param array|Collection $folders * @return int */ public function deleteFolders($folders) { if (!count($folders)) { return 0; } foreach ($folders as $folder) { //if folder is array it's a model instance, otherwise we will need to fetch it from db if (is_array($folder)) { $folder = Folder::with('files', 'folders')->withTrashed()->findOrFail($folder['id']); } if ($folder->user_id === Auth::user()->id || Auth::user()->isAdmin) { //delete all files foreach ($folder->files as $file) { Storage::deleteDirectory($file->getRelativePath(true)); } //delete all file models from db File::whereIn('id', $folder->files->lists('id'))->forceDelete(); //delete sub-folders and files recursively if (!$folder->folders->isEmpty()) { $this->deleteFolders($folder->folders); } //delete folder model from db $folder->forceDelete(); } } return count($folders); }
/** * Add given folder and its files to downloads array. * * @param int|string|Folder $folder * @param array $downloads * @return array */ private function addFolder($folder, &$downloads) { if (!is_object($folder)) { $folder = Folder::with('files', 'folders')->find($folder); } $downloads[$folder->name] = []; if (!$folder->files->isEmpty()) { foreach ($folder->files as $file) { $downloads[$folder->name][$this->getName($file)] = $file->getAbsolutePath(); } } if (!$folder->folders->isEmpty()) { foreach ($folder->folders as $subFolder) { $this->addFolder($subFolder, $downloads[$folder->name]); } } return $downloads; }