71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
<?php
|
|
// gdrive.php
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
function getGoogleDriveClient() {
|
|
if (!class_exists('\Google\Client')) {
|
|
return null;
|
|
}
|
|
|
|
$client = new \Google\Client();
|
|
$client->setApplicationName('P4Q System');
|
|
$client->setScopes([\Google\Service\Drive::DRIVE_FILE]);
|
|
|
|
if (file_exists(GDRIVE_CREDENTIALS_PATH)) {
|
|
try {
|
|
$client->setAuthConfig(GDRIVE_CREDENTIALS_PATH);
|
|
return $client;
|
|
} catch (Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function uploadFileToDrive($filePath, $fileName, $folderId) {
|
|
$client = getGoogleDriveClient();
|
|
if (!$client || empty($folderId)) {
|
|
// Mock upload if no credentials or folder ID
|
|
return "mock_file_id_" . uniqid();
|
|
}
|
|
|
|
$service = new \Google\Service\Drive($client);
|
|
$fileMetadata = new \Google\Service\Drive\DriveFile([
|
|
'name' => $fileName,
|
|
'parents' => [$folderId]
|
|
]);
|
|
|
|
$content = file_get_contents($filePath);
|
|
try {
|
|
$file = $service->files->create($fileMetadata, [
|
|
'data' => $content,
|
|
'mimeType' => mime_content_type($filePath),
|
|
'uploadType' => 'multipart',
|
|
'fields' => 'id'
|
|
]);
|
|
return $file->id;
|
|
} catch (Exception $e) {
|
|
return "mock_file_id_err_" . uniqid();
|
|
}
|
|
}
|
|
|
|
function deleteFileFromDrive($fileId) {
|
|
if (strpos($fileId, 'mock_file_id') === 0) {
|
|
return true;
|
|
}
|
|
|
|
$client = getGoogleDriveClient();
|
|
if (!$client) {
|
|
return true;
|
|
}
|
|
|
|
$service = new \Google\Service\Drive($client);
|
|
try {
|
|
$service->files->delete($fileId);
|
|
return true;
|
|
} catch (Exception $e) {
|
|
return false;
|
|
}
|
|
}
|