You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  4. use Illuminate\Foundation\Bus\DispatchesJobs;
  5. use Illuminate\Foundation\Validation\ValidatesRequests;
  6. use Illuminate\Routing\Controller as BaseController;
  7. use App\Models\Bucket;
  8. class BucketController extends BaseController
  9. {
  10. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  11. private function formatBytes($size, $precision = 1)
  12. {
  13. if ($size === 0) {
  14. return '0 bytes';
  15. }
  16. $base = log($size, 1024);
  17. $suffixes = array('bytes', 'kB', 'MB', 'G', 'T');
  18. return round(pow(1024, $base - floor($base)), $precision).' '.$suffixes[floor($base)];
  19. }
  20. /**
  21. *
  22. *
  23. * @return
  24. *
  25. */
  26. public function single($id)
  27. {
  28. $results = [];
  29. // create bucket
  30. $bucket = Bucket::find($id);
  31. $files = scandir($bucket->path);
  32. foreach($files as $file) {
  33. if ($file === '.' || $file === '..') {
  34. continue;
  35. }
  36. $meta = stat($bucket->path.'/'.$file);
  37. $size = ($meta['size'] / 1000);
  38. if (($meta['size'] / 1000) < 1000) {
  39. $size = ($meta['size'] / 1000);
  40. }
  41. $results[] = [
  42. 'filename' => $file,
  43. 'is_file' => is_file($bucket->path.'/'.$file),
  44. 'meta' => [
  45. 'size' => $this->formatBytes($meta['size']),
  46. 'updated_at' => \Carbon\Carbon::parse($meta['mtime'])->format('d.m.Y h:m:i')
  47. ]
  48. ];
  49. }
  50. return view('bucket.single', [
  51. 'bucket' => $bucket,
  52. 'files' => $results
  53. ]);
  54. }
  55. /**
  56. *
  57. *
  58. * @return [type] [description]
  59. *
  60. */
  61. public function create()
  62. {
  63. return view('bucket.create');
  64. }
  65. /**
  66. *
  67. *
  68. */
  69. public function store()
  70. {
  71. $validated = request()->validate([
  72. 'name' => 'required|max:255',
  73. 'description' => 'max:255',
  74. 'path' => 'present',
  75. 'is_public' => 'boolean',
  76. ]);
  77. if ($validated) {
  78. // create bucket
  79. $bucket = Bucket::create($validated);
  80. return redirect()
  81. ->route('bucket.single', [ 'id' => $bucket->id ]);
  82. } else {
  83. return back()
  84. ->withInput();
  85. }
  86. }
  87. }