. * */ namespace OCA\Passman\Service; use Exception; use OCA\Passman\Db\File; use OCA\Passman\Db\FileMapper; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\IConfig; class FileService { private $server_key; public function __construct( private FileMapper $fileMapper, private EncryptService $encryptService, IConfig $config, ) { $this->server_key = $config->getSystemValue('passwordsalt', ''); } /** * Get a single file. This function also returns the file content. * * @param int $fileId * @param string|null $userId * @return array|File * @throws DoesNotExistException * @throws MultipleObjectsReturnedException * @throws Exception */ public function getFile(int $fileId, string $userId = null) { $file = $this->fileMapper->getFile($fileId, $userId); return $this->encryptService->decryptFile($file); } /** * Get a single file. This function also returns the file content. * * @param string $file_guid * @param string|null $userId * @return array|File * @throws DoesNotExistException * @throws MultipleObjectsReturnedException * @throws Exception */ public function getFileByGuid(string $file_guid, string $userId = null) { $file = $this->fileMapper->getFileByGuid($file_guid, $userId); return $this->encryptService->decryptFile($file); } /** * Upload a new file, * * @param array $file * @param string $userId * @return array|File * @throws DoesNotExistException * @throws MultipleObjectsReturnedException * @throws Exception */ public function createFile(array $file, string $userId) { $file = $this->encryptService->encryptFile($file); $file = $this->fileMapper->create($file, $userId); return $this->getFile($file->getId()); } /** * Delete file * * @param int $file_id * @param string $userId * @return File|Entity */ public function deleteFile(int $file_id, string $userId) { return $this->fileMapper->deleteFile($file_id, $userId); } /** * Update file * * @param File $file * @return File * @throws Exception */ public function updateFile(File $file) { $file = $this->encryptService->encryptFile($file); return $this->fileMapper->updateFile($file); } /** * Get array of all file guids * * @param string $userId * @return string[] * @throws Exception */ public function getFileGuidsFromUser(string $userId) { $files = $this->fileMapper->getFileGuidsFromUser($userId); $results = []; foreach ($files as $fileGuid) { $results[] = $fileGuid->getGuid(); } return $results; } }