Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<bugs>https://github.com/nextcloud/integration_google/issues</bugs>
<screenshot>https://github.com/nextcloud/integration_google/raw/master/img/screenshot1.jpg</screenshot>
<dependencies>
<nextcloud min-version="30" max-version="32"/>
<nextcloud min-version="32" max-version="32"/>
</dependencies>
<settings>
<admin>OCA\Google\Settings\Admin</admin>
Expand Down
5 changes: 5 additions & 0 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
31 changes: 31 additions & 0 deletions lib/Service/GoogleDriveAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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');

Expand All @@ -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) {
Expand Down