diff --git a/appinfo/info.xml b/appinfo/info.xml index 8172b896..f0080b0f 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -16,7 +16,7 @@ https://github.com/nextcloud/integration_google/issues https://github.com/nextcloud/integration_google/raw/master/img/screenshot1.jpg - + OCA\Google\Settings\Admin diff --git a/lib/Controller/ConfigController.php b/lib/Controller/ConfigController.php index 0ea0f0f8..b31f00df 100644 --- a/lib/Controller/ConfigController.php +++ b/lib/Controller/ConfigController.php @@ -16,6 +16,7 @@ use Exception; use OCA\Google\AppInfo\Application; use OCA\Google\Service\GoogleAPIService; +use OCA\Google\Service\GoogleDriveAPIService; use OCA\Google\Service\SecretService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; @@ -49,6 +50,7 @@ public function __construct( private IContactManager $contactsManager, private IInitialState $initialStateService, private GoogleAPIService $googleApiService, + private GoogleDriveAPIService $googleDriveApiService, private ?string $userId, private ICrypto $crypto, private SecretService $secretService, @@ -85,6 +87,9 @@ public function setConfig(array $values): DataResponse { $userRoot = $root->getUserFolder($this->userId); $result['free_space'] = \OCA\Google\Settings\Personal::getFreeSpace($userRoot, $values['drive_output_dir']); } + if (isset($values['importing_drive']) && $values['importing_drive'] === '0') { + $this->googleDriveApiService->cancelImport($this->userId); + } } return new DataResponse($result); } diff --git a/lib/Service/GoogleDriveAPIService.php b/lib/Service/GoogleDriveAPIService.php index 1a2ed4a3..a2cd341a 100644 --- a/lib/Service/GoogleDriveAPIService.php +++ b/lib/Service/GoogleDriveAPIService.php @@ -18,6 +18,7 @@ use OCA\Google\BackgroundJob\ImportDriveJob; use OCA\Google\Service\Utils\FileUtils; use OCP\BackgroundJob\IJobList; +use OCP\Config\IUserConfig; use OCP\Files\File; use OCP\Files\Folder; use OCP\Files\InvalidPathException; @@ -48,6 +49,7 @@ public function __construct( string $appName, private LoggerInterface $logger, private IConfig $config, + private IUserConfig $userConfig, private IRootFolder $root, private IJobList $jobList, private UserScopeService $userScopeService, @@ -153,6 +155,10 @@ public function startImportDrive(string $userId): array { return ['targetPath' => $targetPath]; } + public function cancelImport(string $userId): void { + $this->jobList->remove(ImportDriveJob::class, ['user_id' => $userId]); + } + /** * @param string $userId * @return void @@ -331,6 +337,12 @@ public function importFiles( } foreach ($directoryIdsToExplore as $dirId) { + $cancelImport = $this->hasBeenCancelled($userId); + if ($cancelImport) { + $this->logger->info('Import cancelled by user'); + break; + } + $query = "mimeType!='application/vnd.google-apps.folder' and '" . $dirId . "' in parents"; $earlyResult = $this->retrieveFiles($userId, $dirId, $query, $considerSharedFiles, $rootImportFolder, $rootSharedWithMeImportFolder, $directoriesById, $sharedDirectoriesById, @@ -357,6 +369,15 @@ public function importFiles( ]; } + /** + * @param string $userId + * @return bool + */ + public function hasBeenCancelled(string $userId): bool { + $this->userConfig->clearCache($userId); + return $this->config->getUserValue($userId, Application::APP_ID, 'importing_drive', '0') === '0'; + } + /** * @param array $dirInfo * @return void @@ -824,6 +845,7 @@ private function recursivelyCheckParentOwnership(string $rootId, array $director * @return array|null */ private function retrieveFiles(string $userId, string $dirId, string $query, bool $considerSharedFiles, Folder $rootImportFolder, ?Folder $rootSharedWithMeImportFolder, array $directoriesById, array $sharedDirectoriesById, ?int $maxDownloadSize, string $targetPath, bool $allowParents = true): ?array { + $lastCancelCheck = time(); $alreadyImported = (int)$this->config->getUserValue($userId, Application::APP_ID, 'nb_imported_files', '0'); $alreadyImportedSize = (int)$this->config->getUserValue($userId, Application::APP_ID, 'drive_imported_size', '0'); @@ -848,6 +870,15 @@ private function retrieveFiles(string $userId, string $dirId, string $query, boo return $result; } foreach ($result['files'] as $fileItem) { + if ((time() - $lastCancelCheck) >= 30) { + $cancelImport = $this->hasBeenCancelled($userId); + if ($cancelImport) { + $this->logger->info('Import cancelled by user'); + break 2; + } + $lastCancelCheck = time(); + } + try { if (isset($fileItem['parents']) && count($fileItem['parents']) > 0) { if (!$allowParents) {