Skip to content

Commit c70bc54

Browse files
authored
Fix cleanup action not working on directories (#3664)
1 parent 7c4de04 commit c70bc54

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

app/Assets/Helpers.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
use App\Exceptions\Internal\ZeroModuloException;
1212
use Exception;
1313
use Illuminate\Http\Request;
14+
use Safe\Exceptions\FilesystemException;
1415
use Safe\Exceptions\InfoException;
1516
use function Safe\ini_get;
17+
use function Safe\rmdir;
18+
use function Safe\unlink;
1619

1720
class Helpers
1821
{
@@ -263,4 +266,31 @@ public function getUriWithQueryString(Request $request): string
263266

264267
return $request->path() . '?' . http_build_query($query);
265268
}
269+
270+
/**
271+
* Actually remove the directory recursively.
272+
*
273+
* @param string $dir
274+
*
275+
* @return void
276+
*
277+
* @throws FilesystemException
278+
*/
279+
public function remove_dir(string $dir): void
280+
{
281+
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
282+
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
283+
foreach ($files as $file) {
284+
/** @var \SplFileInfo $file */
285+
if ($file->isLink() || $file->isFile()) {
286+
// Remove files and symlinks without following them
287+
unlink($file->getPathname());
288+
continue;
289+
}
290+
if ($file->isDir()) {
291+
rmdir($file->getPathname());
292+
}
293+
}
294+
rmdir($dir);
295+
}
266296
}

app/Facades/Helpers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @method static string decimalToDegreeMinutesSeconds(float $decimal, bool $type)
2929
* @method static string censor(string $string, float $percentOfClear = 0.5)
3030
* @method static string getUriWithQueryString(\Illuminate\Http\Request $request): string
31+
* @method static void remove_dir(string $dir): void
3132
*/
3233
class Helpers extends Facade
3334
{

app/Http/Controllers/Admin/Maintenance/Cleaning.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
namespace App\Http\Controllers\Admin\Maintenance;
1010

11+
use App\Facades\Helpers;
1112
use App\Http\Requests\Maintenance\CleaningRequest;
1213
use App\Http\Resources\Diagnostics\CleaningState;
1314
use Illuminate\Routing\Controller;
1415
use Illuminate\Support\Facades\Log;
15-
use function Safe\rmdir;
1616
use function Safe\unlink;
1717

1818
/**
@@ -46,7 +46,7 @@ public function do(CleaningRequest $request): array
4646
$results[] = sprintf(__('maintenance.cleaning.result'), $file_info->getFilename());
4747

4848
if ($file_info->isDir()) {
49-
rmdir($file_info->getRealPath());
49+
Helpers::remove_dir($file_info->getRealPath());
5050
continue;
5151
}
5252
unlink($file_info->getRealPath());

app/Jobs/CleanUpExtraction.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
namespace App\Jobs;
1010

1111
use App\Enum\JobStatus;
12+
use App\Facades\Helpers;
1213
use App\Models\JobHistory;
1314
use Illuminate\Bus\Queueable;
1415
use Illuminate\Contracts\Queue\ShouldQueue;
1516
use Illuminate\Foundation\Bus\Dispatchable;
1617
use Illuminate\Queue\InteractsWithQueue;
1718
use Illuminate\Queue\SerializesModels;
1819
use Illuminate\Support\Str;
19-
use Safe\Exceptions\FilesystemException;
20-
use function Safe\rmdir;
21-
use function Safe\unlink;
2220

2321
class CleanUpExtraction implements ShouldQueue
2422
{
@@ -57,7 +55,7 @@ public function handle(): void
5755
// Check if all the sub directories are empty.
5856
if ($this->is_empty($this->folder_path)) {
5957
// Only clear the directory if it is empty.
60-
$this->remove_dir($this->folder_path);
58+
Helpers::remove_dir($this->folder_path);
6159
$this->history->status = JobStatus::SUCCESS;
6260
$this->history->save();
6361

@@ -82,27 +80,4 @@ private function is_empty(string $dir): bool
8280

8381
return true;
8482
}
85-
86-
/**
87-
* Actually remove the directory recursively.
88-
*
89-
* @param string $dir
90-
*
91-
* @return void
92-
*
93-
* @throws FilesystemException
94-
*/
95-
private function remove_dir(string $dir): void
96-
{
97-
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
98-
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
99-
foreach ($files as $file) {
100-
if ($file->isDir()) {
101-
rmdir($file->getPathname());
102-
} else {
103-
unlink($file->getPathname());
104-
}
105-
}
106-
rmdir($dir);
107-
}
10883
}

0 commit comments

Comments
 (0)