Merge pull request #33697 from nextcloud/fix/improve-bulk-upload

Improve bulk upload and add a config switch for it
This commit is contained in:
Côme Chilliet 2022-08-30 12:22:20 +02:00 committed by GitHub
commit 66648011c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 152 additions and 48 deletions

View file

@ -3,6 +3,7 @@
* @copyright Copyright (c) 2021, Louis Chemineau <louis@chmn.me>
*
* @author Louis Chemineau <louis@chmn.me>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license AGPL-3.0
*
@ -27,19 +28,19 @@ use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use OCP\Files\DavUtil;
use OCP\Files\Folder;
use OCP\AppFramework\Http;
use OCA\DAV\Connector\Sabre\MtimeSanitizer;
class BulkUploadPlugin extends ServerPlugin {
private Folder $userFolder;
private LoggerInterface $logger;
/** @var Folder */
private $userFolder;
/** @var LoggerInterface */
private $logger;
public function __construct(Folder $userFolder, LoggerInterface $logger) {
public function __construct(
Folder $userFolder,
LoggerInterface $logger
) {
$this->userFolder = $userFolder;
$this->logger = $logger;
}
@ -95,6 +96,8 @@ class BulkUploadPlugin extends ServerPlugin {
$writtenFiles[$headers['x-file-path']] = [
"error" => false,
"etag" => $node->getETag(),
"fileid" => DavUtil::getDavFileId($node->getId()),
"permissions" => DavUtil::getDavPermissions($node),
];
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['path' => $headers['x-file-path']]);

View file

@ -4,6 +4,7 @@
*
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Louis Chemineau <louis@chmn.me>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license AGPL-3.0
*
@ -23,15 +24,24 @@
namespace OCA\DAV;
use OCP\Capabilities\ICapability;
use OCP\IConfig;
class Capabilities implements ICapability {
private IConfig $config;
public function __construct(IConfig $config) {
$this->config = $config;
}
public function getCapabilities() {
return [
$capabilities = [
'dav' => [
'chunking' => '1.0',
// disabled because of https://github.com/nextcloud/desktop/issues/4243
// 'bulkupload' => '1.0',
]
];
if ($this->config->getSystemValueBool('bulkupload.enabled', true)) {
$capabilities['dav']['bulkupload'] = '1.0';
}
return $capabilities;
}
}

View file

@ -38,9 +38,9 @@ namespace OCA\DAV\Connector\Sabre;
use OC\Files\Mount\MoveableMount;
use OC\Files\Node\File;
use OC\Files\Node\Folder;
use OC\Files\Storage\Wrapper\Wrapper;
use OC\Files\View;
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
use OCP\Files\DavUtil;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Files\StorageNotAvailableException;
@ -252,10 +252,8 @@ abstract class Node implements \Sabre\DAV\INode {
* @return string|null
*/
public function getFileId() {
if ($this->info->getId()) {
$instanceId = \OC_Util::getInstanceId();
$id = sprintf('%08d', $this->info->getId());
return $id . $instanceId;
if ($id = $this->info->getId()) {
return DavUtil::getDavFileId($id);
}
return null;
@ -381,35 +379,7 @@ abstract class Node implements \Sabre\DAV\INode {
* @return string
*/
public function getDavPermissions() {
$p = '';
if ($this->info->isShared()) {
$p .= 'S';
}
if ($this->info->isShareable()) {
$p .= 'R';
}
if ($this->info->isMounted()) {
$p .= 'M';
}
if ($this->info->isReadable()) {
$p .= 'G';
}
if ($this->info->isDeletable()) {
$p .= 'D';
}
if ($this->info->isUpdateable()) {
$p .= 'NV'; // Renameable, Moveable
}
if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
if ($this->info->isUpdateable()) {
$p .= 'W';
}
} else {
if ($this->info->isCreatable()) {
$p .= 'CK';
}
}
return $p;
return DavUtil::getDavPermissions($this->info);
}
public function getOwner() {

View file

@ -313,7 +313,10 @@ class Server {
$view
));
$this->server->addPlugin(
new BulkUploadPlugin($userFolder, $logger)
new BulkUploadPlugin(
$userFolder,
$logger
)
);
}
$this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(

View file

@ -24,6 +24,7 @@
namespace OCA\DAV\Tests\unit;
use OCA\DAV\Capabilities;
use OCP\IConfig;
use Test\TestCase;
/**
@ -31,12 +32,31 @@ use Test\TestCase;
*/
class CapabilitiesTest extends TestCase {
public function testGetCapabilities() {
$capabilities = new Capabilities();
$config = $this->createMock(IConfig::class);
$config->expects($this->once())
->method('getSystemValueBool')
->with('bulkupload.enabled', $this->isType('bool'))
->willReturn(false);
$capabilities = new Capabilities($config);
$expected = [
'dav' => [
'chunking' => '1.0',
// disabled because of https://github.com/nextcloud/desktop/issues/4243
// 'bulkupload' => '1.0',
],
];
$this->assertSame($expected, $capabilities->getCapabilities());
}
public function testGetCapabilitiesWithBulkUpload() {
$config = $this->createMock(IConfig::class);
$config->expects($this->once())
->method('getSystemValueBool')
->with('bulkupload.enabled', $this->isType('bool'))
->willReturn(true);
$capabilities = new Capabilities($config);
$expected = [
'dav' => [
'chunking' => '1.0',
'bulkupload' => '1.0',
],
];
$this->assertSame($expected, $capabilities->getCapabilities());

View file

@ -2238,4 +2238,11 @@ $CONFIG = [
* Defaults to ``false``
*/
'projects.enabled' => false,
/**
* Enable the bulk upload feature.
*
* Defaults to ``true``
*/
'bulkupload.enabled' => true,
];

View file

@ -262,6 +262,7 @@ return array(
'OCP\\Files\\Config\\IMountProviderCollection' => $baseDir . '/lib/public/Files/Config/IMountProviderCollection.php',
'OCP\\Files\\Config\\IRootMountProvider' => $baseDir . '/lib/public/Files/Config/IRootMountProvider.php',
'OCP\\Files\\Config\\IUserMountCache' => $baseDir . '/lib/public/Files/Config/IUserMountCache.php',
'OCP\\Files\\DavUtil' => $baseDir . '/lib/public/Files/DavUtil.php',
'OCP\\Files\\EmptyFileNameException' => $baseDir . '/lib/public/Files/EmptyFileNameException.php',
'OCP\\Files\\EntityTooLargeException' => $baseDir . '/lib/public/Files/EntityTooLargeException.php',
'OCP\\Files\\Events\\BeforeDirectFileDownloadEvent' => $baseDir . '/lib/public/Files/Events/BeforeDirectFileDownloadEvent.php',

View file

@ -295,6 +295,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Files\\Config\\IMountProviderCollection' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IMountProviderCollection.php',
'OCP\\Files\\Config\\IRootMountProvider' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IRootMountProvider.php',
'OCP\\Files\\Config\\IUserMountCache' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IUserMountCache.php',
'OCP\\Files\\DavUtil' => __DIR__ . '/../../..' . '/lib/public/Files/DavUtil.php',
'OCP\\Files\\EmptyFileNameException' => __DIR__ . '/../../..' . '/lib/public/Files/EmptyFileNameException.php',
'OCP\\Files\\EntityTooLargeException' => __DIR__ . '/../../..' . '/lib/public/Files/EntityTooLargeException.php',
'OCP\\Files\\Events\\BeforeDirectFileDownloadEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/BeforeDirectFileDownloadEvent.php',

View file

@ -0,0 +1,89 @@
<?php
/**
* @copyright Copyright (c) 2022 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Arthur Schiwon <blizzz@owncloud.com>
* @author Bart Visscher <bartv@thisnet.nl>
* @author Jakob Sack <mail@jakobsack.de>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Klaas Freitag <freitag@owncloud.com>
* @author Markus Goetz <markus@woboq.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vincent Petry <pvince81@owncloud.com>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @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 OCP\Files;
/**
* This class provides different helper functions related to WebDAV protocol
*
* @since 25.0.0
*/
class DavUtil {
/**
* Compute the fileId to use for dav responses
*
* @param int $id Id of the file returned by FileInfo::getId
* @since 25.0.0
*/
public static function getDavFileId(int $id): string {
$instanceId = \OC_Util::getInstanceId();
$id = sprintf('%08d', $id);
return $id . $instanceId;
}
/**
* Compute the format needed for returning permissions for dav
*
* @since 25.0.0
*/
public static function getDavPermissions(FileInfo $info): string {
$p = '';
if ($info->isShared()) {
$p .= 'S';
}
if ($info->isShareable()) {
$p .= 'R';
}
if ($info->isMounted()) {
$p .= 'M';
}
if ($info->isReadable()) {
$p .= 'G';
}
if ($info->isDeletable()) {
$p .= 'D';
}
if ($info->isUpdateable()) {
$p .= 'NV'; // Renameable, Moveable
}
if ($info->getType() === FileInfo::TYPE_FILE) {
if ($info->isUpdateable()) {
$p .= 'W';
}
} else {
if ($info->isCreatable()) {
$p .= 'CK';
}
}
return $p;
}
}