diff --git a/lib/Controller/Text2ImageController.php b/lib/Controller/Text2ImageController.php index 1f49f626..7b655451 100644 --- a/lib/Controller/Text2ImageController.php +++ b/lib/Controller/Text2ImageController.php @@ -79,7 +79,6 @@ public function getPromptHistory(): DataResponse { #[PublicPage] #[BruteForceProtection(action: 'imageGenId')] public function getImage(string $imageGenId, int $fileNameId): DataDisplayResponse | DataResponse { - try { $result = $this->text2ImageHelperService->getImage($imageGenId, $fileNameId); } catch (Exception $e) { @@ -91,9 +90,11 @@ public function getImage(string $imageGenId, int $fileNameId): DataDisplayRespon return $response; } + /* if (isset($result['processing'])) { return new DataResponse($result, Http::STATUS_OK); } + */ return new DataDisplayResponse( $result['image'] ?? '', diff --git a/lib/Cron/CleanupImageGenerations.php b/lib/Cron/CleanupImageGenerations.php index 9c1ea937..b0f44bc4 100644 --- a/lib/Cron/CleanupImageGenerations.php +++ b/lib/Cron/CleanupImageGenerations.php @@ -7,7 +7,6 @@ namespace OCA\TpAssistant\Cron; use Exception; -use OCA\TpAssistant\Db\Text2Image\ImageGenerationMapper; use OCA\TpAssistant\Service\Text2Image\CleanUpService; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; @@ -16,7 +15,6 @@ class CleanupImageGenerations extends TimedJob { public function __construct( ITimeFactory $time, - private ImageGenerationMapper $imageGenerationMapper, private LoggerInterface $logger, private CleanUpService $cleanUpService, ) { @@ -32,8 +30,5 @@ protected function run($argument): void { } catch (Exception $e) { $this->logger->debug('Cleanup job for image generations failed: ' . $e->getMessage()); } - - - return; } } diff --git a/lib/Db/TaskMapper.php b/lib/Db/TaskMapper.php index b4be5035..7573137b 100644 --- a/lib/Db/TaskMapper.php +++ b/lib/Db/TaskMapper.php @@ -8,7 +8,6 @@ namespace OCA\TpAssistant\Db; use DateTime; -use Doctrine\DBAL\Exception\InvalidArgumentException; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\AppFramework\Db\QBMapper; @@ -109,7 +108,7 @@ public function getTasksByOcpTaskIdAndCategory(int $ocpTaskId, int $category): a $task = $this->update($task); } catch (\InvalidArgumentException $e) { // This should never happen - throw new Exception('Failed to touch timestamp of task', 0 , $e); + throw new Exception('Failed to touch timestamp of task', 0, $e); } } unset($task); diff --git a/lib/Db/Text2Image/ImageGeneration.php b/lib/Db/Text2Image/ImageGeneration.php index 59e06707..d0586aa3 100644 --- a/lib/Db/Text2Image/ImageGeneration.php +++ b/lib/Db/Text2Image/ImageGeneration.php @@ -9,16 +9,16 @@ use OCP\AppFramework\Db\Entity; /** - * @method string getImageGenId() - * @method void setImageGenId(string $imageGenId) - * @method string getPrompt() - * @method void setPrompt(string $prompt) - * @method void setUserId(string $userId) - * @method string getUserId() - * @method void setTimestamp(int $timestamp) - * @method int getTimestamp() - * @method void setExpGenTime(int $expGenTime) - * @method int getExpGenTime() + * @method \string getImageGenId() + * @method \void setImageGenId(string $imageGenId) + * @method \string getPrompt() + * @method \void setPrompt(string $prompt) + * @method \void setUserId(string $userId) + * @method \string getUserId() + * @method \void setTimestamp(int $timestamp) + * @method \int getTimestamp() + * @method \void setExpGenTime(int $expGenTime) + * @method \int getExpGenTime() * */ class ImageGeneration extends Entity implements \JsonSerializable { diff --git a/lib/Db/Text2Image/ImageGenerationMapper.php b/lib/Db/Text2Image/ImageGenerationMapper.php index 477a508a..d52fac18 100644 --- a/lib/Db/Text2Image/ImageGenerationMapper.php +++ b/lib/Db/Text2Image/ImageGenerationMapper.php @@ -171,7 +171,7 @@ public function setNotifyReady(string $imageGenId, bool $notifyReady): int { /** * @param int $maxAge - * @return array ('deleted_generations' => int, 'file_names' => string[]) + * @return array{deleted_generations: int, file_names: array} * @throws Exception * @throws \RuntimeException */ @@ -188,11 +188,9 @@ public function cleanupImageGenerations(int $maxAge = Application::DEFAULT_MAX_I $qb->expr()->lt('timestamp', $qb->createNamedParameter($maxTimestamp, IQueryBuilder::PARAM_INT)) ); - /** @var ImageGeneration[] $generations */ $generations = $this->findEntities($qb); $qb->resetQueryParts(); - /** @var array[] $fileNames */ $fileNames = []; $imageGenIds = []; $generationIds = []; diff --git a/lib/Service/AssistantService.php b/lib/Service/AssistantService.php index 23774aad..5fc34220 100644 --- a/lib/Service/AssistantService.php +++ b/lib/Service/AssistantService.php @@ -11,6 +11,7 @@ use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\Common\Exception\NotFoundException; +use OCP\Files\File; use OCP\Files\GenericFileException; use OCP\Files\IRootFolder; use OCP\Files\NotPermittedException; @@ -207,7 +208,7 @@ public function runTextProcessingTask(string $type, array $inputs, string $appId } $assistantTask = $this->taskMapper->createTask($userId, $inputs, $task->getOutput(), time(), $task->getId(), $type, $appId, $task->getStatus(), Application::TASK_GATEGORY_TEXT_GEN, $identifier); - + return $assistantTask; } @@ -242,7 +243,7 @@ public function scheduleTextProcessingTask(string $type, array $inputs, string $ } $assistantTask = $this->taskMapper->createTask($userId, $inputs, $task->getOutput(), time(), $task->getId(), $type, $appId, $task->getStatus(), Application::TASK_GATEGORY_TEXT_GEN, $identifier); - + return $assistantTask; } @@ -278,7 +279,7 @@ public function runOrScheduleTextProcessingTask(string $type, array $inputs, str } $assistantTask = $this->taskMapper->createTask($userId, $inputs, $task->getOutput(), time(), $task->getId(), $type, $appId, $task->getStatus(), Application::TASK_GATEGORY_TEXT_GEN, $identifier); - + return $assistantTask; } @@ -294,7 +295,7 @@ public function parseTextFromFile(string $filePath): string { } catch (\OC\User\NoUserException | NotPermittedException $e) { throw new \Exception('Could not access user storage.'); } - + try { $mimeType = $userFolder->get($filePath)->getMimeType(); @@ -303,17 +304,22 @@ public function parseTextFromFile(string $filePath): string { } try { - $contents = $userFolder->get($filePath)->getContent(); + $file = $userFolder->get($filePath); + if ($file instanceof File) { + $contents = $file->getContent(); + } else { + throw new \Exception('File is not a file.'); + } } catch (NotFoundException | LockedException | GenericFileException | NotPermittedException $e) { throw new \Exception('File not found or could not be accessed.'); } - + switch ($mimeType) { default: case 'text/plain': { $text = $contents; - + break; } case 'text/markdown': @@ -338,7 +344,7 @@ public function parseTextFromFile(string $filePath): string { file_put_contents($tempFilePath, $contents); $text = $this->parseDocument($tempFilePath, $mimeType); - + // Remove the hardlink to the file (delete it): unlink($tempFilePath); diff --git a/lib/Service/Text2Image/CleanUpService.php b/lib/Service/Text2Image/CleanUpService.php index c54b1d63..cff5fd2d 100644 --- a/lib/Service/Text2Image/CleanUpService.php +++ b/lib/Service/Text2Image/CleanUpService.php @@ -8,30 +8,24 @@ use Exception; use OCA\TpAssistant\AppInfo\Application; use OCA\TpAssistant\Db\Text2Image\ImageGenerationMapper; -use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\IConfig; use Psr\Log\LoggerInterface; use RuntimeException; -/** - * Service to make requests to OpenAI REST API - */ class CleanUpService { public function __construct( private LoggerInterface $logger, private ImageGenerationMapper $imageGenerationMapper, private Text2ImageHelperService $text2ImageHelperService, - private IAppData $appData, private IConfig $config ) { - } /** * @param int|null $maxAge - * @return array ('deleted_files' => int, 'file_deletion_errors' => int, 'deleted_generations' => int) + * @return array{deleted_files: int, file_deletion_errors: int, deleted_generations: int} * @throws Exception */ public function cleanupGenerationsAndFiles(?int $maxAge = null): array { @@ -56,7 +50,6 @@ public function cleanupGenerationsAndFiles(?int $maxAge = null): array { throw new Exception('Image data folder could not be accessed'); } - $deletedFiles = 0; $deletionErrors = 0; @@ -75,8 +68,5 @@ public function cleanupGenerationsAndFiles(?int $maxAge = null): array { ' idle generations. Failed to delete ' . $deletionErrors . ' files.'); return ['deleted_files' => $deletedFiles, 'file_deletion_errors' => $deletionErrors, 'deleted_generations' => $cleanedUp['deleted_generations']]; - } - - } diff --git a/lib/Service/Text2Image/Text2ImageHelperService.php b/lib/Service/Text2Image/Text2ImageHelperService.php index 55af8a33..31e3a543 100644 --- a/lib/Service/Text2Image/Text2ImageHelperService.php +++ b/lib/Service/Text2Image/Text2ImageHelperService.php @@ -45,7 +45,6 @@ class Text2ImageHelperService { public function __construct( private LoggerInterface $logger, private IManager $textToImageManager, - private ?string $userId, private PromptMapper $promptMapper, private ImageGenerationMapper $imageGenerationMapper, private ImageFileNameMapper $imageFileNameMapper, @@ -55,6 +54,7 @@ public function __construct( private IL10N $l10n, private AssistantService $assistantService, private TaskMapper $taskMapper, + private ?string $userId, ) { } @@ -281,7 +281,6 @@ public function getImageDataFolder(): ISimpleFolder { * Get image generation info. * @param string $imageGenId * @param bool $updateTimestamp - * @param string|null $userId * @return array * @throws \Exception */ @@ -356,10 +355,10 @@ public function getGenerationInfo(string $imageGenId, bool $updateTimestamp = tr * Get image based on imageFileNameId (imageGenId is used to prevent guessing image ids) * @param string $imageGenId * @param int $imageFileNameId - * @return array ('image' => string, 'content-type' => string) + * @return array{image: string, 'content-type': array} * @throws BaseException */ - public function getImage(string $imageGenId, int $imageFileNameId): ?array { + public function getImage(string $imageGenId, int $imageFileNameId): array { try { $generationId = $this->imageGenerationMapper->getImageGenerationOfImageGenId($imageGenId)->getId(); /** @var ImageFileName $imageFileName */ diff --git a/psalm.xml b/psalm.xml index 42fd3b2b..5613fa39 100644 --- a/psalm.xml +++ b/psalm.xml @@ -30,11 +30,13 @@ + +