aboutsummaryrefslogtreecommitdiffstats
path: root/core/Controller
diff options
context:
space:
mode:
authorMarcel Klehr <mklehr@gmx.net>2024-07-09 11:43:11 +0200
committerMarcel Klehr <mklehr@gmx.net>2024-07-17 13:55:55 +0200
commit4ac7f8275b5e89023c8c6c4f468d82d5c782c0d4 (patch)
tree2d88320bc29f0b21c29547505e8a1a7b0e3219c6 /core/Controller
parentb06ce832d8f280b9c008b91c41757e8eab37dc77 (diff)
downloadnextcloud-server-4ac7f8275b5e89023c8c6c4f468d82d5c782c0d4.tar.gz
nextcloud-server-4ac7f8275b5e89023c8c6c4f468d82d5c782c0d4.zip
feat(TaskProcessing): Allow setting task results for file slots
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Diffstat (limited to 'core/Controller')
-rw-r--r--core/Controller/TaskProcessingApiController.php51
1 files changed, 49 insertions, 2 deletions
diff --git a/core/Controller/TaskProcessingApiController.php b/core/Controller/TaskProcessingApiController.php
index 90ee650f1ed..981f67804e4 100644
--- a/core/Controller/TaskProcessingApiController.php
+++ b/core/Controller/TaskProcessingApiController.php
@@ -11,6 +11,7 @@ declare(strict_types=1);
namespace OC\Core\Controller;
use OC\Core\ResponseDefinitions;
+use OC\Files\SimpleFS\SimpleFile;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
use OCP\AppFramework\Http\Attribute\ApiRoute;
@@ -22,6 +23,7 @@ use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\File;
use OCP\Files\GenericFileException;
+use OCP\Files\IAppData;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\IL10N;
@@ -50,6 +52,7 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController {
private IL10N $l,
private ?string $userId,
private IRootFolder $rootFolder,
+ private IAppData $appData,
) {
parent::__construct($appName, $request);
}
@@ -287,6 +290,39 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController {
}
/**
+ * Upload a file so it can be referenced in a task result (ExApp route version)
+ *
+ * Use field 'file' for the file upload
+ *
+ * @param int $taskId The id of the task
+ * @return DataDownloadResponse<Http::STATUS_CREATED, array{fileId: int}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
+ *
+ * 201: File created
+ * 404: Task not found
+ */
+ #[ExAppRequired]
+ #[ApiRoute(verb: 'POST', url: '/tasks_provider/{taskId}/file', root: '/taskprocessing')]
+ public function setFileContentsExApp(int $taskId): DataResponse {
+ try {
+ $task = $this->taskProcessingManager->getTask($taskId);
+ $file = $this->request->getUploadedFile('file');
+ if (!isset($file['tmp_name'])) {
+ return new DataResponse(['message' => $this->l->t('Bad request')], Http::STATUS_BAD_REQUEST);
+ }
+ $data = file_get_contents($file['tmp_name']);
+ if (!$data) {
+ return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
+ }
+ $fileId = $this->setFileContentsInternal($task, $data);
+ return new DataResponse(['fileId' => $fileId], Http::STATUS_CREATED);
+ } catch (NotFoundException) {
+ return new DataResponse(['message' => $this->l->t('Not found')], Http::STATUS_NOT_FOUND);
+ } catch (Exception) {
+ return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ /**
* @throws NotPermittedException
* @throws NotFoundException
* @throws GenericFileException
@@ -384,7 +420,7 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController {
* Sets the task result
*
* @param int $taskId The id of the task
- * @param array<string,mixed>|null $output The resulting task output
+ * @param array<string,mixed>|null $output The resulting task output, files are represented by their IDs
* @param string|null $errorMessage An error message if the task failed
* @return DataResponse<Http::STATUS_OK, array{task: CoreTaskProcessingTask}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
*
@@ -396,7 +432,7 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController {
public function setResult(int $taskId, ?array $output = null, ?string $errorMessage = null): DataResponse {
try {
// set result
- $this->taskProcessingManager->setTaskResult($taskId, $errorMessage, $output);
+ $this->taskProcessingManager->setTaskResult($taskId, $errorMessage, $output, true);
$task = $this->taskProcessingManager->getTask($taskId);
/** @var CoreTaskProcessingTask $json */
@@ -493,4 +529,15 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController {
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
+
+ private function setFileContentsInternal(Task $task, string $data) {
+ try {
+ $folder = $this->appData->getFolder('TaskProcessing');
+ } catch (\OCP\Files\NotFoundException) {
+ $folder = $this->appData->newFolder('TaskProcessing');
+ }
+ /** @var SimpleFile $file */
+ $file = $folder->newFile((string) rand(0, 10000000), $data);
+ return $file->getId();
+ }
}