|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Nextcloud - passman |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, Sander Brand ([email protected]) |
|
6
|
|
|
* @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected]) |
|
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
|
|
|
namespace OCA\Passman\Service; |
|
25
|
|
|
|
|
26
|
|
|
use Exception; |
|
27
|
|
|
use OCA\Passman\Db\File; |
|
28
|
|
|
use OCA\Passman\Db\FileMapper; |
|
29
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
30
|
|
|
use OCP\AppFramework\Db\Entity; |
|
31
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException; |
|
32
|
|
|
use OCP\IConfig; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
class FileService { |
|
36
|
|
|
|
|
37
|
|
|
private FileMapper $fileMapper; |
|
|
|
|
|
|
38
|
|
|
private EncryptService $encryptService; |
|
39
|
|
|
private $server_key; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct(FileMapper $fileMapper, EncryptService $encryptService, IConfig $config) { |
|
42
|
|
|
$this->fileMapper = $fileMapper; |
|
43
|
|
|
$this->encryptService = $encryptService; |
|
44
|
|
|
$this->server_key = $config->getSystemValue('passwordsalt', ''); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get a single file. This function also returns the file content. |
|
49
|
|
|
* |
|
50
|
|
|
* @param int $fileId |
|
51
|
|
|
* @param string|null $userId |
|
52
|
|
|
* @return array|File |
|
53
|
|
|
* @throws DoesNotExistException |
|
54
|
|
|
* @throws MultipleObjectsReturnedException |
|
55
|
|
|
* @throws Exception |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getFile(int $fileId, string $userId = null) { |
|
58
|
|
|
$file = $this->fileMapper->getFile($fileId, $userId); |
|
59
|
|
|
return $this->encryptService->decryptFile($file); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get a single file. This function also returns the file content. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $file_guid |
|
66
|
|
|
* @param string|null $userId |
|
67
|
|
|
* @return array|File |
|
68
|
|
|
* @throws DoesNotExistException |
|
69
|
|
|
* @throws MultipleObjectsReturnedException |
|
70
|
|
|
* @throws Exception |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getFileByGuid(string $file_guid, string $userId = null) { |
|
73
|
|
|
$file = $this->fileMapper->getFileByGuid($file_guid, $userId); |
|
74
|
|
|
return $this->encryptService->decryptFile($file); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Upload a new file, |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $file |
|
81
|
|
|
* @param string $userId |
|
82
|
|
|
* @return array|File |
|
83
|
|
|
* @throws DoesNotExistException |
|
84
|
|
|
* @throws MultipleObjectsReturnedException |
|
85
|
|
|
* @throws Exception |
|
86
|
|
|
*/ |
|
87
|
|
|
public function createFile(array $file, string $userId) { |
|
88
|
|
|
$file = $this->encryptService->encryptFile($file); |
|
89
|
|
|
$file = $this->fileMapper->create($file, $userId); |
|
90
|
|
|
return $this->getFile($file->getId()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Delete file |
|
95
|
|
|
* |
|
96
|
|
|
* @param int $file_id |
|
97
|
|
|
* @param string $userId |
|
98
|
|
|
* @return File|Entity |
|
99
|
|
|
*/ |
|
100
|
|
|
public function deleteFile(int $file_id, string $userId) { |
|
101
|
|
|
return $this->fileMapper->deleteFile($file_id, $userId); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Update file |
|
106
|
|
|
* |
|
107
|
|
|
* @param File $file |
|
108
|
|
|
* @return File |
|
109
|
|
|
* @throws Exception |
|
110
|
|
|
*/ |
|
111
|
|
|
public function updateFile(File $file) { |
|
112
|
|
|
$file = $this->encryptService->encryptFile($file); |
|
113
|
|
|
return $this->fileMapper->updateFile($file); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Update file |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $userId |
|
120
|
|
|
* @return File[] |
|
121
|
|
|
* @throws Exception |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getFilesFromUser(string $userId) { |
|
124
|
|
|
$files = $this->fileMapper->getFilesFromUser($userId); |
|
125
|
|
|
$results = array(); |
|
126
|
|
|
foreach ($files as $file) { |
|
127
|
|
|
array_push($results, $this->encryptService->decryptFile($file)); |
|
128
|
|
|
} |
|
129
|
|
|
return $results; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|