<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
|
|
use App\Models\Bucket;
|
|
|
|
class BucketController extends BaseController
|
|
{
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
|
|
|
private function formatBytes($size, $precision = 1)
|
|
{
|
|
if ($size === 0) {
|
|
return '0 bytes';
|
|
}
|
|
|
|
$base = log($size, 1024);
|
|
$suffixes = array('bytes', 'kB', 'MB', 'G', 'T');
|
|
|
|
return round(pow(1024, $base - floor($base)), $precision).' '.$suffixes[floor($base)];
|
|
}
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @return
|
|
*
|
|
*/
|
|
public function single($id)
|
|
{
|
|
$results = [];
|
|
|
|
// create bucket
|
|
$bucket = Bucket::find($id);
|
|
|
|
$files = scandir($bucket->path);
|
|
|
|
foreach($files as $file) {
|
|
|
|
if ($file === '.' || $file === '..') {
|
|
continue;
|
|
}
|
|
|
|
$meta = stat($bucket->path.'/'.$file);
|
|
|
|
$size = ($meta['size'] / 1000);
|
|
|
|
if (($meta['size'] / 1000) < 1000) {
|
|
$size = ($meta['size'] / 1000);
|
|
}
|
|
|
|
$results[] = [
|
|
'filename' => $file,
|
|
'is_file' => is_file($bucket->path.'/'.$file),
|
|
'meta' => [
|
|
'size' => $this->formatBytes($meta['size']),
|
|
'updated_at' => \Carbon\Carbon::parse($meta['mtime'])->format('d.m.Y h:m:i')
|
|
]
|
|
];
|
|
|
|
}
|
|
|
|
return view('bucket.single', [
|
|
'bucket' => $bucket,
|
|
'files' => $results
|
|
]);
|
|
}
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @return [type] [description]
|
|
*
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('bucket.create');
|
|
}
|
|
|
|
/**
|
|
*
|
|
*
|
|
*/
|
|
public function store()
|
|
{
|
|
$validated = request()->validate([
|
|
'name' => 'required|max:255',
|
|
'description' => 'max:255',
|
|
'path' => 'present',
|
|
'is_public' => 'boolean',
|
|
]);
|
|
|
|
if ($validated) {
|
|
|
|
// create bucket
|
|
$bucket = Bucket::create($validated);
|
|
|
|
return redirect()
|
|
->route('bucket.single', [ 'id' => $bucket->id ]);
|
|
} else {
|
|
return back()
|
|
->withInput();
|
|
}
|
|
}
|
|
}
|