Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Configurable thumbnail path #35196

Merged
merged 2 commits into from
May 10, 2019
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
11 changes: 11 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* @author Vincent Petry <[email protected]>
*
* @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
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 @@ -629,6 +630,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* @author Vincent Petry <[email protected]>
*
* @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
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* @author Vincent Petry <[email protected]>
*
* @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');
}
}