Skip to content

Commit 7cee8b3

Browse files
author
Martin Kluska
committed
added scheduler + config file
1 parent bccc87d commit 7cee8b3

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

config/chunk-upload.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
return [
3+
4+
/**
5+
* The storage config
6+
*/
7+
"storage" => [
8+
/**
9+
* Returns the folder name of the chunks. The location is in storage/app/{folder_name}
10+
*/
11+
"chunks" => "chunks",
12+
"disk" => "local"
13+
],
14+
"clear" => [
15+
/**
16+
* How old chunks we should delete
17+
*/
18+
"timestamp" => "-1 HOUR",
19+
"schedule" => [
20+
"enabled" => true,
21+
"cron" => "0 */1 * * * *" // run every hour
22+
]
23+
]
24+
];

src/Config/AbstractConfig.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ abstract public function chunksStorageDirectory();
3333
* @return string
3434
*/
3535
abstract public function clearTimestampString();
36+
37+
/**
38+
* Returns the shedule config array
39+
* @return array<enable,cron>
40+
*/
41+
abstract public function scheduleConfig();
3642
}

src/Config/FileConfig.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class FileConfig extends AbstractConfig
1717

1818
/**
1919
* Returns the disk name to use for the chunk storage
20-
*
20+
*
2121
* @return string
2222
*/
2323
public function chunksDiskName()
@@ -50,6 +50,14 @@ public function clearTimestampString()
5050
return $this->get("clear.timestamp");
5151
}
5252

53+
/**
54+
* Returns the shedule config array
55+
* @return array<enable,cron>
56+
*/
57+
public function scheduleConfig() {
58+
return $this->get("clear.schedule");
59+
}
60+
5361
/**
5462
* Returns a chunks config value
5563
*

src/Providers/ChunkUploadServiceProvider.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace Pion\Laravel\ChunkUpload\Providers;
33

4+
use Illuminate\Console\Scheduling\Schedule;
45
use Illuminate\Foundation\Application;
6+
use Illuminate\Support\Arr;
57
use Illuminate\Support\Facades\Storage;
68
use Illuminate\Support\ServiceProvider;
79
use Pion\Laravel\ChunkUpload\Commands\ClearChunksCommand;
@@ -11,6 +13,31 @@
1113

1214
class ChunkUploadServiceProvider extends ServiceProvider
1315
{
16+
17+
/**
18+
* When the service is beeing booted
19+
*/
20+
public function boot()
21+
{
22+
// get the schedule config
23+
$schedule = AbstractConfig::config()->scheduleConfig();
24+
25+
// run only if schedule is enabled
26+
if (Arr::get($schedule, "enabled", false) === true) {
27+
28+
// wait until the app is fully booted
29+
$this->app->booted(function () use ($schedule) {
30+
// get the sheduler
31+
/** @var Schedule $schedule */
32+
$schedule = $this->app->make(Schedule::class);
33+
34+
// register the clear chunks with custom schedule
35+
$schedule->command('uploads:clear')->cron(Arr::get($schedule, "cron", "* * * * *"));
36+
});
37+
}
38+
}
39+
40+
1441
/**
1542
* Register the package requirements.
1643
*

0 commit comments

Comments
 (0)