1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Tom Needham <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Files\BackgroundJob; |
23
|
|
|
|
24
|
|
|
use OC\BackgroundJob\QueuedJob; |
25
|
|
|
use OCA\Files\Service\TransferOwnership\TransferOwnershipService; |
26
|
|
|
use OCA\Files\Service\TransferOwnership\TransferRequestManager; |
27
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
28
|
|
|
use OCP\Files\IRootFolder; |
29
|
|
|
use OCP\Files\NotFoundException; |
30
|
|
|
use OCP\ILogger; |
31
|
|
|
use OCP\IUserManager; |
32
|
|
|
|
33
|
|
|
class TransferOwnership extends QueuedJob { |
34
|
|
|
|
35
|
|
|
/** @var ILogger */ |
36
|
|
|
protected $logger; |
37
|
|
|
/** @var TransferOwnershipService */ |
38
|
|
|
protected $service; |
39
|
|
|
/** @var IUserManager */ |
40
|
|
|
protected $userManager; |
41
|
|
|
/** @var TransferRequestManager */ |
42
|
|
|
protected $requestManager; |
43
|
|
|
/** @var IRootFolder */ |
44
|
|
|
protected $rootFolder; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
TransferOwnershipService $service, |
48
|
|
|
IUserManager $userManager, |
49
|
|
|
TransferRequestManager $requestManager, |
50
|
|
|
IRootFolder $rootFolder) { |
51
|
|
|
$this->service = $service; |
52
|
|
|
$this->userManager = $userManager; |
53
|
|
|
$this->requestManager = $requestManager; |
54
|
|
|
$this->rootFolder = $rootFolder; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function execute($jobList, ILogger $logger = null) { |
58
|
|
|
$this->logger = $logger; |
59
|
|
|
parent::execute($jobList, $logger); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function run($argument) { |
63
|
|
|
// Get the arguments; |
64
|
|
|
$arguments = json_decode($argument); |
65
|
|
|
try { |
66
|
|
|
$request = $this->requestManager->getRequestById($arguments->requestId); |
67
|
|
|
} catch (DoesNotExistException $e) { |
68
|
|
|
// Can happen if this gets deleted before the cron runs |
69
|
|
|
$this->logger->info("Transfer job called but request is missing"); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Call the transfer service |
74
|
|
|
$sourceUser = $this->userManager->get($request->getSourceUserId()); |
|
|
|
|
75
|
|
|
$destinationUser = $this->userManager->get($request->getDestinationUserId()); |
|
|
|
|
76
|
|
|
|
77
|
|
|
if ($sourceUser === null || $destinationUser === null) { |
78
|
|
|
$this->logger->error("Trasnfer job called but at least one of the users in the request are missing: source:{$request->getSourceUserId()} destination:{$request->getDestinationUserId()}", ['app' => 'files']); |
|
|
|
|
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
$sourcePath = $this->rootFolder->getUserFolder($request->getSourceUserId()) |
|
|
|
|
84
|
|
|
->getById($request->getFileId())[0] |
|
|
|
|
85
|
|
|
->getInternalPath(); |
86
|
|
|
} catch (NotFoundException $e) { |
87
|
|
|
$this->logger->error("Transfer job called but node no longer exists"); |
88
|
|
|
$this->requestManager->deleteRequest($request); |
|
|
|
|
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
$this->service->transfer($sourceUser, $destinationUser, $sourcePath); |
95
|
|
|
} catch (\Exception $e) { |
96
|
|
|
$this->logger->logException($e, ['app' => 'files']); |
97
|
|
|
$this->requestManager->actionRequestFailure($request); |
|
|
|
|
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->logger->info("Finished transfer of $sourcePath from {$sourceUser->getUID()} to {$destinationUser->getUID()}"); |
102
|
|
|
$this->requestManager->actionRequest($request); |
|
|
|
|
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.