add OAUth2.0

This commit is contained in:
2026-06-15 11:14:35 +02:00
parent 7973a94498
commit 69f4d6a602
32 changed files with 1126 additions and 51 deletions
+62
View File
@@ -4,7 +4,9 @@ declare(strict_types=1);
namespace App\Services;
use App\Enums\GoogleService;
use App\Models\StorageRepository;
use App\Services\GoogleOAuthService;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use League\Flysystem\Filesystem;
@@ -69,6 +71,20 @@ class StorageRepositoryService
private function checkGoogleDriveApiStatus(array $config): ?string
{
try {
try {
$oauth = app(GoogleOAuthService::class);
$connection = $oauth->getConnection(GoogleService::Drive);
if ($connection) {
$client = $oauth->buildClient(GoogleService::Drive);
$service = new \Google_Service_Drive($client);
$rootId = $config['root_folder_id'] ?? 'root';
$service->files->get($rootId, ['fields' => 'id']);
return null;
}
} catch (\Exception $e) {
// fall through to config-based auth
}
$client = new \Google_Client();
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
@@ -244,6 +260,18 @@ class StorageRepositoryService
{
$config = $repo->getDecryptedConfig();
try {
$oauth = app(GoogleOAuthService::class);
$connection = $oauth->getConnection(GoogleService::Drive);
if ($connection) {
$client = $oauth->buildClient(GoogleService::Drive);
$service = new \Google_Service_Drive($client);
return $this->readGoogleDriveFileWithService($service, $config, $normalizedPath, $basename);
}
} catch (\Exception $e) {
\Illuminate\Support\Facades\Log::warning('Central Google Drive OAuth failed in readGoogleDriveFile, falling back: ' . $e->getMessage());
}
$client = new \Google_Client();
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
@@ -283,6 +311,40 @@ class StorageRepositoryService
];
}
private function readGoogleDriveFileWithService(\Google_Service_Drive $service, array $config, string $normalizedPath, string $basename): array
{
$rootFolderId = $config['root_folder_id'] ?? 'root';
$fileId = $this->findGoogleDriveFileId($service, $rootFolderId, $normalizedPath);
$file = $service->files->get($fileId, ['fields' => 'id,name,mimeType,size']);
$mimeType = $file->getMimeType();
if ($mimeType && str_starts_with($mimeType, 'application/vnd.google-apps.')) {
return $this->exportGoogleDriveDoc($service, $fileId, $mimeType, $basename);
}
$response = $service->files->get($fileId, ['alt' => 'media']);
$body = $response->getBody();
$stream = $body instanceof \Psr\Http\Message\StreamInterface ? $body->detach() : null;
if (!$stream) {
$stream = fopen('php://temp', 'r+');
fwrite($stream, (string) $body);
rewind($stream);
}
$fileSize = $file->getSize();
return [
'stream' => $stream,
'mimeType' => $mimeType,
'basename' => $basename,
'fileSize' => $fileSize !== null ? (int) $fileSize : null,
];
}
private function exportGoogleDriveDoc(\Google_Service_Drive $service, string $fileId, string $googleMimeType, string $originalBasename): array
{
$exportMap = [