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.

75 lines
2.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Models\Collection;
  4. use App\Models\File;
  5. use Closure;
  6. use Illuminate\Support\Str;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Storage;
  9. class BindFileModelMiddleware
  10. {
  11. /**
  12. * Handle an incoming request.
  13. *
  14. * @param \Illuminate\Http\Request $request
  15. * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
  16. * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
  17. */
  18. public function handle(Request $request, Closure $next)
  19. {
  20. if (($request->route()->action['as'] == 'api.files.store') && is_null($request->file('file'))) {
  21. $file = File::find($request->file);
  22. if (!is_null($file)) {
  23. $Collection = Collection::where('name', $request->route('collection_name'))->firstOrFail();
  24. if (Storage::disk($Collection->disk)->exists($file->server_path . $file->uuid . '.' . $Collection->ext)) {
  25. app()->bind('file', function () use ($file) {
  26. return $file;
  27. });
  28. }
  29. }
  30. }
  31. if ($request->route()->action['as'] == 'api.files.show') {
  32. $file = File::findOrFail($request->route('uuid'));
  33. $Collection = Collection::where('name', $request->route('collection_name'))->firstOrFail();
  34. if (Storage::disk($Collection->disk)->exists($file->server_path . $file->uuid . '.' . $Collection->ext)) {
  35. app()->bind('file', function () use ($file) {
  36. return $file;
  37. });
  38. }else{
  39. abort(404);
  40. }
  41. }
  42. if ($request->route()->action['as'] == 'api.files.update') {
  43. $file = File::findOrFail($request->route('uuid'));
  44. $Collection = Collection::where('name', $request->route('collection_name'))->firstOrFail();
  45. if (Storage::disk($Collection->disk)->exists($file->server_path . $file->uuid . '.' . $Collection->ext)) {
  46. app()->bind('file', function () use ($file) {
  47. return $file;
  48. });
  49. }else{
  50. abort(404);
  51. }
  52. }
  53. if ($request->route()->action['as'] == 'api.files.destroy') {
  54. $file = File::withTrashed()->findOrFail($request->route('uuid'));
  55. $Collection = Collection::withTrashed()->where('name', $request->route('collection_name'))->firstOrFail();
  56. if (Storage::disk($Collection->disk)->exists($file->server_path . $file->uuid . '.' . $Collection->ext)) {
  57. app()->bind('file', function () use ($file) {
  58. return $file;
  59. });
  60. }else{
  61. abort(404);
  62. }
  63. }
  64. return $next($request);
  65. }
  66. }