| 1: | <?php |
| 2: | |
| 3: | namespace LaravelUi5\Core\Controllers; |
| 4: | |
| 5: | use Illuminate\Routing\Controller; |
| 6: | use LaravelUi5\Core\Contracts\Ui5Context; |
| 7: | use LaravelUi5\Core\Exceptions\MissingAssetException; |
| 8: | use LaravelUi5\Core\Ui5\Contracts\HasAssetsInterface; |
| 9: | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | class AssetController extends Controller |
| 23: | { |
| 24: | public function __invoke( |
| 25: | Ui5Context $context, |
| 26: | string $type, |
| 27: | string $slug, |
| 28: | string $version, |
| 29: | string $file |
| 30: | ): BinaryFileResponse |
| 31: | { |
| 32: | |
| 33: | $artifact = $context->artifact; |
| 34: | |
| 35: | if ($artifact instanceof HasAssetsInterface) { |
| 36: | $path = $artifact->getAssetPath($file); |
| 37: | |
| 38: | if ($path && file_exists($path)) { |
| 39: | $mime = match (true) { |
| 40: | str_ends_with($file, '.js') => 'application/javascript', |
| 41: | str_ends_with($file, '.js.map') => 'application/json', |
| 42: | str_ends_with($file, '.json') => 'application/json', |
| 43: | str_ends_with($file, '.css') => 'text/css', |
| 44: | str_ends_with($file, '.properties') => 'text/plain; charset=utf-8', |
| 45: | default => 'application/octet-stream', |
| 46: | }; |
| 47: | |
| 48: | return response()->file($path, ['Content-Type' => $mime]); |
| 49: | } |
| 50: | } |
| 51: | |
| 52: | throw new MissingAssetException($type, $slug, $version, $file); |
| 53: | } |
| 54: | } |
| 55: | |