1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OCA\Files\BackgroundJob; |
4
|
|
|
|
5
|
|
|
use OC\BackgroundJob\QueuedJob; |
6
|
|
|
use OCA\Files\Service\TransferOwnership\TransferOwnershipService; |
7
|
|
|
use OCA\Files\Service\TransferOwnership\TransferRequestManager; |
8
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
9
|
|
|
use OCP\Files\IRootFolder; |
10
|
|
|
use OCP\Files\NotFoundException; |
11
|
|
|
use OCP\ILogger; |
12
|
|
|
use OCP\IUserManager; |
13
|
|
|
|
14
|
|
|
class TransferOwnership extends QueuedJob { |
15
|
|
|
|
16
|
|
|
/** @var ILogger */ |
17
|
|
|
protected $logger; |
18
|
|
|
/** @var TransferOwnershipService */ |
19
|
|
|
protected $service; |
20
|
|
|
/** @var IUserManager */ |
21
|
|
|
protected $userManager; |
22
|
|
|
/** @var TransferRequestManager */ |
23
|
|
|
protected $requestManager; |
24
|
|
|
/** @var IRootFolder */ |
25
|
|
|
protected $rootFolder; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
TransferOwnershipService $service, |
29
|
|
|
IUserManager $userManager, |
30
|
|
|
TransferRequestManager $requestManager, |
31
|
|
|
IRootFolder $rootFolder) { |
32
|
|
|
$this->service = $service; |
33
|
|
|
$this->userManager = $userManager; |
34
|
|
|
$this->requestManager = $requestManager; |
35
|
|
|
$this->rootFolder = $rootFolder; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function execute($jobList, ILogger $logger = null) { |
39
|
|
|
$this->logger = $logger; |
40
|
|
|
parent::execute($jobList, $logger); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function run($argument) { |
44
|
|
|
// Get the arguments; |
45
|
|
|
$arguments = json_decode($argument); |
46
|
|
|
try { |
47
|
|
|
$request = $this->requestManager->getRequestById($arguments->requestId); |
48
|
|
|
} catch (DoesNotExistException $e) { |
49
|
|
|
// Can happen if this gets deleted before the cron runs |
50
|
|
|
$this->logger->info("Transfer job called but request is missing"); |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Call the transfer service |
55
|
|
|
$sourceUser = $this->userManager->get($request->getSourceUserId()); |
|
|
|
|
56
|
|
|
$destinationUser = $this->userManager->get($request->getDestinationUserId()); |
|
|
|
|
57
|
|
|
|
58
|
|
|
if ($sourceUser === null || $destinationUser === null) { |
59
|
|
|
$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']); |
|
|
|
|
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
try { |
64
|
|
|
$sourcePath = $this->rootFolder->getUserFolder($request->getSourceUserId()) |
|
|
|
|
65
|
|
|
->getById($request->getFileId())[0] |
|
|
|
|
66
|
|
|
->getInternalPath(); |
67
|
|
|
} catch (NotFoundException $e) { |
68
|
|
|
$this->logger->error("Transfer job called but node no longer exists"); |
69
|
|
|
$this->requestManager->deleteRequest($request); |
|
|
|
|
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$this->service->transfer($sourceUser, $destinationUser, $sourcePath); |
76
|
|
|
} catch (\Exception $e) { |
77
|
|
|
$this->logger->logException($e, ['app' => 'files']); |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->logger->info("Finished transfer of $sourcePath from {$sourceUser->getUID()} to {$destinationUser->getUID()}"); |
82
|
|
|
$this->requestManager->actionRequest($request); |
|
|
|
|
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
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.