aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_versions/tests/StorageTest.php
blob: 592f03f5e6304afe9b5f461fe018889c6efaa7c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\files_versions\tests;

use OCA\Files_Versions\Expiration;
use OCA\Files_Versions\Storage;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Server;
use Test\TestCase;
use Test\Traits\UserTrait;

/**
 * @group DB
 */
class StorageTest extends TestCase {
	use UserTrait;

	private $versionsRoot;
	private $userFolder;
	private int $expireTimestamp = 10;

	protected function setUp(): void {
		parent::setUp();

		$expiration = $this->createMock(Expiration::class);
		$expiration->method('getMaxAgeAsTimestamp')
			->willReturnCallback(function () {
				return $this->expireTimestamp;
			});
		$this->overwriteService(Expiration::class, $expiration);

		\OC::$server->boot();

		$this->createUser('version_test', '');
		$this->loginAsUser('version_test');
		/** @var IRootFolder $root */
		$root = Server::get(IRootFolder::class);
		$this->userFolder = $root->getUserFolder('version_test');
	}


	protected function createPastFile(string $path, int $mtime): void {
		try {
			$file = $this->userFolder->get($path);
		} catch (NotFoundException $e) {
			$file = $this->userFolder->newFile($path);
		}
		$file->putContent((string)$mtime);
		$file->touch($mtime);
	}

	public function testExpireMaxAge(): void {
		$this->userFolder->newFolder('folder1');
		$this->userFolder->newFolder('folder1/sub1');
		$this->userFolder->newFolder('folder2');

		$this->createPastFile('file1', 100);
		$this->createPastFile('file1', 500);
		$this->createPastFile('file1', 900);

		$this->createPastFile('folder1/file2', 100);
		$this->createPastFile('folder1/file2', 200);
		$this->createPastFile('folder1/file2', 300);

		$this->createPastFile('folder1/sub1/file3', 400);
		$this->createPastFile('folder1/sub1/file3', 500);
		$this->createPastFile('folder1/sub1/file3', 600);

		$this->createPastFile('folder2/file4', 100);
		$this->createPastFile('folder2/file4', 600);
		$this->createPastFile('folder2/file4', 800);

		$this->assertCount(2, Storage::getVersions('version_test', 'file1'));
		$this->assertCount(2, Storage::getVersions('version_test', 'folder1/file2'));
		$this->assertCount(2, Storage::getVersions('version_test', 'folder1/sub1/file3'));
		$this->assertCount(2, Storage::getVersions('version_test', 'folder2/file4'));

		$this->expireTimestamp = 150;
		Storage::expireOlderThanMaxForUser('version_test');

		$this->assertCount(1, Storage::getVersions('version_test', 'file1'));
		$this->assertCount(1, Storage::getVersions('version_test', 'folder1/file2'));
		$this->assertCount(2, Storage::getVersions('version_test', 'folder1/sub1/file3'));
		$this->assertCount(1, Storage::getVersions('version_test', 'folder2/file4'));

		$this->expireTimestamp = 550;
		Storage::expireOlderThanMaxForUser('version_test');

		$this->assertCount(0, Storage::getVersions('version_test', 'file1'));
		$this->assertCount(0, Storage::getVersions('version_test', 'folder1/file2'));
		$this->assertCount(0, Storage::getVersions('version_test', 'folder1/sub1/file3'));
		$this->assertCount(1, Storage::getVersions('version_test', 'folder2/file4'));
	}
}
use OCP\IRequest; use OCP\IURLGenerator; use OCP\IUserSession; use OCP\Share\IManager; /** * Class ViewController * * @package OCA\Files\Controller */ class ViewController extends Controller { /** @var string */ protected $appName; /** @var IRequest */ protected $request; /** @var IURLGenerator */ protected $urlGenerator; /** @var IL10N */ protected $l10n; /** @var IConfig */ protected $config; /** @var IEventDispatcher */ protected $eventDispatcher; /** @var IUserSession */ protected $userSession; /** @var IAppManager */ protected $appManager; /** @var IRootFolder */ protected $rootFolder; /** @var Helper */ protected $activityHelper; /** @var IInitialState */ private $initialState; /** @var ITemplateManager */ private $templateManager; /** @var IManager */ private $shareManager; public function __construct(string $appName, IRequest $request, IURLGenerator $urlGenerator, IL10N $l10n, IConfig $config, IEventDispatcher $eventDispatcher, IUserSession $userSession, IAppManager $appManager, IRootFolder $rootFolder, Helper $activityHelper, IInitialState $initialState, ITemplateManager $templateManager, IManager $shareManager ) { parent::__construct($appName, $request); $this->appName = $appName; $this->request = $request; $this->urlGenerator = $urlGenerator; $this->l10n = $l10n; $this->config = $config; $this->eventDispatcher = $eventDispatcher; $this->userSession = $userSession; $this->appManager = $appManager; $this->rootFolder = $rootFolder; $this->activityHelper = $activityHelper; $this->initialState = $initialState; $this->templateManager = $templateManager; $this->shareManager = $shareManager; } /** * @param string $appName * @param string $scriptName * @return string */ protected function renderScript($appName, $scriptName) { $content = ''; $appPath = \OC_App::getAppPath($appName); $scriptPath = $appPath . '/' . $scriptName; if (file_exists($scriptPath)) { // TODO: sanitize path / script name ? ob_start(); include $scriptPath; $content = ob_get_contents(); @ob_end_clean(); } return $content; } /** * FIXME: Replace with non static code * * @return array * @throws \OCP\Files\NotFoundException */ protected function getStorageInfo() { \OC_Util::setupFS(); $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false); return \OC_Helper::getStorageInfo('/', $dirInfo); } /** * @NoCSRFRequired * @NoAdminRequired * * @param string $fileid * @return TemplateResponse|RedirectResponse * @throws NotFoundException */ public function showFile(string $fileid = null): Response { // This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server. try { return $this->redirectToFile($fileid, true); } catch (NotFoundException $e) { return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); } } /** * @NoCSRFRequired * @NoAdminRequired * * @param string $dir * @param string $view * @param string $fileid * @param bool $fileNotFound * @param string $openfile - the openfile URL parameter if it was present in the initial request * @return TemplateResponse|RedirectResponse * @throws NotFoundException */ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false, $openfile = null) { if ($fileid !== null) { try { return $this->redirectToFile($fileid); } catch (NotFoundException $e) { return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); } } $nav = new \OCP\Template('files', 'appnavigation', ''); // Load the files we need \OCP\Util::addStyle('files', 'merged'); \OCP\Util::addScript('files', 'merged-index'); \OCP\Util::addScript('files', 'dist/main'); // mostly for the home storage's free space // FIXME: Make non static $storageInfo = $this->getStorageInfo(); $user = $this->userSession->getUser()->getUID(); // Get all the user favorites to create a submenu try { $favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID()); } catch (\RuntimeException $e) { $favElements['folders'] = []; } $collapseClasses = ''; if (count($favElements['folders']) > 0) { $collapseClasses = 'collapsible'; } $favoritesSublistArray = []; $navBarPositionPosition = 6; $currentCount = 0; foreach ($favElements['folders'] as $favElement) { $link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $favElement, 'view' => 'files']); $sortingValue = ++$currentCount; $element = [ 'id' => str_replace('/', '-', $favElement), 'view' => 'files', 'href' => $link, 'dir' => $favElement, 'order' => $navBarPositionPosition, 'folderPosition' => $sortingValue, 'name' => basename($favElement), 'icon' => 'files', 'quickaccesselement' => 'true' ]; array_push($favoritesSublistArray, $element); $navBarPositionPosition++; } $navItems = \OCA\Files\App::getNavigationManager()->getAll(); // add the favorites entry in menu $navItems['favorites']['sublist'] = $favoritesSublistArray; $navItems['favorites']['classes'] = $collapseClasses; // parse every menu and add the expandedState user value foreach ($navItems as $key => $item) { if (isset($item['expandedState'])) { $navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1'; } } $nav->assign('navigationItems', $navItems); $nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used'])); if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) { $totalSpace = $this->l10n->t('Unlimited'); } else { $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); } $nav->assign('total_space', $totalSpace); $nav->assign('quota', $storageInfo['quota']); $nav->assign('usage_relative', $storageInfo['relative']); $nav->assign('webdav_url', \OCP\Util::linkToRemote('dav/files/' . $user)); $contentItems = []; // render the container content for every navigation item foreach ($navItems as $item) { $content = ''; if (isset($item['script'])) { $content = $this->renderScript($item['appname'], $item['script']); } // parse submenus if (isset($item['sublist'])) { foreach ($item['sublist'] as $subitem) { $subcontent = ''; if (isset($subitem['script'])) { $subcontent = $this->renderScript($subitem['appname'], $subitem['script']); } $contentItems[$subitem['id']] = [ 'id' => $subitem['id'], 'content' => $subcontent ]; } } $contentItems[$item['id']] = [ 'id' => $item['id'], 'content' => $content ]; } $event = new LoadAdditionalScriptsEvent(); $this->eventDispatcher->dispatchTyped($event); $this->eventDispatcher->dispatchTyped(new LoadSidebar()); // Load Viewer scripts if (class_exists(LoadViewer::class)) { $this->eventDispatcher->dispatchTyped(new LoadViewer()); } $this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false); $this->initialState->provideInitialState('templates', $this->templateManager->listCreators()); $params = []; $params['usedSpacePercent'] = (int) $storageInfo['relative']; $params['owner'] = $storageInfo['owner'] ?? ''; $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? ''; $params['isPublic'] = false; $params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no'; $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); $params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false); $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); $params['showHiddenFiles'] = $showHidden ? 1 : 0; $cropImagePreviews = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', true); $params['cropImagePreviews'] = $cropImagePreviews ? 1 : 0; $params['fileNotFound'] = $fileNotFound ? 1 : 0; $params['appNavigation'] = $nav; $params['appContents'] = $contentItems; $params['hiddenFields'] = $event->getHiddenFields(); $response = new TemplateResponse( $this->appName, 'index', $params ); $policy = new ContentSecurityPolicy(); $policy->addAllowedFrameDomain('\'self\''); $response->setContentSecurityPolicy($policy); $this->provideInitialState($dir, $openfile); return $response; } /** * Add openFileInfo in initialState if $openfile is set. * @param string $dir - the ?dir= URL param * @param string $openfile - the ?openfile= URL param * @return void */ private function provideInitialState(string $dir, ?string $openfile): void { if ($openfile === null) { return; } $user = $this->userSession->getUser(); if ($user === null) { return; } $uid = $user->getUID(); $userFolder = $this->rootFolder->getUserFolder($uid); $nodes = $userFolder->getById((int) $openfile); $node = array_shift($nodes); if ($node === null) { return; } // properly format full path and make sure // we're relative to the user home folder $isRoot = $node === $userFolder; $path = $userFolder->getRelativePath($node->getPath()); $directory = $userFolder->getRelativePath($node->getParent()->getPath()); // Prevent opening a file from another folder. if ($dir !== $directory) { return; } $this->initialState->provideInitialState( 'openFileInfo', [ 'id' => $node->getId(), 'name' => $isRoot ? '' : $node->getName(), 'path' => $path, 'directory' => $directory, 'mime' => $node->getMimetype(), 'type' => $node->getType(), 'permissions' => $node->getPermissions(), ] ); } /** * Redirects to the file list and highlight the given file id * * @param string $fileId file id to show * @param bool $setOpenfile - whether or not to set the openfile URL parameter * @return RedirectResponse redirect response or not found response * @throws \OCP\Files\NotFoundException */ private function redirectToFile($fileId, bool $setOpenfile = false) { $uid = $this->userSession->getUser()->getUID(); $baseFolder = $this->rootFolder->getUserFolder($uid); $files = $baseFolder->getById($fileId); $params = []; if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) { $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); $files = $baseFolder->getById($fileId); $params['view'] = 'trashbin'; } if (!empty($files)) { $file = current($files); if ($file instanceof Folder) { // set the full path to enter the folder $params['dir'] = $baseFolder->getRelativePath($file->getPath()); } else { // set parent path as dir $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath()); // and scroll to the entry $params['scrollto'] = $file->getName(); if ($setOpenfile) { // forward the openfile URL parameter. $params['openfile'] = $fileId; } } return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); } throw new \OCP\Files\NotFoundException(); } }