cms/app/Http/Controllers/Admin/Themes/ThemeEditorFileReadController.php

45 lines
1.4 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Controllers\Admin\Themes;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Themes\ThemeFileReadRequest;
use App\Services\ThemeEditorService;
use Illuminate\Support\Facades\File;
use Exception;
/**
* Controller for reading the content of a theme file.
*/
class ThemeEditorFileReadController extends Controller
{
/**
* Handle the file read request.
*
* @param \App\Http\Requests\Admin\Themes\ThemeFileReadRequest $request
* @param \App\Services\ThemeEditorService $editorService
* @return \Illuminate\Http\JsonResponse
*/
public function __invoke(ThemeFileReadRequest $request, ThemeEditorService $editorService)
{
$theme = $request->query('theme');
$path = $request->query('path');
try {
$content = $editorService->readFile($theme, $path);
// We need the full path to get the extension for the response
$basePath = realpath(base_path('themes/' . $theme));
$fullPath = realpath($basePath . '/' . $path);
return response()->json([
'content' => $content,
'extension' => File::extension($fullPath)
]);
} catch (Exception $e) {
$statusCode = str_contains($e->getMessage(), 'Unauthorized') ? 403 : 404;
return response()->json(['error' => $e->getMessage()], $statusCode);
}
}
}