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.
 
 
 
 

73 lines
2.8 KiB

<?php
namespace App\Http\Middleware;
use App\Models\Collection;
use App\Models\File;
use Closure;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class BindFileModelMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if (($request->route()->action['as'] == 'api.files.store') && is_null($request->file('file'))) {
$file = File::find($request->file);
if (!is_null($file)) {
$Collection = Collection::where('name', $request->route('collection_name'))->firstOrFail();
if (Storage::disk($Collection->disk)->exists($file->server_path . $file->uuid . '.' . $Collection->ext)) {
app()->bind('file', function () use ($file) {
return $file;
});
}
}
}
if ($request->route()->action['as'] == 'api.files.show') {
$file = File::findOrFail($request->route('uuid'));
$Collection = Collection::where('name', $request->route('collection_name'))->firstOrFail();
if (Storage::disk($Collection->disk)->exists($file->server_path . $file->uuid . '.' . $Collection->ext)) {
app()->bind('file', function () use ($file) {
return $file;
});
}else{
abort(404);
}
}
if ($request->route()->action['as'] == 'api.files.update') {
$file = File::findOrFail($request->route('uuid'));
$Collection = Collection::where('name', $request->route('collection_name'))->firstOrFail();
if (Storage::disk($Collection->disk)->exists($file->server_path . $file->uuid . '.' . $Collection->ext)) {
app()->bind('file', function () use ($file) {
return $file;
});
}else{
abort(404);
}
}
if ($request->route()->action['as'] == 'api.files.destroy') {
$file = File::findOrFail($request->route('uuid'));
$Collection = Collection::withTrashed()->where('name', $request->route('collection_name'))->firstOrFail();
if (Storage::disk($Collection->disk)->exists($file->server_path . $file->uuid . '.' . $Collection->ext)) {
app()->bind('file', function () use ($file) {
return $file;
});
}else{
abort(404);
}
}
return $next($request);
}
}