|
| 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