Skip to content

Commit dbeebd1

Browse files
authored
Merge pull request #15347 from nextcloud/feat/taskprocessing-fileshaped
docs(taskprocessing): Document FileShaped usage
2 parents 29023a0 + 5d2554d commit dbeebd1

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

developer_manual/digging_deeper/task_processing.rst

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,45 @@ A **Task processing provider** will usually be a class that implements the inter
416416
The method ``getName`` returns a string to identify the registered provider in the user interface.
417417

418418
The method ``process`` implements the task processing step. In case execution fails for some reason, you should throw a ``\OCP\TaskProcessing\Exception\ProcessingException`` with an explanatory error message.
419-
Since v33.0.0 you can now also throw an ``OCP\TaskProcessing\Exception\UserFacingProcessingException`` which includes a string parameter to set for error messages that will be propagated to the end-user, make sure to always translate these into the language of the user that requested the task. The rule of thumb of when to use a user-facing error message, is as follows: When the error happened due to a mistake from the user or the user can do something to fix the error, throw a user facing error. However, do make sure to not include implementation or server details in the user facing error message, that's what the normal error message is for; it will only be visible to administrators.
420419

421-
Important to note here is that ``Image``, ``Audio``, ``Video`` and ``File`` slots in the input array will be filled with ``\OCP\Files\File`` objects for your convenience. When outputting one of these you should simply return a string, the API will turn the data into a proper file for convenience. The ``$reportProgress`` parameter is a callback that you may use at will to report the task progress as a single float value between 0 and 1. Its return value will indicate if the task is still running (``true``) or if it was cancelled (``false``) and processing should be terminated.
420+
The ``$reportProgress`` parameter is a callback that you may use at will to report the task progress as a single float value between 0 and 1. Its return value will indicate if the task is still running (``true``) or if it was cancelled (``false``) and processing should be terminated.
422421

423422
This class would typically be saved into a file in ``lib/TaskProcessing`` of your app but you are free to put it elsewhere as long as it's loadable by Nextcloud's :ref:`dependency injection container<dependency-injection>`.
424423

424+
User-facing errors
425+
^^^^^^^^^^^^^^^^^^
426+
427+
.. versionadded:: 33.0.0
428+
429+
Since v33.0.0 you can now also throw an ``OCP\TaskProcessing\Exception\UserFacingProcessingException`` which includes a string parameter to set for error messages that will be propagated to the end-user, make sure to always translate these into the language of the user that requested the task. The rule of thumb of when to use a user-facing error message, is as follows: When the error happened due to a mistake from the user or the user can do something to fix the error, throw a user facing error. However, do make sure to not include implementation or server details in the user facing error message, that's what the normal error message is for; it will only be visible to administrators.
430+
431+
432+
Dealing with file-shaped data
433+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
434+
435+
TaskProcessing providers often want to operate on files. If your provider takes ``Image``, ``Audio``, ``Video`` and ``File`` slots, these will be passed to the ``process`` method as ``\OCP\Files\File`` objects.
436+
437+
If your provider provides ``Image``, ``Audio``, ``Video`` and ``File`` slots as output, you can output these as strings that contain the binary data. These will be stored in files, and consumers will receive the corresponding file IDs instead.
438+
439+
.. versionadded:: 35.0.0
440+
441+
Since v35.0.0 we recommend providing file-shaped output (ie. ``Image``, ``Audio``, ``Video`` and ``File`` slots) using the the ``\OCP\TaskProcessing\FileShaped`` class, which also allows specifying a file extension.
442+
443+
This eases mime-type detection for task processing consumers.
444+
445+
.. code-block:: php
446+
447+
public function process(
448+
?string $userId, array $input, callable $reportProgress
449+
): array {
450+
// …
451+
return ['output' => new FileShaped(EShapeType::Audio, $binaryOutput, extension: 'ogg')];
452+
}
453+
425454
.. _task_processing-options:
426455

427456
Implementing an advanced TaskProcessing provider
428-
------------------------------------------------
457+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
429458

430459
The ``\OCP\TaskProcessing\ISynchronousOptionsAwareProvider`` interface is available if you want your provider
431460
to support watermarking or streaming. If your provider implements ``\OCP\TaskProcessing\ISynchronousOptionsAwareProvider``,

0 commit comments

Comments
 (0)