40
common/Files/Actions/Deletion/DeleteEntries.php
Executable file
40
common/Files/Actions/Deletion/DeleteEntries.php
Executable file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Actions\Deletion;
|
||||
|
||||
use Arr;
|
||||
use Common\Files\FileEntry;
|
||||
use Gate;
|
||||
|
||||
class DeleteEntries
|
||||
{
|
||||
public function execute(array $params, $authorize = true): void
|
||||
{
|
||||
$entryIds =
|
||||
$params['entryIds'] ?? $this->idsFromPaths($params['paths']);
|
||||
|
||||
if (count($entryIds)) {
|
||||
if ($authorize) {
|
||||
Gate::authorize('destroy', [FileEntry::class, $entryIds]);
|
||||
}
|
||||
|
||||
if (Arr::get($params, 'soft')) {
|
||||
app(SoftDeleteEntries::class)->execute($entryIds);
|
||||
} else {
|
||||
app(PermanentlyDeleteEntries::class)->execute($entryIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function idsFromPaths(array $paths): array
|
||||
{
|
||||
$filenames = array_map(function ($path) {
|
||||
return basename($path);
|
||||
}, $paths);
|
||||
|
||||
return app(FileEntry::class)
|
||||
->whereIn('file_name', $filenames)
|
||||
->pluck('id')
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
61
common/Files/Actions/Deletion/PermanentlyDeleteEntries.php
Executable file
61
common/Files/Actions/Deletion/PermanentlyDeleteEntries.php
Executable file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Actions\Deletion;
|
||||
|
||||
use Common\Files\Events\FileEntriesDeleted;
|
||||
use Common\Files\FileEntry;
|
||||
use DB;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class PermanentlyDeleteEntries extends SoftDeleteEntries
|
||||
{
|
||||
/**
|
||||
* Permanently delete file entries, related records and files from disk.
|
||||
*/
|
||||
protected function delete(Collection|array $entries): void
|
||||
{
|
||||
$entries = $this->loadChildEntries($entries, true);
|
||||
$this->deleteFiles($entries);
|
||||
$this->deleteEntries($entries);
|
||||
event(new FileEntriesDeleted($entries->pluck('id')->toArray(), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete file entries from database.
|
||||
*/
|
||||
private function deleteEntries(Collection $entries): void
|
||||
{
|
||||
$entryIds = $entries->pluck('id');
|
||||
|
||||
// detach users
|
||||
DB::table('file_entry_models')
|
||||
->whereIn('file_entry_id', $entryIds)
|
||||
->delete();
|
||||
|
||||
// detach tags
|
||||
DB::table('taggables')
|
||||
->where('taggable_type', FileEntry::MODEL_TYPE)
|
||||
->whereIn('taggable_id', $entryIds)
|
||||
->delete();
|
||||
|
||||
$this->entry->whereIn('id', $entries->pluck('id'))->forceDelete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete files from disk.
|
||||
*/
|
||||
private function deleteFiles(Collection $entries): void
|
||||
{
|
||||
$entries
|
||||
->filter(function (FileEntry $entry) {
|
||||
return $entry->type !== 'folder';
|
||||
})
|
||||
->each(function (FileEntry $entry) {
|
||||
if ($entry->public) {
|
||||
$entry->getDisk()->delete($entry->getStoragePath());
|
||||
} else {
|
||||
$entry->getDisk()->deleteDirectory($entry->file_name);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
22
common/Files/Actions/Deletion/RestoreEntries.php
Executable file
22
common/Files/Actions/Deletion/RestoreEntries.php
Executable file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Actions\Deletion;
|
||||
|
||||
use Common\Files\Events\FileEntriesRestored;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class RestoreEntries extends SoftDeleteEntries
|
||||
{
|
||||
public function execute(Collection|array $entryIds): void
|
||||
{
|
||||
$entries = $this->entry
|
||||
->onlyTrashed()
|
||||
->whereIn('id', $entryIds)
|
||||
->get();
|
||||
$entries = $this->loadChildEntries($entries, true);
|
||||
|
||||
$this->entry->whereIn('id', $entries->pluck('id'))->restore();
|
||||
|
||||
event(new FileEntriesRestored($entries->pluck('id')->toArray()));
|
||||
}
|
||||
}
|
||||
40
common/Files/Actions/Deletion/SoftDeleteEntries.php
Executable file
40
common/Files/Actions/Deletion/SoftDeleteEntries.php
Executable file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Actions\Deletion;
|
||||
|
||||
use Common\Files\Events\FileEntriesDeleted;
|
||||
use Common\Files\FileEntry;
|
||||
use Common\Files\Traits\LoadsAllChildEntries;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class SoftDeleteEntries
|
||||
{
|
||||
use LoadsAllChildEntries;
|
||||
|
||||
public function __construct(protected FileEntry $entry)
|
||||
{
|
||||
}
|
||||
|
||||
public function execute(Collection|array $entryIds): void
|
||||
{
|
||||
collect($entryIds)
|
||||
->chunk(50)
|
||||
->each(function ($ids) {
|
||||
$entries = $this->entry
|
||||
->withTrashed()
|
||||
->whereIn('id', $ids)
|
||||
->get();
|
||||
$this->delete($entries);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Move specified entries to "trash".
|
||||
*/
|
||||
protected function delete(Collection|array $entries): void
|
||||
{
|
||||
$entries = $this->loadChildEntries($entries);
|
||||
$this->entry->whereIn('id', $entries->pluck('id'))->delete();
|
||||
event(new FileEntriesDeleted($entries->pluck('id')->toArray(), false));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user