aboutsummaryrefslogtreecommitdiffstats
path: root/core/Controller
diff options
context:
space:
mode:
authorMarcel Klehr <mklehr@gmx.net>2024-07-19 12:38:30 +0200
committerGitHub <noreply@github.com>2024-07-19 12:38:30 +0200
commita3c3eab09c50bc7dd16e95e8689f3a03b1d2ac98 (patch)
tree4b832bf2b18e9468c0781d5f912626655b24cdaf /core/Controller
parent64ca4b832dc6b7a574fd3301b1c0e022b192ea5d (diff)
parentff99df07e7212a3c8bfdcc3cf9a571353c6f84ff (diff)
downloadnextcloud-server-a3c3eab09c50bc7dd16e95e8689f3a03b1d2ac98.tar.gz
nextcloud-server-a3c3eab09c50bc7dd16e95e8689f3a03b1d2ac98.zip
Merge pull request #46368 from nextcloud/fix/task-processing
TaskProcessing follow-up
Diffstat (limited to 'core/Controller')
-rw-r--r--core/Controller/TaskProcessingApiController.php57
1 files changed, 55 insertions, 2 deletions
diff --git a/core/Controller/TaskProcessingApiController.php b/core/Controller/TaskProcessingApiController.php
index 90ee650f1ed..d9bcbd5da45 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,40 @@ 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 DataResponse<Http::STATUS_CREATED, array{fileId: int}, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
+ *
+ * 201: File created
+ * 400: File upload failed or no file was uploaded
+ * 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);
+ }
+ $handle = fopen($file['tmp_name'], 'r');
+ if (!$handle) {
+ return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
+ }
+ $fileId = $this->setFileContentsInternal($handle);
+ 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 +421,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 +433,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 +530,20 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController {
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
+
+ /**
+ * @param resource $data
+ * @return int
+ * @throws NotPermittedException
+ */
+ private function setFileContentsInternal($data): int {
+ try {
+ $folder = $this->appData->getFolder('TaskProcessing');
+ } catch (\OCP\Files\NotFoundException) {
+ $folder = $this->appData->newFolder('TaskProcessing');
+ }
+ /** @var SimpleFile $file */
+ $file = $folder->newFile(time() . '-' . rand(1, 100000), $data);
+ return $file->getId();
+ }
}