Skip to content

Commit 2e1973d

Browse files
committed
feat(files_sharing): schedule external share scan job when accepted
Signed-off-by: Kent Delante <kent@delante.me> Schedule a scan job on an external share when an account accepts it to make file information available (e.g. size) without having to interact with the share.
1 parent 5ae39da commit 2e1973d

6 files changed

Lines changed: 65 additions & 0 deletions

File tree

apps/files_sharing/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Turning the feature off removes shared files and folders on the server for all s
3535
<job>OCA\Files_Sharing\ExpireSharesJob</job>
3636
<job>OCA\Files_Sharing\SharesReminderJob</job>
3737
<job>OCA\Files_Sharing\BackgroundJob\FederatedSharesDiscoverJob</job>
38+
<job>OCA\Files_Sharing\BackgroundJob\ExternalShareScanJob</job>
3839
</background-jobs>
3940

4041
<repair-steps>

apps/files_sharing/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'OCA\\Files_Sharing\\Activity\\Settings\\ShareActivitySettings' => $baseDir . '/../lib/Activity/Settings/ShareActivitySettings.php',
2121
'OCA\\Files_Sharing\\Activity\\Settings\\Shared' => $baseDir . '/../lib/Activity/Settings/Shared.php',
2222
'OCA\\Files_Sharing\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
23+
'OCA\\Files_Sharing\\BackgroundJob\\ExternalShareScanJob' => $baseDir . '/../lib/BackgroundJob/ExternalShareScanJob.php',
2324
'OCA\\Files_Sharing\\BackgroundJob\\FederatedSharesDiscoverJob' => $baseDir . '/../lib/BackgroundJob/FederatedSharesDiscoverJob.php',
2425
'OCA\\Files_Sharing\\Cache' => $baseDir . '/../lib/Cache.php',
2526
'OCA\\Files_Sharing\\Capabilities' => $baseDir . '/../lib/Capabilities.php',

apps/files_sharing/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ComposerStaticInitFiles_Sharing
3535
'OCA\\Files_Sharing\\Activity\\Settings\\ShareActivitySettings' => __DIR__ . '/..' . '/../lib/Activity/Settings/ShareActivitySettings.php',
3636
'OCA\\Files_Sharing\\Activity\\Settings\\Shared' => __DIR__ . '/..' . '/../lib/Activity/Settings/Shared.php',
3737
'OCA\\Files_Sharing\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
38+
'OCA\\Files_Sharing\\BackgroundJob\\ExternalShareScanJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/ExternalShareScanJob.php',
3839
'OCA\\Files_Sharing\\BackgroundJob\\FederatedSharesDiscoverJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/FederatedSharesDiscoverJob.php',
3940
'OCA\\Files_Sharing\\Cache' => __DIR__ . '/..' . '/../lib/Cache.php',
4041
'OCA\\Files_Sharing\\Capabilities' => __DIR__ . '/..' . '/../lib/Capabilities.php',
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-only
8+
*/
9+
10+
namespace OCA\Files_Sharing\BackgroundJob;
11+
12+
use OCP\AppFramework\Utility\ITimeFactory;
13+
use OCP\BackgroundJob\QueuedJob;
14+
use OCP\Files\IRootFolder;
15+
use OCP\IConfig;
16+
use Psr\Log\LoggerInterface;
17+
18+
/**
19+
* Scans an external share with a specific path
20+
*
21+
* @since 35.0.0
22+
*/
23+
class ExternalShareScanJob extends QueuedJob {
24+
public function __construct(
25+
private IConfig $config,
26+
private IRootFolder $rootFolder,
27+
private LoggerInterface $logger,
28+
protected ITimeFactory $time,
29+
) {
30+
parent::__construct($time);
31+
}
32+
33+
#[\Override]
34+
protected function run($argument): void {
35+
if ($this->config->getSystemValueBool('files_no_background_scan', false)) {
36+
return;
37+
}
38+
39+
[$userId, $path] = $argument;
40+
try {
41+
$this->rootFolder
42+
->getUserFolder($userId)
43+
->get($path)
44+
->getStorage()
45+
->getScanner()
46+
->scan('');
47+
} catch (\Exception $e) {
48+
$this->logger->error($e->getMessage(), [ 'exception' => $e ]);
49+
}
50+
}
51+
}

apps/files_sharing/lib/Controller/ExternalSharesController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
namespace OCA\Files_Sharing\Controller;
1010

11+
use OCA\Files_Sharing\BackgroundJob\ExternalShareScanJob;
1112
use OCA\Files_Sharing\External\Manager;
1213
use OCP\AppFramework\Controller;
1314
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1415
use OCP\AppFramework\Http\JSONResponse;
16+
use OCP\BackgroundJob\IJobList;
1517
use OCP\IRequest;
1618

1719
/**
@@ -24,6 +26,7 @@ public function __construct(
2426
string $appName,
2527
IRequest $request,
2628
private readonly Manager $externalManager,
29+
private IJobList $jobList,
2730
) {
2831
parent::__construct($appName, $request);
2932
}
@@ -44,6 +47,7 @@ public function create(string $id): JSONResponse {
4447
$externalShare = $this->externalManager->getShare($id);
4548
if ($externalShare !== false) {
4649
$this->externalManager->acceptShare($externalShare);
50+
$this->jobList->add(ExternalShareScanJob::class, [$externalShare->getUser(), $externalShare->getMountpoint()]);
4751
}
4852
return new JSONResponse();
4953
}

apps/files_sharing/tests/Controller/ExternalShareControllerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\Files_Sharing\External\ExternalShare;
1313
use OCA\Files_Sharing\External\Manager;
1414
use OCP\AppFramework\Http\JSONResponse;
15+
use OCP\BackgroundJob\IJobList;
1516
use OCP\IRequest;
1617
use PHPUnit\Framework\MockObject\MockObject;
1718

@@ -23,18 +24,21 @@
2324
class ExternalShareControllerTest extends \Test\TestCase {
2425
private IRequest&MockObject $request;
2526
private Manager&MockObject $externalManager;
27+
private IJobList&MockObject $jobList;
2628

2729
protected function setUp(): void {
2830
parent::setUp();
2931
$this->request = $this->createMock(IRequest::class);
3032
$this->externalManager = $this->createMock(Manager::class);
33+
$this->jobList = $this->createMock(IJobList::class);
3134
}
3235

3336
public function getExternalShareController(): ExternalSharesController {
3437
return new ExternalSharesController(
3538
'files_sharing',
3639
$this->request,
3740
$this->externalManager,
41+
$this->jobList,
3842
);
3943
}
4044

@@ -58,6 +62,9 @@ public function testCreate(): void {
5862
->expects($this->once())
5963
->method('acceptShare')
6064
->with($share);
65+
$this->jobList
66+
->expects($this->once())
67+
->method('add');
6168

6269
$this->assertEquals(new JSONResponse(), $this->getExternalShareController()->create('4'));
6370
}

0 commit comments

Comments
 (0)