Skip to content
Merged
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
32 changes: 21 additions & 11 deletions apps/files/lib/Command/ScanAppData.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ protected function configure(): void {
$this->addArgument('folder', InputArgument::OPTIONAL, 'The appdata subfolder to scan', '');
}

protected function getScanner(OutputInterface $output): Scanner {
$connection = $this->reconnectToDatabase($output);
return new Scanner(
null,
new ConnectionAdapter($connection),
Server::get(IEventDispatcher::class),
Server::get(LoggerInterface::class),
);
}

protected function scanFiles(OutputInterface $output, string $folder): int {
if ($folder === 'preview' || $folder === '') {
$this->previewsCounter = $this->previewStorage->scan();
Expand All @@ -79,13 +89,7 @@ protected function scanFiles(OutputInterface $output, string $folder): int {
}
}

$connection = $this->reconnectToDatabase($output);
$scanner = new Scanner(
null,
new ConnectionAdapter($connection),
Server::get(IEventDispatcher::class),
Server::get(LoggerInterface::class)
);
$scanner = $this->getScanner($output);

# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output): void {
Expand Down Expand Up @@ -142,6 +146,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$folder = $input->getArgument('folder');

// Start the timer
$this->execTime = -microtime(true);

$this->initTools();

$exitCode = $this->scanFiles($output, $folder);
Expand All @@ -155,8 +162,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* Initialises some useful tools for the Command
*/
protected function initTools(): void {
// Start the timer
$this->execTime = -microtime(true);
// Convert PHP errors to exceptions
set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
}
Expand Down Expand Up @@ -210,6 +215,11 @@ protected function showSummary(array $headers, ?array $rows, OutputInterface $ou
$rows[] = $this->filesCounter;
$rows[] = $niceDate;
}

$this->displayTable($output, $headers, $rows);
}

protected function displayTable($output, $headers, $rows): void {
$table = new Table($output);
$table
->setHeaders($headers)
Expand Down Expand Up @@ -250,9 +260,9 @@ protected function reconnectToDatabase(OutputInterface $output): Connection {
* @throws NotFoundException
*/
private function getAppDataFolder(): Node {
$instanceId = $this->config->getSystemValue('instanceid', null);
$instanceId = $this->config->getSystemValueString('instanceid', '');

if ($instanceId === null) {
if ($instanceId === '') {
throw new NotFoundException();
}

Expand Down
234 changes: 234 additions & 0 deletions apps/files/tests/Command/ScanAppDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH
* SPDX-FileContributor: Carl Schwan
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Files\Tests\Command;

use OC\Files\Mount\ObjectHomeMountProvider;
use OC\Files\Utils\Scanner;
use OC\Preview\Db\Preview;
use OC\Preview\Db\PreviewMapper;
use OC\Preview\PreviewService;
use OC\Preview\Storage\StorageFactory;
use OCA\Files\Command\ScanAppData;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder;
use OCP\Files\ISetupManager;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorageFactory;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Server;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Test\TestCase;

#[Group(name: 'DB')]
class ScanAppDataTest extends TestCase {
private IRootFolder $rootFolder;
private IConfig $config;
private StorageFactory $storageFactory;
private OutputInterface&MockObject $output;
private InputInterface&MockObject $input;
private Scanner&MockObject $internalScanner;
private ScanAppData $scanner;
private string $user;

public function setUp(): void {
$this->config = Server::get(IConfig::class);
$this->rootFolder = Server::get(IRootFolder::class);
$this->storageFactory = Server::get(StorageFactory::class);
$this->user = static::getUniqueID('user');
$user = Server::get(IUserManager::class)->createUser($this->user, 'test');
Server::get(ISetupManager::class)->setupForUser($user);
Server::get(IUserSession::class)->setUser($user);
$this->output = $this->createMock(OutputInterface::class);
$this->input = $this->createMock(InputInterface::class);
$this->scanner = $this->getMockBuilder(ScanAppData::class)
->onlyMethods(['displayTable', 'initTools', 'getScanner'])
->setConstructorArgs([$this->rootFolder, $this->config, $this->storageFactory])
->getMock();
$this->internalScanner = $this->getMockBuilder(Scanner::class)
->onlyMethods(['scan'])
->disableOriginalConstructor()
->getMock();
$this->scanner->method('getScanner')->willReturn($this->internalScanner);

$this->scanner->method('initTools')
->willReturnCallback(function () {});
try {
$this->rootFolder->get($this->rootFolder->getAppDataDirectoryName() . '/preview')->delete();
} catch (NotFoundException) {
}

Server::get(PreviewService::class)->deleteAll();

try {
$appDataFolder = $this->rootFolder->get($this->rootFolder->getAppDataDirectoryName());
} catch (NotFoundException) {
$appDataFolder = $this->rootFolder->newFolder($this->rootFolder->getAppDataDirectoryName());
}

$appDataFolder->newFolder('preview');
}

public function tearDown(): void {
Server::get(IUserManager::class)->get($this->user)->delete();
Server::get(IUserSession::class)->setUser(null);
$this->rootFolder->get($this->rootFolder->getAppDataDirectoryName())->delete();
parent::tearDown();
}

public function testScanAppDataRoot(): void {
$homeProvider = Server::get(ObjectHomeMountProvider::class);
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('foo');
if ($homeProvider->getHomeMountForUser($user, $this->createMock(IStorageFactory::class)) !== null) {
$this->markTestSkipped();
}

$this->input->method('getArgument')->with('folder')->willReturn('');
$this->internalScanner->method('scan')->willReturnCallback(function () {
$this->internalScanner->emit('\OC\Files\Utils\Scanner', 'scanFile', ['path42']);
$this->internalScanner->emit('\OC\Files\Utils\Scanner', 'scanFolder', ['path42']);
$this->internalScanner->emit('\OC\Files\Utils\Scanner', 'scanFolder', ['path42']);
});
$this->scanner->expects($this->once())->method('displayTable')
->willReturnCallback(function (OutputInterface $output, array $headers, array $rows): void {
$this->assertEquals($this->output, $output);
$this->assertEquals(['Previews', 'Folders', 'Files', 'Elapsed time'], $headers);
$this->assertEquals(0, $rows[0]);
$this->assertEquals(2, $rows[1]);
$this->assertEquals(1, $rows[2]);
});

$errorCode = $this->invokePrivate($this->scanner, 'execute', [$this->input, $this->output]);
$this->assertEquals(ScanAppData::SUCCESS, $errorCode);
}


public static function scanPreviewLocalData(): \Generator {
yield 'initial migration done' => [true, null];
yield 'initial migration not done' => [false, false];
yield 'initial migration not done with legacy paths' => [false, true];
}

#[DataProvider(methodName: 'scanPreviewLocalData')]
public function testScanAppDataPreviewOnlyLocalFile(bool $migrationDone, ?bool $legacy): void {
$homeProvider = Server::get(ObjectHomeMountProvider::class);
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('foo');
if ($homeProvider->getHomeMountForUser($user, $this->createMock(IStorageFactory::class)) !== null) {
$this->markTestSkipped();
}
$this->input->method('getArgument')->with('folder')->willReturn('preview');

$file = $this->rootFolder->getUserFolder($this->user)->newFile('myfile.jpeg');

if ($migrationDone) {
Server::get(IAppConfig::class)->setValueBool('core', 'previewMovedDone', true);
$preview = new Preview();
$preview->generateId();
$preview->setFileId($file->getId());
$preview->setStorageId($file->getStorage()->getCache()->getNumericStorageId());
$preview->setEtag('abc');
$preview->setMtime(1);
$preview->setWidth(1024);
$preview->setHeight(1024);
$preview->setMimeType('image/jpeg');
$preview->setSize($this->storageFactory->writePreview($preview, 'preview content'));
Server::get(PreviewMapper::class)->insert($preview);

$preview = new Preview();
$preview->generateId();
$preview->setFileId($file->getId());
$preview->setStorageId($file->getStorage()->getCache()->getNumericStorageId());
$preview->setEtag('abc');
$preview->setMtime(1);
$preview->setWidth(2024);
$preview->setHeight(2024);
$preview->setMax(true);
$preview->setMimeType('image/jpeg');
$preview->setSize($this->storageFactory->writePreview($preview, 'preview content'));
Server::get(PreviewMapper::class)->insert($preview);

$preview = new Preview();
$preview->generateId();
$preview->setFileId($file->getId());
$preview->setStorageId($file->getStorage()->getCache()->getNumericStorageId());
$preview->setEtag('abc');
$preview->setMtime(1);
$preview->setWidth(2024);
$preview->setHeight(2024);
$preview->setMax(true);
$preview->setCropped(true);
$preview->setMimeType('image/jpeg');
$preview->setSize($this->storageFactory->writePreview($preview, 'preview content'));
Server::get(PreviewMapper::class)->insert($preview);

$previews = Server::get(PreviewService::class)->getAvailablePreviews([$file->getId()]);
$this->assertCount(3, $previews[$file->getId()]);
} else {
Server::get(IAppConfig::class)->setValueBool('core', 'previewMovedDone', false);
/** @var Folder $previewFolder */
$previewFolder = $this->rootFolder->get($this->rootFolder->getAppDataDirectoryName() . '/preview');
if (!$legacy) {
foreach (str_split(substr(md5((string)$file->getId()), 0, 7)) as $subPath) {
$previewFolder = $previewFolder->newFolder($subPath);
}
}
$previewFolder = $previewFolder->newFolder((string)$file->getId());
$previewFolder->newFile('1024-1024.jpg');
$previewFolder->newFile('2024-2024-max.jpg');
$previewFolder->newFile('2024-2024-max-crop.jpg');

$this->assertCount(3, $previewFolder->getDirectoryListing());

$previews = Server::get(PreviewService::class)->getAvailablePreviews([$file->getId()]);
$this->assertCount(0, $previews[$file->getId()]);
}

$mimetypeDetector = $this->createMock(IMimeTypeDetector::class);
$mimetypeDetector->method('detectPath')->willReturn('image/jpeg');

$appConfig = $this->createMock(IAppConfig::class);
$appConfig->method('getValueBool')->with('core', 'previewMovedDone')->willReturn($migrationDone);

$mimetypeLoader = $this->createMock(IMimeTypeLoader::class);
$mimetypeLoader->method('getMimetypeById')->willReturn('image/jpeg');

$this->scanner->expects($this->once())->method('displayTable')
->willReturnCallback(function ($output, array $headers, array $rows): void {
$this->assertEquals($output, $this->output);
$this->assertEquals(['Previews', 'Folders', 'Files', 'Elapsed time'], $headers);
$this->assertEquals(3, $rows[0]);
$this->assertEquals(0, $rows[1]);
$this->assertEquals(0, $rows[2]);
});
$errorCode = $this->invokePrivate($this->scanner, 'execute', [$this->input, $this->output]);
$this->assertEquals(ScanAppData::SUCCESS, $errorCode);

/** @var Folder $previewFolder */
$previewFolder = $this->rootFolder->get($this->rootFolder->getAppDataDirectoryName() . '/preview');
$children = $previewFolder->getDirectoryListing();
$this->assertCount(0, $children);

Server::get(PreviewService::class)->deleteAll();
}
}
3 changes: 0 additions & 3 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1287,9 +1287,6 @@
<InvalidArgument>
<code><![CDATA[[$this, 'exceptionErrorHandler']]]></code>
</InvalidArgument>
<NullArgument>
<code><![CDATA[null]]></code>
</NullArgument>
</file>
<file src="apps/files/lib/Controller/ApiController.php">
<DeprecatedMethod>
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Cache/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ protected function scanChildren(string $path, $recursive, int $reuse, int $folde
* @param bool|IScanner::SCAN_RECURSIVE_INCOMPLETE $recursive
*/
private function handleChildren(string $path, $recursive, int $reuse, int $folderId, bool $lock, int|float &$size, bool &$etagChanged): array {
// we put this in it's own function so it cleans up the memory before we start recursing
// we put this in its own function so it cleans up the memory before we start recursing
$existingChildren = $this->getExistingChildren($folderId);
$newChildren = iterator_to_array($this->storage->getDirectoryContent($path));

Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Utils/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Scanner extends PublicEmitter {
protected int $entriesToCommit = 0;

public function __construct(
private string $user,
private ?string $user,
protected ?IDBConnection $db,
private IEventDispatcher $dispatcher,
protected LoggerInterface $logger,
Expand Down
30 changes: 20 additions & 10 deletions lib/private/Preview/Db/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,30 @@ public static function fromPath(string $path, IMimeTypeDetector $mimeTypeDetecto
$preview->setFileId((int)basename(dirname($path)));

$fileName = pathinfo($path, PATHINFO_FILENAME) . '.' . pathinfo($path, PATHINFO_EXTENSION);
$ok = preg_match('/(([A-Za-z0-9\+\/]+)-)?([0-9]+)-([0-9]+)(-(max))?(-(crop))?\.([a-z]{3,4})/', $fileName, $matches);
$ok = preg_match('/(([A-Za-z0-9\+\/]+)-)?([0-9]+)-([0-9]+)(-(crop))?(-(max))?\.([a-z]{3,4})$/', $fileName, $matches);

if ($ok !== 1) {
return false;
$ok = preg_match('/(([A-Za-z0-9\+\/]+)-)?([0-9]+)-([0-9]+)(-(max))?(-(crop))?\.([a-z]{3,4})$/', $fileName, $matches);
if ($ok !== 1) {
return false;
}
[
2 => $version,
3 => $width,
4 => $height,
6 => $max,
8 => $crop,
] = $matches;
} else {
[
2 => $version,
3 => $width,
4 => $height,
6 => $crop,
8 => $max,
] = $matches;
}

[
2 => $version,
3 => $width,
4 => $height,
6 => $max,
8 => $crop,
] = $matches;

$preview->setMimeType($mimeTypeDetector->detectPath($fileName));

$preview->setWidth((int)$width);
Expand Down
Loading
Loading