Initial commit
This commit is contained in:
103
api/app/Http/Controllers/Admin/GoogleDriveController.php
Normal file
103
api/app/Http/Controllers/Admin/GoogleDriveController.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\GoogleDrive\GoogleDriveService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class GoogleDriveController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected GoogleDriveService $googleDrive
|
||||
) {}
|
||||
|
||||
public function authUrl(): JsonResponse
|
||||
{
|
||||
$url = $this->googleDrive->getAuthUrl();
|
||||
|
||||
return response()->json(['url' => $url]);
|
||||
}
|
||||
|
||||
public function callback(Request $request): RedirectResponse
|
||||
{
|
||||
$frontendUrl = config('app.frontend_admin_url', 'http://localhost:5173');
|
||||
|
||||
$code = $request->query('code');
|
||||
if (! $code) {
|
||||
return redirect($frontendUrl.'?google_drive_error=missing_code');
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
if (! $user) {
|
||||
return redirect($frontendUrl.'?google_drive_error=not_authenticated');
|
||||
}
|
||||
|
||||
try {
|
||||
$this->googleDrive->handleCallback($code, $user);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Google Drive connection failed', [
|
||||
'error' => $e->getMessage(),
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
return redirect($frontendUrl.'?google_drive_error=connection_failed');
|
||||
}
|
||||
|
||||
return redirect($frontendUrl.'?google_drive_connected=1');
|
||||
}
|
||||
|
||||
public function status(): JsonResponse
|
||||
{
|
||||
$connection = Auth::user()?->googleDriveConnections()->first();
|
||||
|
||||
return response()->json([
|
||||
'connected' => (bool) $connection,
|
||||
'account_email' => $connection?->account_email,
|
||||
]);
|
||||
}
|
||||
|
||||
public function disconnect(): JsonResponse
|
||||
{
|
||||
Auth::user()?->googleDriveConnections()->delete();
|
||||
|
||||
return response()->json(['message' => 'Disconnected']);
|
||||
}
|
||||
|
||||
public function sharedDrives(): JsonResponse
|
||||
{
|
||||
$drives = $this->googleDrive->listSharedDrives(Auth::user());
|
||||
|
||||
return response()->json(['data' => $drives]);
|
||||
}
|
||||
|
||||
public function folders(Request $request): JsonResponse
|
||||
{
|
||||
$parentId = $request->query('parent_id');
|
||||
$driveId = $request->query('drive_id');
|
||||
$folders = $this->googleDrive->listFolders(Auth::user(), $parentId, $driveId);
|
||||
|
||||
return response()->json(['data' => $folders]);
|
||||
}
|
||||
|
||||
public function createFolder(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'parent_id' => ['nullable', 'string'],
|
||||
'drive_id' => ['nullable', 'string'],
|
||||
]);
|
||||
$folder = $this->googleDrive->createFolder(
|
||||
Auth::user(),
|
||||
$request->input('name'),
|
||||
$request->input('parent_id'),
|
||||
$request->input('drive_id')
|
||||
);
|
||||
|
||||
return response()->json(['data' => $folder]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user