Skip to content

Commit ed559f3

Browse files
Merge pull request #62197 from nextcloud/backport/61908/stable34
[stable34] feat(files_sharing): schedule external share scan job when accepted
2 parents 843f6a6 + f63c5b3 commit ed559f3

6 files changed

Lines changed: 63 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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
class ExternalShareScanJob extends QueuedJob {
22+
public function __construct(
23+
private readonly IConfig $config,
24+
private readonly IRootFolder $rootFolder,
25+
private readonly LoggerInterface $logger,
26+
ITimeFactory $time,
27+
) {
28+
parent::__construct($time);
29+
}
30+
31+
#[\Override]
32+
protected function run($argument): void {
33+
if ($this->config->getSystemValueBool('files_no_background_scan', false)) {
34+
return;
35+
}
36+
37+
[$userId, $path] = $argument;
38+
try {
39+
$this->rootFolder
40+
->getUserFolder($userId)
41+
->get($path)
42+
->getStorage()
43+
->getScanner()
44+
->scan('');
45+
} catch (\Exception $e) {
46+
$this->logger->error($e->getMessage(), [ 'exception' => $e ]);
47+
}
48+
}
49+
}

apps/files_sharing/lib/Controller/ExternalSharesController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
*/
88
namespace OCA\Files_Sharing\Controller;
99

10+
use OCA\Files_Sharing\BackgroundJob\ExternalShareScanJob;
1011
use OCA\Files_Sharing\External\Manager;
1112
use OCP\AppFramework\Controller;
1213
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1314
use OCP\AppFramework\Http\JSONResponse;
15+
use OCP\BackgroundJob\IJobList;
1416
use OCP\IRequest;
1517

1618
/**
@@ -23,6 +25,7 @@ public function __construct(
2325
string $appName,
2426
IRequest $request,
2527
private readonly Manager $externalManager,
28+
private IJobList $jobList,
2629
) {
2730
parent::__construct($appName, $request);
2831
}
@@ -43,6 +46,7 @@ public function create(string $id): JSONResponse {
4346
$externalShare = $this->externalManager->getShare($id);
4447
if ($externalShare !== false) {
4548
$this->externalManager->acceptShare($externalShare);
49+
$this->jobList->add(ExternalShareScanJob::class, [$externalShare->getUser(), $externalShare->getMountpoint()]);
4650
}
4751
return new JSONResponse();
4852
}

apps/files_sharing/tests/Controller/ExternalShareControllerTest.php

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

@@ -22,18 +23,21 @@
2223
class ExternalShareControllerTest extends \Test\TestCase {
2324
private IRequest&MockObject $request;
2425
private Manager&MockObject $externalManager;
26+
private IJobList&MockObject $jobList;
2527

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

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

@@ -57,6 +61,9 @@ public function testCreate(): void {
5761
->expects($this->once())
5862
->method('acceptShare')
5963
->with($share);
64+
$this->jobList
65+
->expects($this->once())
66+
->method('add');
6067

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

0 commit comments

Comments
 (0)