Skip to content

Commit

Permalink
Merge pull request #35131 from owncloud/stable10-configurable-thumbna…
Browse files Browse the repository at this point in the history
…ils-path

[stable10] Allow configuring thumbnails path
  • Loading branch information
Vincent Petry committed May 10, 2019
2 parents c8baa5c + c57b8b7 commit 1e67d80
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 2 deletions.
11 changes: 11 additions & 0 deletions config/config.sample.php
Expand Up @@ -776,6 +776,17 @@
*/
'enable_previews' => true,

/**
* Location of the thumbnails folder, defaults to `data/$user/thumbnails` where
* `$user` is the current user. When specified, the format will change to
* `$previews_path/$user` where `$previews_path` is the configured previews base directory
* and `$user` will be substituted with the user id automatically.
*
* For example if `previews_path` is `/var/cache/owncloud/thumbnails` then for a logged in
* user `user1` the thumbnail path will be `/var/cache/owncloud/thumbnails/user1`.
*/
'previews_path' => '',

/**
* The maximum width, in pixels, of a preview. A value of `null` means there
* is no limit.
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Files/Mount/CacheMountProvider.php
Expand Up @@ -36,7 +36,7 @@ class CacheMountProvider implements IMountProvider {
private $config;

/**
* ObjectStoreHomeMountProvider constructor.
* CacheMountProvider constructor.
*
* @param IConfig $config
*/
Expand All @@ -60,7 +60,7 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
}

return [
new MountPoint('\OC\Files\Storage\Local', '/' . $user->getUID() . '/cache', ['datadir' => $cacheDir, $loader])
new MountPoint('\OC\Files\Storage\Local', '/' . $user->getUID() . '/cache', ['datadir' => $cacheDir], $loader)
];
} else {
return [];
Expand Down
69 changes: 69 additions & 0 deletions lib/private/Files/Mount/PreviewsMountProvider.php
@@ -0,0 +1,69 @@
<?php
/**
* @author Vincent Petry <pvince81@owncloud.com>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Files\Mount;

use OCP\Files\Config\IMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use OCP\IUser;

/**
* Mount provider for custom preview storages
*/
class PreviewsMountProvider implements IMountProvider {
/**
* @var IConfig
*/
private $config;

/**
* PreviewsMountProvider constructor.
*
* @param IConfig $config
*/
public function __construct(IConfig $config) {
$this->config = $config;
}

/**
* Get the previews mount for a user
*
* @param IUser $user
* @param IStorageFactory $loader
* @return \OCP\Files\Mount\IMountPoint[]
*/
public function getMountsForUser(IUser $user, IStorageFactory $loader) {
$previewsPath = $this->config->getSystemValue('previews_path', '');
if ($previewsPath !== '') {
$previewsDir = \rtrim($previewsPath, '/') . '/' . $user->getUID();
if (!\file_exists($previewsDir)) {
\mkdir($previewsDir, 0770, true);
}

return [
new MountPoint('\OC\Files\Storage\Local', '/' . $user->getUID() . '/thumbnails', ['datadir' => $previewsDir], $loader)
];
} else {
return [];
}
}
}
2 changes: 2 additions & 0 deletions lib/private/Server.php
Expand Up @@ -51,6 +51,7 @@
use OC\Files\Config\UserMountCache;
use OC\Files\Config\UserMountCacheListener;
use OC\Files\Mount\CacheMountProvider;
use OC\Files\Mount\PreviewsMountProvider;
use OC\Files\Mount\LocalHomeMountProvider;
use OC\Files\Mount\ObjectHomeMountProvider;
use OC\Files\Node\HookConnector;
Expand Down Expand Up @@ -627,6 +628,7 @@ public function __construct($webRoot, \OC\Config $config) {

$config = $c->getConfig();
$manager->registerProvider(new CacheMountProvider($config));
$manager->registerProvider(new PreviewsMountProvider($config));
$manager->registerHomeProvider(new LocalHomeMountProvider());
$manager->registerHomeProvider(new ObjectHomeMountProvider($config));

Expand Down
85 changes: 85 additions & 0 deletions tests/lib/Files/Mount/CacheMountProviderTest.php
@@ -0,0 +1,85 @@
<?php
/**
* @author Vincent Petry <pvince81@owncloud.com>
*
* @copyright Copyright (c) 2019, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace Test\Files\Mount;

use OC\Files\Mount\CacheMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use OCP\IUser;
use OC\Files\Mount\MountPoint;

class CacheMountProviderTest extends \Test\TestCase {

/** @var CacheMountPorivder */
protected $provider;

/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
protected $config;

/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
protected $user;

/** @var IStorageFactory|\PHPUnit\Framework\MockObject\MockObject */
protected $loader;

public function setUp() {
parent::setUp();

$this->config = $this->createMock(IConfig::class);
$this->user = $this->createMock(IUser::class);
$this->loader = $this->createMock(IStorageFactory::class);

$this->provider = new CacheMountProvider($this->config);
}

public function testConfiguredCachePath() {
$tempFolder = \OC::$server->getTempManager()->getTemporaryFolder();
$this->config->expects($this->once())
->method('getSystemValue')
->with('cache_path', '')
->willReturn($tempFolder . '/cache');

$this->user->expects($this->any())->method('getUID')->willReturn('someuser');

$mounts = $this->provider->getMountsForUser($this->user, $this->loader);

$this->assertCount(1, $mounts);
$mount = $mounts[0];

$this->assertInstanceOf(MountPoint::class, $mount);
$this->assertEquals('/someuser/cache/', $mount->getMountPoint());

$this->assertTrue(\is_dir($tempFolder . '/cache/someuser'));

$storageArgs = ['datadir' => $tempFolder . '/cache/someuser'];

$this->loader->expects($this->once())
->method('getInstance')
->with($mount, '\OC\Files\Storage\Local', $storageArgs);

// trigger storage creation which will pass config args above
$mount->getStorage();

\rmdir($tempFolder . '/cache/someuser');
\rmdir($tempFolder . '/cache');
}
}
85 changes: 85 additions & 0 deletions tests/lib/Files/Mount/PreviewsMountProviderTest.php
@@ -0,0 +1,85 @@
<?php
/**
* @author Vincent Petry <pvince81@owncloud.com>
*
* @copyright Copyright (c) 2019, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace Test\Files\Mount;

use OC\Files\Mount\PreviewsMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use OCP\IUser;
use OC\Files\Mount\MountPoint;

class PreviewsMountProviderTest extends \Test\TestCase {

/** @var PreviewsMountPorivder */
protected $provider;

/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
protected $config;

/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
protected $user;

/** @var IStorageFactory|\PHPUnit\Framework\MockObject\MockObject */
protected $loader;

public function setUp() {
parent::setUp();

$this->config = $this->createMock(IConfig::class);
$this->user = $this->createMock(IUser::class);
$this->loader = $this->createMock(IStorageFactory::class);

$this->provider = new PreviewsMountProvider($this->config);
}

public function testConfiguredPreviewsPath() {
$tempFolder = \OC::$server->getTempManager()->getTemporaryFolder();
$this->config->expects($this->once())
->method('getSystemValue')
->with('previews_path', '')
->willReturn($tempFolder . '/thumbnails');

$this->user->expects($this->any())->method('getUID')->willReturn('someuser');

$mounts = $this->provider->getMountsForUser($this->user, $this->loader);

$this->assertCount(1, $mounts);
$mount = $mounts[0];

$this->assertInstanceOf(MountPoint::class, $mount);
$this->assertEquals('/someuser/thumbnails/', $mount->getMountPoint());

$this->assertTrue(\is_dir($tempFolder . '/thumbnails/someuser'));

$storageArgs = ['datadir' => $tempFolder . '/thumbnails/someuser'];

$this->loader->expects($this->once())
->method('getInstance')
->with($mount, '\OC\Files\Storage\Local', $storageArgs);

// trigger storage creation which will pass config args above
$mount->getStorage();

\rmdir($tempFolder . '/thumbnails/someuser');
\rmdir($tempFolder . '/thumbnails');
}
}

0 comments on commit 1e67d80

Please sign in to comment.