Skip to content

Commit b1690b2

Browse files
committed
Move dav property deletion to dedicated service to cleanup on background job
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 4f71915 commit b1690b2

File tree

9 files changed

+98
-10
lines changed

9 files changed

+98
-10
lines changed

apps/dav/composer/composer/autoload_classmap.php

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
'OCA\\DAV\\Search\\EventsSearchProvider' => $baseDir . '/../lib/Search/EventsSearchProvider.php',
249249
'OCA\\DAV\\Search\\TasksSearchProvider' => $baseDir . '/../lib/Search/TasksSearchProvider.php',
250250
'OCA\\DAV\\Server' => $baseDir . '/../lib/Server.php',
251+
'OCA\\DAV\\Service\\CustomPropertiesService' => $baseDir . '/../lib/Service/CustomPropertiesService.php',
251252
'OCA\\DAV\\Settings\\CalDAVSettings' => $baseDir . '/../lib/Settings/CalDAVSettings.php',
252253
'OCA\\DAV\\Storage\\PublicOwnerWrapper' => $baseDir . '/../lib/Storage/PublicOwnerWrapper.php',
253254
'OCA\\DAV\\SystemTag\\SystemTagMappingNode' => $baseDir . '/../lib/SystemTag/SystemTagMappingNode.php',

apps/dav/composer/composer/autoload_static.php

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ class ComposerStaticInitDAV
263263
'OCA\\DAV\\Search\\EventsSearchProvider' => __DIR__ . '/..' . '/../lib/Search/EventsSearchProvider.php',
264264
'OCA\\DAV\\Search\\TasksSearchProvider' => __DIR__ . '/..' . '/../lib/Search/TasksSearchProvider.php',
265265
'OCA\\DAV\\Server' => __DIR__ . '/..' . '/../lib/Server.php',
266+
'OCA\\DAV\\Service\\CustomPropertiesService' => __DIR__ . '/..' . '/../lib/Service/CustomPropertiesService.php',
266267
'OCA\\DAV\\Settings\\CalDAVSettings' => __DIR__ . '/..' . '/../lib/Settings/CalDAVSettings.php',
267268
'OCA\\DAV\\Storage\\PublicOwnerWrapper' => __DIR__ . '/..' . '/../lib/Storage/PublicOwnerWrapper.php',
268269
'OCA\\DAV\\SystemTag\\SystemTagMappingNode' => __DIR__ . '/..' . '/../lib/SystemTag/SystemTagMappingNode.php',

apps/dav/lib/BackgroundJob/UploadCleanup.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
namespace OCA\DAV\BackgroundJob;
3030

3131
use OC\User\NoUserException;
32+
use OCA\DAV\Service\CustomPropertiesService;
3233
use OCP\AppFramework\Utility\ITimeFactory;
3334
use OCP\BackgroundJob\IJobList;
3435
use OCP\BackgroundJob\TimedJob;
@@ -45,10 +46,14 @@ class UploadCleanup extends TimedJob {
4546
/** @var IJobList */
4647
private $jobList;
4748

48-
public function __construct(ITimeFactory $time, IRootFolder $rootFolder, IJobList $jobList) {
49+
/** @var CustomPropertiesService */
50+
private $customPropertiesService;
51+
52+
public function __construct(ITimeFactory $time, IRootFolder $rootFolder, IJobList $jobList, CustomPropertiesService $customPropertiesService) {
4953
parent::__construct($time);
5054
$this->rootFolder = $rootFolder;
5155
$this->jobList = $jobList;
56+
$this->customPropertiesService = $customPropertiesService;
5257

5358
// Run once a day
5459
$this->setInterval(60 * 60 * 24);
@@ -72,6 +77,9 @@ protected function run($argument) {
7277

7378
$files = $uploadFolder->getDirectoryListing();
7479

80+
$davPath = 'uploads/' . $uid . '/' . $uploadFolder->getName();
81+
$this->customPropertiesService->delete($uid, $davPath);
82+
7583
// Remove if all files have an mtime of more than a day
7684
$time = $this->time->getTime() - 60 * 60 * 24;
7785

apps/dav/lib/Connector/Sabre/ServerFactory.php

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OC\Files\Node\Folder;
3636
use OCA\DAV\AppInfo\PluginManager;
3737
use OCA\DAV\Files\BrowserErrorPagePlugin;
38+
use OCA\DAV\Service\CustomPropertiesService;
3839
use OCP\Files\Mount\IMountManager;
3940
use OCP\IConfig;
4041
use OCP\IDBConnection;
@@ -204,6 +205,7 @@ public function createServer($baseUri,
204205
new \OCA\DAV\DAV\CustomPropertiesBackend(
205206
$objectTree,
206207
$this->databaseConnection,
208+
\OC::$server->get(CustomPropertiesService::class),
207209
$this->userSession->getUser()
208210
)
209211
)

apps/dav/lib/DAV/CustomPropertiesBackend.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace OCA\DAV\DAV;
2727

2828
use OCA\DAV\Connector\Sabre\Node;
29+
use OCA\DAV\Service\CustomPropertiesService;
2930
use OCP\IDBConnection;
3031
use OCP\IUser;
3132
use Sabre\DAV\PropertyStorage\Backend\BackendInterface;
@@ -63,6 +64,11 @@ class CustomPropertiesBackend implements BackendInterface {
6364
*/
6465
private $connection;
6566

67+
/**
68+
* @var CustomPropertiesService
69+
*/
70+
private $customPropertiesService;
71+
6672
/**
6773
* @var IUser
6874
*/
@@ -83,9 +89,11 @@ class CustomPropertiesBackend implements BackendInterface {
8389
public function __construct(
8490
Tree $tree,
8591
IDBConnection $connection,
92+
CustomPropertiesService $customPropertiesService,
8693
IUser $user) {
8794
$this->tree = $tree;
8895
$this->connection = $connection;
96+
$this->customPropertiesService = $customPropertiesService;
8997
$this->user = $user;
9098
}
9199

@@ -155,12 +163,7 @@ public function propPatch($path, PropPatch $propPatch) {
155163
* @param string $path path of node for which to delete properties
156164
*/
157165
public function delete($path) {
158-
$statement = $this->connection->prepare(
159-
'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
160-
);
161-
$statement->execute([$this->user->getUID(), $this->formatPath($path)]);
162-
$statement->closeCursor();
163-
166+
$this->customPropertiesService->delete($this->user->getUID(), $path);
164167
unset($this->cache[$path]);
165168
}
166169

apps/dav/lib/Server.php

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
use OCA\DAV\Files\BrowserErrorPagePlugin;
6565
use OCA\DAV\Files\LazySearchBackend;
6666
use OCA\DAV\Provisioning\Apple\AppleProvisioningPlugin;
67+
use OCA\DAV\Service\CustomPropertiesService;
6768
use OCA\DAV\SystemTag\SystemTagPlugin;
6869
use OCA\DAV\Upload\ChunkingPlugin;
6970
use OCA\DAV\Upload\ChunkingV2Plugin;
@@ -250,6 +251,7 @@ public function __construct(IRequest $request, $baseUri) {
250251
new CustomPropertiesBackend(
251252
$this->server->tree,
252253
\OC::$server->getDatabaseConnection(),
254+
\OC::$server->get(CustomPropertiesService::class),
253255
\OC::$server->getUserSession()->getUser()
254256
)
255257
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/*
3+
* @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
4+
*
5+
* @author Julius Härtl <jus@bitgrid.net>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
27+
namespace OCA\DAV\Service;
28+
29+
use OCP\IDBConnection;
30+
31+
class CustomPropertiesService {
32+
33+
/** @var IDBConnection */
34+
private $connection;
35+
36+
public function __construct(IDBConnection $connection) {
37+
$this->connection = $connection;
38+
}
39+
40+
public function delete(string $userId, string $path): void {
41+
$statement = $this->connection->prepare(
42+
'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
43+
);
44+
$result = $statement->execute([$userId, $this->formatPath($path)]);
45+
$result->closeCursor();
46+
}
47+
48+
/**
49+
* long paths are hashed to ensure they fit in the database
50+
*
51+
* @param string $path
52+
* @return string
53+
*/
54+
private function formatPath(string $path): string {
55+
if (strlen($path) > 250) {
56+
return sha1($path);
57+
}
58+
return $path;
59+
}
60+
}

apps/dav/tests/unit/Connector/Sabre/CustomPropertiesBackendTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
use OCA\DAV\Connector\Sabre\Directory;
3939
use OCA\DAV\Connector\Sabre\File;
40+
use OCA\DAV\Service\CustomPropertiesService;
4041
use OCP\IUser;
4142
use Sabre\DAV\Tree;
4243

@@ -88,6 +89,7 @@ protected function setUp(): void {
8889
$this->plugin = new \OCA\DAV\DAV\CustomPropertiesBackend(
8990
$this->tree,
9091
\OC::$server->getDatabaseConnection(),
92+
$this->createMock(CustomPropertiesService::class),
9193
$this->user
9294
);
9395
}

apps/dav/tests/unit/DAV/CustomPropertiesBackendTest.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
namespace OCA\DAV\Tests\DAV;
3030

3131
use OCA\DAV\DAV\CustomPropertiesBackend;
32+
use OCA\DAV\Service\CustomPropertiesService;
3233
use OCP\IDBConnection;
3334
use OCP\IUser;
35+
use PHPUnit\Framework\MockObject\MockObject;
3436
use Sabre\DAV\PropFind;
3537
use Sabre\DAV\PropPatch;
3638
use Sabre\DAV\Tree;
@@ -41,16 +43,19 @@
4143
*/
4244
class CustomPropertiesBackendTest extends TestCase {
4345

44-
/** @var Tree | \PHPUnit\Framework\MockObject\MockObject */
46+
/** @var Tree | MockObject */
4547
private $tree;
4648

4749
/** @var IDBConnection */
4850
private $dbConnection;
4951

50-
/** @var IUser | \PHPUnit\Framework\MockObject\MockObject */
52+
/** @var CustomPropertiesService */
53+
private $customPropertiesService;
54+
55+
/** @var IUser | MockObject */
5156
private $user;
5257

53-
/** @var CustomPropertiesBackend | \PHPUnit\Framework\MockObject\MockObject */
58+
/** @var CustomPropertiesBackend */
5459
private $backend;
5560

5661
protected function setUp(): void {
@@ -62,10 +67,12 @@ protected function setUp(): void {
6267
->with()
6368
->willReturn('dummy_user_42');
6469
$this->dbConnection = \OC::$server->getDatabaseConnection();
70+
$this->customPropertiesService = new CustomPropertiesService($this->dbConnection);
6571

6672
$this->backend = new CustomPropertiesBackend(
6773
$this->tree,
6874
$this->dbConnection,
75+
$this->customPropertiesService,
6976
$this->user
7077
);
7178
}
@@ -123,9 +130,11 @@ protected function getProps(string $user, string $path) {
123130

124131
public function testPropFindNoDbCalls() {
125132
$db = $this->createMock(IDBConnection::class);
133+
$service = new CustomPropertiesService($db);
126134
$backend = new CustomPropertiesBackend(
127135
$this->tree,
128136
$db,
137+
$service,
129138
$this->user
130139
);
131140

0 commit comments

Comments
 (0)