2012-04-20 13:39:30 +04:00
|
|
|
<?php
|
2015-03-26 13:44:34 +03:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Felix Moeller <mail@felixmoeller.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author scambra <sergio@entrecables.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
namespace OCA\DAV\Connector\Sabre;
|
2020-04-09 12:48:10 +03:00
|
|
|
|
2017-09-13 14:12:16 +03:00
|
|
|
use OCA\DAV\Upload\FutureFile;
|
2016-10-13 13:59:10 +03:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
|
|
|
use Sabre\DAV\Exception\InsufficientStorage;
|
|
|
|
use Sabre\DAV\Exception\ServiceUnavailable;
|
2017-09-13 14:12:16 +03:00
|
|
|
use Sabre\DAV\INode;
|
2012-04-20 13:39:30 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This plugin check user quota and deny creating files when they exceeds the quota.
|
2012-11-05 01:16:04 +04:00
|
|
|
*
|
2012-04-20 13:39:30 +04:00
|
|
|
* @author Sergio Cambra
|
2013-08-08 15:33:00 +04:00
|
|
|
* @copyright Copyright (C) 2012 entreCables S.L. All rights reserved.
|
2012-04-20 13:39:30 +04:00
|
|
|
* @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
|
|
|
|
*/
|
2015-02-12 14:29:01 +03:00
|
|
|
class QuotaPlugin extends \Sabre\DAV\ServerPlugin {
|
2012-04-20 13:39:30 +04:00
|
|
|
|
2017-09-13 14:12:16 +03:00
|
|
|
/** @var \OC\Files\View */
|
2014-02-25 19:23:09 +04:00
|
|
|
private $view;
|
|
|
|
|
2012-04-20 13:39:30 +04:00
|
|
|
/**
|
2013-08-08 15:33:00 +04:00
|
|
|
* Reference to main server object
|
|
|
|
*
|
2014-01-09 17:25:48 +04:00
|
|
|
* @var \Sabre\DAV\Server
|
2013-08-08 15:33:00 +04:00
|
|
|
*/
|
2012-04-20 13:39:30 +04:00
|
|
|
private $server;
|
|
|
|
|
|
|
|
/**
|
2014-02-25 19:23:09 +04:00
|
|
|
* @param \OC\Files\View $view
|
2013-08-08 15:33:00 +04:00
|
|
|
*/
|
2014-02-25 19:23:09 +04:00
|
|
|
public function __construct($view) {
|
|
|
|
$this->view = $view;
|
|
|
|
}
|
2013-08-08 15:33:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This initializes the plugin.
|
|
|
|
*
|
2014-01-09 17:25:48 +04:00
|
|
|
* This function is called by \Sabre\DAV\Server, after
|
2013-08-08 15:33:00 +04:00
|
|
|
* addPlugin is called.
|
|
|
|
*
|
|
|
|
* This method should set up the requires event subscriptions.
|
|
|
|
*
|
2014-01-09 17:25:48 +04:00
|
|
|
* @param \Sabre\DAV\Server $server
|
2013-08-08 15:33:00 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
2014-01-09 17:25:48 +04:00
|
|
|
public function initialize(\Sabre\DAV\Server $server) {
|
2013-08-08 15:33:00 +04:00
|
|
|
$this->server = $server;
|
2012-04-20 13:39:30 +04:00
|
|
|
|
2017-09-13 14:12:16 +03:00
|
|
|
$server->on('beforeWriteContent', [$this, 'beforeWriteContent'], 10);
|
|
|
|
$server->on('beforeCreateFile', [$this, 'beforeCreateFile'], 10);
|
|
|
|
$server->on('beforeMove', [$this, 'beforeMove'], 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check quota before creating file
|
|
|
|
*
|
|
|
|
* @param string $uri target file URI
|
|
|
|
* @param resource $data data
|
|
|
|
* @param INode $parent Sabre Node
|
|
|
|
* @param bool $modified modified
|
|
|
|
*/
|
|
|
|
public function beforeCreateFile($uri, $data, INode $parent, $modified) {
|
|
|
|
if (!$parent instanceof Node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->checkQuota($parent->getPath() . '/' . basename($uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check quota before writing content
|
|
|
|
*
|
|
|
|
* @param string $uri target file URI
|
|
|
|
* @param INode $node Sabre Node
|
|
|
|
* @param resource $data data
|
|
|
|
* @param bool $modified modified
|
|
|
|
*/
|
|
|
|
public function beforeWriteContent($uri, INode $node, $data, $modified) {
|
|
|
|
if (!$node instanceof Node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->checkQuota($node->getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we're moving a Futurefile in which case we need to check
|
|
|
|
* the quota on the target destination.
|
|
|
|
*
|
|
|
|
* @param string $source source path
|
|
|
|
* @param string $destination destination path
|
|
|
|
*/
|
|
|
|
public function beforeMove($source, $destination) {
|
|
|
|
$sourceNode = $this->server->tree->getNodeForPath($source);
|
|
|
|
if (!$sourceNode instanceof FutureFile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get target node for proper path conversion
|
|
|
|
if ($this->server->tree->nodeExists($destination)) {
|
|
|
|
$destinationNode = $this->server->tree->getNodeForPath($destination);
|
|
|
|
$path = $destinationNode->getPath();
|
|
|
|
} else {
|
|
|
|
$parentNode = $this->server->tree->getNodeForPath(dirname($destination));
|
|
|
|
$path = $parentNode->getPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->checkQuota($path, $sourceNode->getSize());
|
2012-04-20 13:39:30 +04:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:12:16 +03:00
|
|
|
|
2012-04-20 13:39:30 +04:00
|
|
|
/**
|
2013-08-08 15:33:00 +04:00
|
|
|
* This method is called before any HTTP method and validates there is enough free space to store the file
|
|
|
|
*
|
2017-09-13 14:12:16 +03:00
|
|
|
* @param string $path relative to the users home
|
|
|
|
* @param int $length
|
2016-10-13 13:59:10 +03:00
|
|
|
* @throws InsufficientStorage
|
2013-08-08 15:33:00 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-09-13 14:12:16 +03:00
|
|
|
public function checkQuota($path, $length = null) {
|
|
|
|
if ($length === null) {
|
|
|
|
$length = $this->getLength();
|
|
|
|
}
|
|
|
|
|
2012-04-20 13:39:30 +04:00
|
|
|
if ($length) {
|
2017-09-13 14:12:16 +03:00
|
|
|
list($parentPath, $newName) = \Sabre\Uri\split($path);
|
2020-04-10 15:19:56 +03:00
|
|
|
if (is_null($parentPath)) {
|
2017-09-13 14:12:16 +03:00
|
|
|
$parentPath = '';
|
2015-05-12 15:02:27 +03:00
|
|
|
}
|
2014-03-12 19:57:39 +04:00
|
|
|
$req = $this->server->httpRequest;
|
|
|
|
if ($req->getHeader('OC-Chunked')) {
|
2015-02-12 14:29:01 +03:00
|
|
|
$info = \OC_FileChunking::decodeName($newName);
|
2016-01-11 14:38:13 +03:00
|
|
|
$chunkHandler = $this->getFileChunking($info);
|
2014-04-23 17:34:04 +04:00
|
|
|
// subtract the already uploaded size to see whether
|
2014-03-12 19:57:39 +04:00
|
|
|
// there is still enough space for the remaining chunks
|
|
|
|
$length -= $chunkHandler->getCurrentSize();
|
2016-01-11 14:38:13 +03:00
|
|
|
// use target file name for free space check in case of shared files
|
2017-09-13 14:12:16 +03:00
|
|
|
$path = rtrim($parentPath, '/') . '/' . $info['name'];
|
2014-03-12 19:57:39 +04:00
|
|
|
}
|
2017-09-13 14:12:16 +03:00
|
|
|
$freeSpace = $this->getFreeSpace($path);
|
2018-04-13 12:52:01 +03:00
|
|
|
if ($freeSpace >= 0 && $length > $freeSpace) {
|
2014-03-12 19:57:39 +04:00
|
|
|
if (isset($chunkHandler)) {
|
|
|
|
$chunkHandler->cleanup();
|
|
|
|
}
|
2018-04-13 12:52:01 +03:00
|
|
|
throw new InsufficientStorage("Insufficient space in $path, $length required, $freeSpace available");
|
2012-04-20 13:39:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-08 02:42:28 +04:00
|
|
|
|
2016-01-11 14:38:13 +03:00
|
|
|
public function getFileChunking($info) {
|
|
|
|
// FIXME: need a factory for better mocking support
|
|
|
|
return new \OC_FileChunking($info);
|
|
|
|
}
|
|
|
|
|
2014-02-25 19:23:09 +04:00
|
|
|
public function getLength() {
|
2013-08-08 15:33:00 +04:00
|
|
|
$req = $this->server->httpRequest;
|
|
|
|
$length = $req->getHeader('X-Expected-Entity-Length');
|
2016-10-14 10:09:21 +03:00
|
|
|
if (!is_numeric($length)) {
|
2013-08-08 15:33:00 +04:00
|
|
|
$length = $req->getHeader('Content-Length');
|
2016-10-14 10:09:21 +03:00
|
|
|
$length = is_numeric($length) ? $length : null;
|
2013-08-08 15:33:00 +04:00
|
|
|
}
|
2013-08-08 02:42:28 +04:00
|
|
|
|
2013-08-08 15:33:00 +04:00
|
|
|
$ocLength = $req->getHeader('OC-Total-Length');
|
2016-10-14 10:09:21 +03:00
|
|
|
if (is_numeric($length) && is_numeric($ocLength)) {
|
2013-08-08 02:42:28 +04:00
|
|
|
return max($length, $ocLength);
|
2013-08-08 15:33:00 +04:00
|
|
|
}
|
2013-08-08 02:42:28 +04:00
|
|
|
|
|
|
|
return $length;
|
|
|
|
}
|
2013-08-26 22:21:16 +04:00
|
|
|
|
|
|
|
/**
|
2016-01-11 14:38:13 +03:00
|
|
|
* @param string $uri
|
2013-08-26 22:21:16 +04:00
|
|
|
* @return mixed
|
2016-10-13 13:59:10 +03:00
|
|
|
* @throws ServiceUnavailable
|
2013-08-26 22:21:16 +04:00
|
|
|
*/
|
2016-01-11 14:38:13 +03:00
|
|
|
public function getFreeSpace($uri) {
|
2014-11-06 12:59:36 +03:00
|
|
|
try {
|
2016-01-11 14:38:13 +03:00
|
|
|
$freeSpace = $this->view->free_space(ltrim($uri, '/'));
|
2014-11-06 12:59:36 +03:00
|
|
|
return $freeSpace;
|
2016-10-13 13:59:10 +03:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
throw new ServiceUnavailable($e->getMessage());
|
2014-11-06 12:59:36 +03:00
|
|
|
}
|
2013-08-26 22:21:16 +04:00
|
|
|
}
|
2012-04-20 13:39:30 +04:00
|
|
|
}
|