Skip to content

Commit a11b260

Browse files
author
steve-ks
committed
Commit working state
* Added localization * Added storage and upload cleanup commands * Added generate storage lifetime value storage
1 parent ae313bd commit a11b260

33 files changed

+7189
-771
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CACHE_DRIVER=file
1010
QUEUE_CONNECTION=sync
1111
SESSION_DRIVER=file
1212
SESSION_LIFETIME=300
13+
MAX_FILESIZE_MB=1024
1314

1415
MAIL_MAILER=smtp
1516
MAIL_HOST=smtp.mailtrap.io
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Traits\StorageTime;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\Storage;
8+
9+
class StorageCleanup extends Command
10+
{
11+
use StorageTime;
12+
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'storage:cleanup
19+
{--f|force : Force removal of all directories, regardless of storage lifetime}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Cleanup all old storage directories';
27+
28+
/**
29+
* Create a new command instance.
30+
*
31+
* @return void
32+
*/
33+
public function __construct()
34+
{
35+
parent::__construct();
36+
}
37+
38+
/**
39+
* Execute the console command.
40+
*
41+
* @return mixed
42+
*/
43+
public function handle()
44+
{
45+
// Current timestamp in seconds
46+
$now = now();
47+
48+
// Force option
49+
$force = $this->option('force');
50+
51+
// Get paths and remove
52+
$paths = $force ? Storage::disk('public')->directories() : $this->allOutdatedStorages();
53+
$this->info(print_r($paths, true));
54+
foreach ($paths as $path)
55+
{
56+
if (Storage::disk('public')->exists($path))
57+
Storage::disk('public')->deleteDirectory($path);
58+
$this->forgetStorageTime($path);
59+
}
60+
if ($force)
61+
$this->wipeStorageTimes();
62+
else
63+
$this->clearOutdatedStorages();
64+
}
65+
}
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Storage;
7+
8+
class UploadCleanup extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'upload:cleanup
16+
{olderThan=3 : Remove upload directories older than x hours.}
17+
{--f|force : Force removal of all directories, regardless of last modified time}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Cleanup all old upload directories';
25+
26+
/**
27+
* Create a new command instance.
28+
*
29+
* @return void
30+
*/
31+
public function __construct()
32+
{
33+
parent::__construct();
34+
}
35+
36+
/**
37+
* Execute the console command.
38+
*
39+
* @return mixed
40+
*/
41+
public function handle()
42+
{
43+
// Get older-than argument in seconds
44+
$olderThan = intval($this->argument('olderThan'));
45+
if (!$olderThan) $olderThan = 3;
46+
$olderThan *= 3600;
47+
// Current timestamp in seconds
48+
$now = time();
49+
// Force option
50+
$force = $this->option('force');
51+
52+
foreach (Storage::disk('upload')->directories() as $dir)
53+
{
54+
// Delete all that is older than 3 hours (or force deletion)
55+
if (!!$force || ($now - Storage::disk('upload')->lastModified($dir) > $olderThan))
56+
{
57+
Storage::disk('upload')->deleteDirectory($dir);
58+
}
59+
}
60+
}
61+
}

app/Console/Kernel.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ class Kernel extends ConsoleKernel
1919
/**
2020
* Define the application's command schedule.
2121
*
22-
* @param \Illuminate\Console\Scheduling\Schedule $schedule
22+
* @param \Illuminate\Console\Scheduling\Schedule $schedule
2323
* @return void
2424
*/
2525
protected function schedule(Schedule $schedule)
2626
{
27-
// $schedule->command('inspire')->hourly();
27+
// Cleanup old upload directories
28+
$schedule->command('upload:cleanup')->hourly();
29+
// Cleanup old storage directories
30+
$schedule->command('storage:cleanup')->hourly();
2831
}
2932

3033
/**

app/Http/Controllers/SessionController.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Http\Controllers;
44

55
use Illuminate\Http\Request;
6-
use Illuminate\Support\Facades\Auth;
76
use Illuminate\Support\Facades\Storage;
87

98
class SessionController extends Controller
@@ -25,11 +24,10 @@ public function login(Request $request)
2524
if ($request->input('email_456'))
2625
{
2726
// Honeypot is filled
28-
sleep(1); // Throttle for brute-force protection
2927
$request->session()->put('authenticated', null);
3028
return view('login', [
3129
'status' => 'error',
32-
'message' => 'Service derzeit nicht verfügbar, bitte probieren Sie es später erneut.'
30+
'message' => __('messages.honeypot.alert.nice')
3331
]);
3432
}
3533

@@ -39,13 +37,13 @@ public function login(Request $request)
3937
$this->clearUploadDirectory();
4038
// Do not throttle successful login attempts
4139
$request->session()->put('authenticated', time());
40+
$request->session()->put('sessionid', md5(uniqid()));
4241
return redirect()->route('home');
4342
}
4443

45-
sleep(1); // Throttle for brute-force protection
4644
return view('login', [
4745
'status' => 'error',
48-
'message' => 'Falscher Sicherheitscode.'
46+
'message' => __('messages.token.alert')
4947
]);
5048
}
5149

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Traits\StringAdditions;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\File;
8+
use Illuminate\Support\Facades\Storage;
9+
10+
class StorageController extends Controller
11+
{
12+
use StringAdditions;
13+
14+
// Static font-awesome mime icons
15+
private $faMimeIcons = [
16+
'archive' => 'file-archive',
17+
'audio' => 'file-audio',
18+
'code' => 'file-code',
19+
'csv' => 'file-csv',
20+
'excel' => 'file-excel',
21+
'file' => 'file',
22+
'folder' => 'folder',
23+
'pdf' => 'file-pdf',
24+
'powerpoint' => 'file-powerpoint',
25+
'text' => 'file-alt',
26+
'video' => 'file-video',
27+
'word' => 'file-word',
28+
29+
'unknown' => 'file',
30+
];
31+
32+
private function selectMimeIcon($mime)
33+
{
34+
if ($this->startsWith($mime, 'text'))
35+
{
36+
switch ($mime)
37+
{
38+
case 'text/css':
39+
case 'text/html':
40+
case 'text/javascript':
41+
return $this->faMimeIcons['code'];
42+
case 'text/csv':
43+
return $this->faMimeIcons['csv'];
44+
case 'text/plain':
45+
return $this->faMimeIcons['text'];
46+
default:
47+
return $this->faMimeIcons['unknown'];
48+
}
49+
}
50+
51+
if ($this->startsWith($mime, 'application'))
52+
{
53+
if ($this->contains($mime, 'word') || $this->contains($mime, 'text'))
54+
return $this->faMimeIcons['word'];
55+
if ($this->contains($mime, 'pdf'))
56+
return $this->faMimeIcons['pdf'];
57+
if ($this->contains($mime, 'excel') || $this->contains($mime, 'spreadsheet'))
58+
return $this->faMimeIcons['excel'];
59+
if ($this->contains($mime, 'zip') || $this->contains($mime, 'x-tar') || $this->contains($mime, 'x-bzip') || $this->contains($mime, 'vnd.rar') || $this->contains($mime, 'archive'))
60+
return $this->faMimeIcons['archive'];
61+
if ($this->contains($mime, 'powerpoint') || $this->contains($mime, 'presentation'))
62+
return $this->faMimeIcons['powerpoint'];
63+
}
64+
65+
if ($this->startsWith($mime, 'audio'))
66+
return $this->faMimeIcons['audio'];
67+
68+
if ($this->startsWith($mime, 'video'))
69+
return $this->faMimeIcons['video'];
70+
}
71+
72+
public function index(String $dir)
73+
{
74+
$files = [];
75+
$thumbs = [];
76+
77+
foreach (Storage::disk('public')->files($dir."/thumbs") as $thumb)
78+
{
79+
array_push($thumbs, File::basename($thumb));
80+
}
81+
82+
foreach (Storage::disk('public')->files($dir) as $file)
83+
{
84+
$basename = File::basename($file);
85+
$image = in_array($basename, $thumbs) ?
86+
Storage::disk('public')->url(File::dirname($file) . "/thumbs/" . File::basename($file)) :
87+
null;
88+
89+
array_push($files, [
90+
'basename' => $basename,
91+
'image' => $image,
92+
'icon' => $image == null ? $this->selectMimeIcon(Storage::disk('public')->mimeType($file)) : null,
93+
'url' => Storage::disk('public')->url($file),
94+
'mime' => Storage::disk('public')->mimeType($file)
95+
]);
96+
}
97+
98+
99+
return view('storage')->with([
100+
'directory' => $dir,
101+
'files' => $files
102+
]);
103+
}
104+
105+
}

0 commit comments

Comments
 (0)