mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-05 18:23:48 +00:00
- added it_delete_zips_older_than_one_day test
- refactored scheduler tasks into SchedulerService.php
This commit is contained in:
@@ -3,9 +3,7 @@
|
||||
namespace App\Console;
|
||||
|
||||
use App\Console\Commands\SetupDevEnvironment;
|
||||
use App\Share;
|
||||
use App\Zip;
|
||||
use Carbon\Carbon;
|
||||
use App\Services\SchedulerService;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
@@ -28,12 +26,14 @@ class Kernel extends ConsoleKernel
|
||||
*/
|
||||
protected function schedule(Schedule $schedule)
|
||||
{
|
||||
$schedule->call(function () {
|
||||
$this->delete_expired_shared_links();
|
||||
$scheduler = app()->make(SchedulerService::class);
|
||||
|
||||
$schedule->call(function () use ($scheduler) {
|
||||
$scheduler->delete_expired_shared_links();
|
||||
})->everyMinute();
|
||||
|
||||
$schedule->call(function () {
|
||||
$this->delete_old_zips();
|
||||
$schedule->call(function () use ($scheduler) {
|
||||
$scheduler->delete_old_zips();
|
||||
})->everySixHours();
|
||||
|
||||
// Run queue jobs every minute
|
||||
@@ -53,42 +53,4 @@ class Kernel extends ConsoleKernel
|
||||
|
||||
require base_path('routes/console.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete old zips
|
||||
*/
|
||||
protected function delete_old_zips(): void
|
||||
{
|
||||
// Get all zips
|
||||
$zips = Zip::where('created_at', '<=', Carbon::now()->subDay()->toDateTimeString())->get();
|
||||
|
||||
$zips->each(function ($zip) {
|
||||
|
||||
// Delete zip file
|
||||
\Storage::disk('local')->delete('zip/' . $zip->basename);
|
||||
|
||||
// Delete zip record
|
||||
$zip->delete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and delete expired shared links
|
||||
*/
|
||||
protected function delete_expired_shared_links(): void
|
||||
{
|
||||
// Get all shares with expiration time
|
||||
$shares = Share::whereNotNull('expire_in')->get();
|
||||
|
||||
$shares->each(function ($share) {
|
||||
|
||||
// Get dates
|
||||
$created_at = Carbon::parse($share->created_at);
|
||||
|
||||
// If time was over, then delete share record
|
||||
if ($created_at->diffInHours(Carbon::now()) >= $share->expire_in) {
|
||||
$share->delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
48
app/Services/SchedulerService.php
Normal file
48
app/Services/SchedulerService.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
|
||||
use App\Models\Share;
|
||||
use App\Models\Zip;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class SchedulerService
|
||||
{
|
||||
/**
|
||||
* Delete old zips
|
||||
*/
|
||||
public function delete_old_zips(): void
|
||||
{
|
||||
Zip::where('created_at', '<=', Carbon::now()->subDay()->toDateTimeString())
|
||||
->get()
|
||||
->each(function ($zip) {
|
||||
|
||||
// Delete zip file
|
||||
\Storage::disk('local')->delete("zip/$zip->basename");
|
||||
|
||||
// Delete zip record
|
||||
$zip->delete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and delete expired shared links
|
||||
*/
|
||||
public function delete_expired_shared_links(): void
|
||||
{
|
||||
Share::whereNotNull('expire_in')
|
||||
->get()
|
||||
->each(function ($share) {
|
||||
|
||||
// Get dates
|
||||
$created_at = Carbon::parse($share->created_at);
|
||||
|
||||
// If time was over, then delete share record
|
||||
if ($created_at->diffInHours(Carbon::now()) >= $share->expire_in) {
|
||||
$share->delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,9 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "app/"
|
||||
"App\\": "app/",
|
||||
"Database\\Factories\\": "database/factories/",
|
||||
"Database\\Seeders\\": "database/seeders/"
|
||||
},
|
||||
"classmap": [
|
||||
"database/seeds",
|
||||
|
||||
Reference in New Issue
Block a user