Failed Conditions
Pull Request — master (#682)
by
unknown
09:03
created

lib/Service/FileService.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 OCA\Passman\Db\File;
27
use OCA\Passman\Db\FileMapper;
28
use OCP\AppFramework\Db\DoesNotExistException;
29
use OCP\AppFramework\Db\Entity;
30
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
31
use OCP\IConfig;
32
33
34
class FileService {
35
36
	private FileMapper $fileMapper;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
37
	private EncryptService $encryptService;
38
	private $server_key;
39
40
	public function __construct(FileMapper $fileMapper, EncryptService $encryptService, IConfig $config) {
41
		$this->fileMapper = $fileMapper;
42
		$this->encryptService = $encryptService;
43
		$this->server_key = $config->getSystemValue('passwordsalt', '');
44
	}
45
46
	/**
47
	 * Get a single file. This function also returns the file content.
48
	 *
49
	 * @param int $fileId
50
	 * @param string|null $userId
51
	 * @return array|File
52
	 * @throws DoesNotExistException
53
	 * @throws MultipleObjectsReturnedException
54
	 */
55
	public function getFile(int $fileId, string $userId = null) {
56
		$file = $this->fileMapper->getFile($fileId, $userId);
57
		return $this->encryptService->decryptFile($file);
58
	}
59
60
	/**
61
	 * Get a single file. This function also returns the file content.
62
	 *
63
	 * @param string $file_guid
64
	 * @param string|null $userId
65
	 * @return array|File
66
	 * @throws DoesNotExistException
67
	 * @throws MultipleObjectsReturnedException
68
	 */
69
	public function getFileByGuid(string $file_guid, string $userId = null) {
70
		$file = $this->fileMapper->getFileByGuid($file_guid, $userId);
71
		return $this->encryptService->decryptFile($file);
72
	}
73
74
	/**
75
	 * Upload a new file,
76
	 *
77
	 * @param array $file
78
	 * @param string $userId
79
	 * @return array|File
80
	 * @throws DoesNotExistException
81
	 * @throws MultipleObjectsReturnedException
82
	 */
83
	public function createFile(array $file, string $userId) {
84
		$file = $this->encryptService->encryptFile($file);
85
		$file = $this->fileMapper->create($file, $userId);
86
		return $this->getFile($file->getId());
87
	}
88
89
	/**
90
	 * Delete file
91
	 *
92
	 * @param int $file_id
93
	 * @param string $userId
94
	 * @return File|Entity
95
	 */
96
	public function deleteFile(int $file_id, string $userId) {
97
		return $this->fileMapper->deleteFile($file_id, $userId);
98
	}
99
100
	/**
101
	 * Update file
102
	 *
103
	 * @param File $file
104
	 * @return File
105
	 */
106
	public function updateFile(File $file) {
107
		$file = $this->encryptService->encryptFile($file);
108
		return $this->fileMapper->updateFile($file);
109
	}
110
111
	/**
112
	 * Update file
113
	 *
114
	 * @param string $userId
115
	 * @return File[]
116
	 */
117
	public function getFilesFromUser(string $userId) {
118
		$files = $this->fileMapper->getFilesFromUser($userId);
119
		$results = array();
120
		foreach ($files as $file) {
121
			array_push($results, $this->encryptService->decryptFile($file));
122
		}
123
		return $results;
124
	}
125
}
126