feat: initial project setup with Laravel 12 and Filament v4
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\OsurgInitial;
|
||||
use App\Models\SyncLog;
|
||||
use App\Services\SyncService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class SyncController extends Controller
|
||||
{
|
||||
protected function verifyToken(Request $request): bool
|
||||
{
|
||||
$token = OsurgInitial::val('sync_token', config('sync.token'));
|
||||
return $token !== '' && $request->header('X-Sync-Token') === $token;
|
||||
}
|
||||
|
||||
public function export(Request $request): JsonResponse
|
||||
{
|
||||
if (! $this->verifyToken($request)) {
|
||||
return response()->json(['error' => 'Unauthorized'], 401);
|
||||
}
|
||||
|
||||
$since = (int) $request->query('since', 0);
|
||||
|
||||
return response()->json([
|
||||
'last_sync_id' => SyncLog::lastSyncId(),
|
||||
'changes' => SyncLog::changesSince($since),
|
||||
]);
|
||||
}
|
||||
|
||||
public function apply(Request $request): JsonResponse
|
||||
{
|
||||
if (! $this->verifyToken($request)) {
|
||||
return response()->json(['error' => 'Unauthorized'], 401);
|
||||
}
|
||||
|
||||
set_time_limit(0);
|
||||
|
||||
$service = SyncService::fromConfig();
|
||||
$report = $service->applyChanges($request->input('changes', []));
|
||||
|
||||
return response()->json(['report' => $report]);
|
||||
}
|
||||
|
||||
public function downloadFile(Request $request): mixed
|
||||
{
|
||||
if (! $this->verifyToken($request)) {
|
||||
return response()->json(['error' => 'Unauthorized'], 401);
|
||||
}
|
||||
|
||||
$path = $request->query('path');
|
||||
if (! $path || ! Storage::disk('public')->exists($path)) {
|
||||
return response()->json(['error' => 'File not found'], 404);
|
||||
}
|
||||
|
||||
return Storage::disk('public')->download($path);
|
||||
}
|
||||
|
||||
public function receiveFile(Request $request): JsonResponse
|
||||
{
|
||||
if (! $this->verifyToken($request)) {
|
||||
return response()->json(['error' => 'Unauthorized'], 401);
|
||||
}
|
||||
|
||||
set_time_limit(0);
|
||||
|
||||
$path = $request->input('path');
|
||||
$file = $request->file('file');
|
||||
|
||||
if (! $path || ! $file) {
|
||||
return response()->json(['error' => 'Missing path or file'], 400);
|
||||
}
|
||||
|
||||
$path = ltrim(str_replace(['..', '\\'], ['', '/'], $path), '/');
|
||||
|
||||
Storage::disk('public')->put($path, file_get_contents($file->getRealPath()));
|
||||
|
||||
return response()->json(['success' => true, 'path' => $path]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user