ItemPicturesController.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\UploadedFile;
  4. use Illuminate\Support\Facades\File;
  5. use Illuminate\Support\Facades\Response;
  6. use Illuminate\Support\Facades\Storage;
  7. class ItemPicturesController extends Controller
  8. {
  9. protected $visibility = 'public';
  10. protected $driver = 'images';
  11. protected $prefix = 'items';
  12. protected $defaultPath = 'items-default-image.jpg';
  13. public function show($pathAfter)
  14. {
  15. $path = Storage::disk($this->driver)->url($pathAfter);
  16. if (!File::exists($path)) {
  17. if ($pathAfter == 'avatar'){
  18. $path = Storage::disk($this->driver)->url('users/default-avatar.jpg');
  19. }else {
  20. $path = Storage::disk($this->driver)->url('items/items-default-image.jpg');
  21. }
  22. }
  23. $file = File::get($path);
  24. $type = File::mimeType($path);
  25. $response = Response::make($file, 200);
  26. $response->header("Content-Type", $type);
  27. return $response;
  28. }
  29. public function store(UploadedFile $file): string
  30. {
  31. return Storage::disk($this->driver)->put($this->prefix, $file);
  32. }
  33. /**
  34. * @param array $files from Input::file()
  35. * @return array
  36. */
  37. public function storeInputFiles(array $files): array
  38. {
  39. $pathToFiles = [];
  40. foreach ($files as $fileBag) {
  41. foreach ($fileBag as $file) {
  42. $pathToFiles[] = $this->store($file);
  43. }
  44. }
  45. return $pathToFiles;
  46. }
  47. }