2023-09-07 12:37:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
2024-05-27 10:08:53 +02:00
|
|
|
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2023-09-07 12:37:09 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
2024-05-26 12:51:24 +02:00
|
|
|
use OC\Core\ResponseDefinitions;
|
2023-09-07 12:37:09 +02:00
|
|
|
use OC\Files\AppData\AppData;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
|
2024-01-10 12:35:44 +01:00
|
|
|
use OCP\AppFramework\Http\Attribute\ApiRoute;
|
2023-10-18 14:09:19 +02:00
|
|
|
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
|
2023-09-07 12:37:09 +02:00
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
|
|
|
use OCP\AppFramework\Http\Attribute\PublicPage;
|
|
|
|
use OCP\AppFramework\Http\Attribute\UserRateLimit;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
2023-10-20 12:17:17 +02:00
|
|
|
use OCP\DB\Exception;
|
2023-10-18 10:51:28 +02:00
|
|
|
use OCP\Files\NotFoundException;
|
2023-09-07 12:37:09 +02:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\PreConditionNotMetException;
|
2023-10-26 11:16:15 +02:00
|
|
|
use OCP\TextToImage\Exception\TaskFailureException;
|
2023-09-07 12:37:09 +02:00
|
|
|
use OCP\TextToImage\Exception\TaskNotFoundException;
|
|
|
|
use OCP\TextToImage\IManager;
|
|
|
|
use OCP\TextToImage\Task;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-import-type CoreTextToImageTask from ResponseDefinitions
|
|
|
|
*/
|
|
|
|
class TextToImageApiController extends \OCP\AppFramework\OCSController {
|
|
|
|
public function __construct(
|
2023-10-16 15:41:56 +02:00
|
|
|
string $appName,
|
|
|
|
IRequest $request,
|
|
|
|
private IManager $textToImageManager,
|
|
|
|
private IL10N $l,
|
|
|
|
private ?string $userId,
|
|
|
|
private AppData $appData,
|
2023-09-07 12:37:09 +02:00
|
|
|
) {
|
|
|
|
parent::__construct($appName, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-19 11:07:03 +02:00
|
|
|
* Check whether this feature is available
|
2023-09-07 12:37:09 +02:00
|
|
|
*
|
|
|
|
* @return DataResponse<Http::STATUS_OK, array{isAvailable: bool}, array{}>
|
2023-10-19 11:07:03 +02:00
|
|
|
*
|
|
|
|
* 200: Returns availability status
|
2023-09-07 12:37:09 +02:00
|
|
|
*/
|
2023-10-16 16:19:19 +02:00
|
|
|
#[PublicPage]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'GET', url: '/is_available', root: '/text2image')]
|
2023-09-07 12:37:09 +02:00
|
|
|
public function isAvailable(): DataResponse {
|
|
|
|
return new DataResponse([
|
|
|
|
'isAvailable' => $this->textToImageManager->hasProviders(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This endpoint allows scheduling a text to image task
|
|
|
|
*
|
|
|
|
* @param string $input Input text
|
|
|
|
* @param string $appId ID of the app that will execute the task
|
|
|
|
* @param string $identifier An arbitrary identifier for the task
|
2023-10-20 13:13:15 +02:00
|
|
|
* @param int $numberOfImages The number of images to generate
|
2023-09-07 12:37:09 +02:00
|
|
|
*
|
2023-10-20 13:33:05 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, array{task: CoreTextToImageTask}, array{}>|DataResponse<Http::STATUS_PRECONDITION_FAILED|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
|
2023-09-07 12:37:09 +02:00
|
|
|
*
|
|
|
|
* 200: Task scheduled successfully
|
|
|
|
* 412: Scheduling task is not possible
|
|
|
|
*/
|
|
|
|
#[PublicPage]
|
|
|
|
#[UserRateLimit(limit: 20, period: 120)]
|
|
|
|
#[AnonRateLimit(limit: 5, period: 120)]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'POST', url: '/schedule', root: '/text2image')]
|
2023-10-20 13:13:15 +02:00
|
|
|
public function schedule(string $input, string $appId, string $identifier = '', int $numberOfImages = 8): DataResponse {
|
|
|
|
$task = new Task($input, $appId, $numberOfImages, $this->userId, $identifier);
|
2023-09-07 12:37:09 +02:00
|
|
|
try {
|
2023-10-20 12:17:17 +02:00
|
|
|
try {
|
|
|
|
$this->textToImageManager->runOrScheduleTask($task);
|
2023-10-26 11:16:15 +02:00
|
|
|
} catch (TaskFailureException) {
|
2023-10-26 11:35:55 +02:00
|
|
|
// Task status was already updated by the manager, nothing to do here
|
2023-10-20 12:17:17 +02:00
|
|
|
}
|
2023-09-07 12:37:09 +02:00
|
|
|
|
|
|
|
$json = $task->jsonSerialize();
|
|
|
|
|
|
|
|
return new DataResponse([
|
|
|
|
'task' => $json,
|
|
|
|
]);
|
|
|
|
} catch (PreConditionNotMetException) {
|
|
|
|
return new DataResponse(['message' => $this->l->t('No text to image provider is available')], Http::STATUS_PRECONDITION_FAILED);
|
2023-10-20 12:17:17 +02:00
|
|
|
} catch (Exception) {
|
|
|
|
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2023-09-07 12:37:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This endpoint allows checking the status and results of a task.
|
|
|
|
* Tasks are removed 1 week after receiving their last update.
|
|
|
|
*
|
|
|
|
* @param int $id The id of the task
|
|
|
|
*
|
|
|
|
* @return DataResponse<Http::STATUS_OK, array{task: CoreTextToImageTask}, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
|
|
|
|
*
|
|
|
|
* 200: Task returned
|
|
|
|
* 404: Task not found
|
|
|
|
*/
|
|
|
|
#[PublicPage]
|
2023-10-18 14:46:40 +02:00
|
|
|
#[BruteForceProtection(action: 'text2image')]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'GET', url: '/task/{id}', root: '/text2image')]
|
2023-09-07 12:37:09 +02:00
|
|
|
public function getTask(int $id): DataResponse {
|
|
|
|
try {
|
|
|
|
$task = $this->textToImageManager->getUserTask($id, $this->userId);
|
|
|
|
|
|
|
|
$json = $task->jsonSerialize();
|
|
|
|
|
|
|
|
return new DataResponse([
|
|
|
|
'task' => $json,
|
|
|
|
]);
|
2023-09-07 13:03:38 +02:00
|
|
|
} catch (TaskNotFoundException) {
|
2023-10-18 14:09:19 +02:00
|
|
|
$res = new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
|
2023-10-18 14:46:40 +02:00
|
|
|
$res->throttle(['action' => 'text2image']);
|
2023-10-18 14:09:19 +02:00
|
|
|
return $res;
|
2023-09-07 13:03:38 +02:00
|
|
|
} catch (\RuntimeException) {
|
2023-10-18 14:46:40 +02:00
|
|
|
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2023-09-07 12:37:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-18 10:53:06 +02:00
|
|
|
* This endpoint allows downloading the resulting image of a task
|
2023-09-07 12:37:09 +02:00
|
|
|
*
|
|
|
|
* @param int $id The id of the task
|
2023-10-20 13:13:15 +02:00
|
|
|
* @param int $index The index of the image to retrieve
|
2023-09-07 12:37:09 +02:00
|
|
|
*
|
|
|
|
* @return FileDisplayResponse<Http::STATUS_OK, array{'Content-Type': string}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
|
|
|
|
*
|
2023-10-18 10:53:06 +02:00
|
|
|
* 200: Image returned
|
|
|
|
* 404: Task or image not found
|
2023-09-07 12:37:09 +02:00
|
|
|
*/
|
|
|
|
#[PublicPage]
|
2023-10-18 14:46:40 +02:00
|
|
|
#[BruteForceProtection(action: 'text2image')]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'GET', url: '/task/{id}/image/{index}', root: '/text2image')]
|
2023-10-20 13:13:15 +02:00
|
|
|
public function getImage(int $id, int $index): DataResponse|FileDisplayResponse {
|
2023-09-07 12:37:09 +02:00
|
|
|
try {
|
|
|
|
$task = $this->textToImageManager->getUserTask($id, $this->userId);
|
|
|
|
try {
|
|
|
|
$folder = $this->appData->getFolder('text2image');
|
2023-10-18 10:51:28 +02:00
|
|
|
} catch (NotFoundException) {
|
2023-10-20 13:13:15 +02:00
|
|
|
$res = new DataResponse(['message' => $this->l->t('Image not found')], Http::STATUS_NOT_FOUND);
|
|
|
|
$res->throttle(['action' => 'text2image']);
|
|
|
|
return $res;
|
2023-09-07 12:37:09 +02:00
|
|
|
}
|
2023-10-20 13:13:15 +02:00
|
|
|
$file = $folder->getFolder((string)$task->getId())->getFile((string)$index);
|
2023-09-07 12:37:09 +02:00
|
|
|
$info = getimagesizefromstring($file->getContent());
|
|
|
|
|
|
|
|
return new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => image_type_to_mime_type($info[2])]);
|
|
|
|
} catch (TaskNotFoundException) {
|
2023-10-18 14:09:19 +02:00
|
|
|
$res = new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
|
2023-10-18 14:46:40 +02:00
|
|
|
$res->throttle(['action' => 'text2image']);
|
2023-10-18 14:09:19 +02:00
|
|
|
return $res;
|
2023-09-07 12:37:09 +02:00
|
|
|
} catch (\RuntimeException) {
|
2023-10-18 14:46:40 +02:00
|
|
|
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2023-10-18 10:51:28 +02:00
|
|
|
} catch (NotFoundException) {
|
2023-10-18 14:09:19 +02:00
|
|
|
$res = new DataResponse(['message' => $this->l->t('Image not found')], Http::STATUS_NOT_FOUND);
|
2023-10-18 14:46:40 +02:00
|
|
|
$res->throttle(['action' => 'text2image']);
|
2023-10-18 14:09:19 +02:00
|
|
|
return $res;
|
2023-09-07 12:37:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This endpoint allows to delete a scheduled task for a user
|
|
|
|
*
|
|
|
|
* @param int $id The id of the task
|
|
|
|
*
|
|
|
|
* @return DataResponse<Http::STATUS_OK, array{task: CoreTextToImageTask}, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
|
|
|
|
*
|
|
|
|
* 200: Task returned
|
|
|
|
* 404: Task not found
|
|
|
|
*/
|
|
|
|
#[NoAdminRequired]
|
2023-10-18 14:46:40 +02:00
|
|
|
#[BruteForceProtection(action: 'text2image')]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'DELETE', url: '/task/{id}', root: '/text2image')]
|
2023-09-07 12:37:09 +02:00
|
|
|
public function deleteTask(int $id): DataResponse {
|
|
|
|
try {
|
|
|
|
$task = $this->textToImageManager->getUserTask($id, $this->userId);
|
|
|
|
|
|
|
|
$this->textToImageManager->deleteTask($task);
|
|
|
|
|
|
|
|
$json = $task->jsonSerialize();
|
|
|
|
|
|
|
|
return new DataResponse([
|
|
|
|
'task' => $json,
|
|
|
|
]);
|
2023-09-07 13:03:38 +02:00
|
|
|
} catch (TaskNotFoundException) {
|
2023-10-18 14:09:19 +02:00
|
|
|
$res = new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
|
2023-10-18 14:46:40 +02:00
|
|
|
$res->throttle(['action' => 'text2image']);
|
2023-10-18 14:09:19 +02:00
|
|
|
return $res;
|
2023-09-07 13:03:38 +02:00
|
|
|
} catch (\RuntimeException) {
|
2023-10-18 14:46:40 +02:00
|
|
|
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2023-09-07 12:37:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This endpoint returns a list of tasks of a user that are related
|
|
|
|
* with a specific appId and optionally with an identifier
|
|
|
|
*
|
|
|
|
* @param string $appId ID of the app
|
|
|
|
* @param string|null $identifier An arbitrary identifier for the task
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, array{tasks: list<CoreTextToImageTask>}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
|
2023-09-07 12:37:09 +02:00
|
|
|
*
|
|
|
|
* 200: Task list returned
|
|
|
|
*/
|
|
|
|
#[NoAdminRequired]
|
2023-10-18 10:51:28 +02:00
|
|
|
#[AnonRateLimit(limit: 5, period: 120)]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'GET', url: '/tasks/app/{appId}', root: '/text2image')]
|
2023-09-07 12:37:09 +02:00
|
|
|
public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse {
|
|
|
|
try {
|
|
|
|
$tasks = $this->textToImageManager->getUserTasksByApp($this->userId, $appId, $identifier);
|
2024-09-24 15:53:13 +02:00
|
|
|
$json = array_values(array_map(static function (Task $task) {
|
2023-09-07 12:37:09 +02:00
|
|
|
return $task->jsonSerialize();
|
2024-09-24 15:53:13 +02:00
|
|
|
}, $tasks));
|
2023-09-07 12:37:09 +02:00
|
|
|
|
|
|
|
return new DataResponse([
|
|
|
|
'tasks' => $json,
|
|
|
|
]);
|
2023-09-07 13:03:38 +02:00
|
|
|
} catch (\RuntimeException) {
|
2023-09-07 12:37:09 +02:00
|
|
|
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|