1 | <?php |
||
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 |
||
132 |