Skip to content

Commit

Permalink
Merge pull request #46368 from nextcloud/fix/task-processing
Browse files Browse the repository at this point in the history
TaskProcessing follow-up
  • Loading branch information
marcelklehr authored Jul 19, 2024
2 parents 64ca4b8 + ff99df0 commit a3c3eab
Show file tree
Hide file tree
Showing 8 changed files with 703 additions and 69 deletions.
57 changes: 55 additions & 2 deletions core/Controller/TaskProcessingApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand All @@ -22,6 +23,7 @@
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;
Expand Down Expand Up @@ -50,6 +52,7 @@ public function __construct(
private IL10N $l,
private ?string $userId,
private IRootFolder $rootFolder,
private IAppData $appData,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -286,6 +289,40 @@ public function getFileContentsExApp(int $taskId, int $fileId): Http\DataDownloa
}
}

/**
* 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
Expand Down Expand Up @@ -384,7 +421,7 @@ public function setProgress(int $taskId, float $progress): DataResponse {
* 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{}>
*
Expand All @@ -396,7 +433,7 @@ public function setProgress(int $taskId, float $progress): DataResponse {
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 */
Expand Down Expand Up @@ -493,4 +530,20 @@ public function getNextScheduledTask(array $providerIds, array $taskTypeIds): Da
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();
}
}
197 changes: 196 additions & 1 deletion core/openapi-ex_app.json
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,201 @@
}
}
},
"/ocs/v2.php/taskprocessing/tasks_provider/{taskId}/file": {
"post": {
"operationId": "task_processing_api-set-file-contents-ex-app",
"summary": "Upload a file so it can be referenced in a task result (ExApp route version)",
"description": "Use field 'file' for the file upload\nThis endpoint requires admin access",
"tags": [
"task_processing_api"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "taskId",
"in": "path",
"description": "The id of the task",
"required": true,
"schema": {
"type": "integer",
"format": "int64"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"201": {
"description": "File created",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"required": [
"fileId"
],
"properties": {
"fileId": {
"type": "integer",
"format": "int64"
}
}
}
}
}
}
}
}
}
},
"400": {
"description": "File upload failed or no file was uploaded",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"required": [
"message"
],
"properties": {
"message": {
"type": "string"
}
}
}
}
}
}
}
}
}
},
"500": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"required": [
"message"
],
"properties": {
"message": {
"type": "string"
}
}
}
}
}
}
}
}
}
},
"404": {
"description": "Task not found",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"required": [
"message"
],
"properties": {
"message": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/taskprocessing/tasks_provider/{taskId}/progress": {
"post": {
"operationId": "task_processing_api-set-progress",
Expand Down Expand Up @@ -541,7 +736,7 @@
"output": {
"type": "object",
"nullable": true,
"description": "The resulting task output",
"description": "The resulting task output, files are represented by their IDs",
"additionalProperties": {
"type": "object"
}
Expand Down
Loading

0 comments on commit a3c3eab

Please sign in to comment.