summaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/Upload
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2018-11-02 21:19:27 +0100
committerRoeland Jago Douma <roeland@famdouma.nl>2019-01-04 09:21:21 +0100
commitcb742e7045d1f5af7994ad1e918b0ddc9ba01914 (patch)
tree7d1681c35780cc4b4ca4283d16845ea74ad61dac /apps/dav/lib/Upload
parenta1f9ed1d7d9e953833850a14405687e115bf0b82 (diff)
downloadnextcloud-server-cb742e7045d1f5af7994ad1e918b0ddc9ba01914.tar.gz
nextcloud-server-cb742e7045d1f5af7994ad1e918b0ddc9ba01914.zip
Background job to cleanup leftover chunked uploads
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps/dav/lib/Upload')
-rw-r--r--apps/dav/lib/Upload/CleanupService.php49
-rw-r--r--apps/dav/lib/Upload/RootCollection.php18
-rw-r--r--apps/dav/lib/Upload/UploadFolder.php11
-rw-r--r--apps/dav/lib/Upload/UploadHome.php39
4 files changed, 96 insertions, 21 deletions
diff --git a/apps/dav/lib/Upload/CleanupService.php b/apps/dav/lib/Upload/CleanupService.php
new file mode 100644
index 00000000000..9e70a1cff90
--- /dev/null
+++ b/apps/dav/lib/Upload/CleanupService.php
@@ -0,0 +1,49 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\DAV\Upload;
+
+use OCA\DAV\BackgroundJob\UploadCleanup;
+use OCP\BackgroundJob\IJobList;
+use OCP\IUserSession;
+
+class CleanupService {
+ /** @var IUserSession */
+ private $userSession;
+ /** @var IJobList */
+ private $jobList;
+
+ public function __construct(IUserSession $userSession, IJobList $jobList) {
+ $this->userSession = $userSession;
+ $this->jobList = $jobList;
+ }
+
+ public function addJob(string $folder) {
+ $this->jobList->add(UploadCleanup::class, ['uid' => $this->userSession->getUser()->getUID(), 'folder' => $folder]);
+ }
+
+ public function removeJob(string $folder) {
+ $this->jobList->remove(UploadCleanup::class, ['uid' => $this->userSession->getUser()->getUID(), 'folder' => $folder]);
+ }
+}
diff --git a/apps/dav/lib/Upload/RootCollection.php b/apps/dav/lib/Upload/RootCollection.php
index 696de064dd8..436792e25ec 100644
--- a/apps/dav/lib/Upload/RootCollection.php
+++ b/apps/dav/lib/Upload/RootCollection.php
@@ -1,4 +1,5 @@
<?php
+declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@@ -23,20 +24,31 @@
namespace OCA\DAV\Upload;
use Sabre\DAVACL\AbstractPrincipalCollection;
+use Sabre\DAVACL\PrincipalBackend;
class RootCollection extends AbstractPrincipalCollection {
+ /** @var CleanupService */
+ private $cleanupService;
+
+ public function __construct(PrincipalBackend\BackendInterface $principalBackend,
+ string $principalPrefix,
+ CleanupService $cleanupService) {
+ parent::__construct($principalBackend, $principalPrefix);
+ $this->cleanupService = $cleanupService;
+ }
+
/**
* @inheritdoc
*/
- function getChildForPrincipal(array $principalInfo) {
- return new UploadHome($principalInfo);
+ public function getChildForPrincipal(array $principalInfo): UploadHome {
+ return new UploadHome($principalInfo, $this->cleanupService);
}
/**
* @inheritdoc
*/
- function getName() {
+ public function getName(): string {
return 'uploads';
}
diff --git a/apps/dav/lib/Upload/UploadFolder.php b/apps/dav/lib/Upload/UploadFolder.php
index b3d43fef95d..233c3ac3e52 100644
--- a/apps/dav/lib/Upload/UploadFolder.php
+++ b/apps/dav/lib/Upload/UploadFolder.php
@@ -22,16 +22,22 @@
*/
namespace OCA\DAV\Upload;
+use OCA\DAV\BackgroundJob\UploadCleanup;
use OCA\DAV\Connector\Sabre\Directory;
+use OCP\BackgroundJob\IJobList;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\ICollection;
class UploadFolder implements ICollection {
+ /** @var Directory */
private $node;
+ /** @var CleanupService */
+ private $cleanupService;
- function __construct(Directory $node) {
+ function __construct(Directory $node, CleanupService $cleanupService) {
$this->node = $node;
+ $this->cleanupService = $cleanupService;
}
function createFile($name, $data = null) {
@@ -65,6 +71,9 @@ class UploadFolder implements ICollection {
function delete() {
$this->node->delete();
+
+ // Background cleanup job is not needed anymore
+ $this->cleanupService->removeJob($this->getName());
}
function getName() {
diff --git a/apps/dav/lib/Upload/UploadHome.php b/apps/dav/lib/Upload/UploadHome.php
index 6976ece9943..fcfd978b33d 100644
--- a/apps/dav/lib/Upload/UploadHome.php
+++ b/apps/dav/lib/Upload/UploadHome.php
@@ -30,51 +30,56 @@ use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\ICollection;
class UploadHome implements ICollection {
- /**
- * UploadHome constructor.
- *
- * @param array $principalInfo
- */
- public function __construct($principalInfo) {
+
+ /** @var array */
+ private $principalInfo;
+ /** @var CleanupService */
+ private $cleanupService;
+
+ public function __construct(array $principalInfo, CleanupService $cleanupService) {
$this->principalInfo = $principalInfo;
+ $this->cleanupService = $cleanupService;
}
- function createFile($name, $data = null) {
+ public function createFile($name, $data = null) {
throw new Forbidden('Permission denied to create file (filename ' . $name . ')');
}
- function createDirectory($name) {
+ public function createDirectory($name) {
$this->impl()->createDirectory($name);
+
+ // Add a cleanup job
+ $this->cleanupService->addJob($name);
}
- function getChild($name) {
- return new UploadFolder($this->impl()->getChild($name));
+ public function getChild($name): UploadFolder {
+ return new UploadFolder($this->impl()->getChild($name), $this->cleanupService);
}
- function getChildren() {
+ public function getChildren(): array {
return array_map(function($node) {
- return new UploadFolder($node);
+ return new UploadFolder($node, $this->cleanupService);
}, $this->impl()->getChildren());
}
- function childExists($name) {
+ public function childExists($name): bool {
return !is_null($this->getChild($name));
}
- function delete() {
+ public function delete() {
$this->impl()->delete();
}
- function getName() {
+ public function getName() {
list(,$name) = \Sabre\Uri\split($this->principalInfo['uri']);
return $name;
}
- function setName($name) {
+ public function setName($name) {
throw new Forbidden('Permission denied to rename this folder');
}
- function getLastModified() {
+ public function getLastModified() {
return $this->impl()->getLastModified();
}