Task.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\TextToImage;
  8. use DateTime;
  9. use OCP\Files\AppData\IAppDataFactory;
  10. use OCP\Files\NotFoundException;
  11. use OCP\Files\NotPermittedException;
  12. use OCP\IImage;
  13. use OCP\Image;
  14. /**
  15. * This is a text to image task
  16. *
  17. * @since 28.0.0
  18. */
  19. final class Task implements \JsonSerializable {
  20. protected ?int $id = null;
  21. protected ?DateTime $completionExpectedAt = null;
  22. /**
  23. * @since 28.0.0
  24. */
  25. public const STATUS_FAILED = 4;
  26. /**
  27. * @since 28.0.0
  28. */
  29. public const STATUS_SUCCESSFUL = 3;
  30. /**
  31. * @since 28.0.0
  32. */
  33. public const STATUS_RUNNING = 2;
  34. /**
  35. * @since 28.0.0
  36. */
  37. public const STATUS_SCHEDULED = 1;
  38. /**
  39. * @since 28.0.0
  40. */
  41. public const STATUS_UNKNOWN = 0;
  42. /**
  43. * @psalm-var self::STATUS_*
  44. */
  45. protected int $status = self::STATUS_UNKNOWN;
  46. /**
  47. * @param string $input
  48. * @param string $appId
  49. * @param int $numberOfImages
  50. * @param string|null $userId
  51. * @param null|string $identifier An arbitrary identifier for this task. max length: 255 chars
  52. * @since 28.0.0
  53. */
  54. final public function __construct(
  55. protected string $input,
  56. protected string $appId,
  57. protected int $numberOfImages,
  58. protected ?string $userId,
  59. protected ?string $identifier = '',
  60. ) {
  61. }
  62. /**
  63. * @return IImage[]|null
  64. * @since 28.0.0
  65. */
  66. final public function getOutputImages(): ?array {
  67. $appData = \OCP\Server::get(IAppDataFactory::class)->get('core');
  68. try {
  69. $folder = $appData->getFolder('text2image')->getFolder((string)$this->getId());
  70. $images = [];
  71. for ($i = 0; $i < $this->getNumberOfImages(); $i++) {
  72. $image = new Image();
  73. $image->loadFromFileHandle($folder->getFile((string) $i)->read());
  74. $images[] = $image;
  75. }
  76. return $images;
  77. } catch (NotFoundException|NotPermittedException) {
  78. return null;
  79. }
  80. }
  81. /**
  82. * @return int
  83. * @since 28.0.0
  84. */
  85. final public function getNumberOfImages(): int {
  86. return $this->numberOfImages;
  87. }
  88. /**
  89. * @psalm-return self::STATUS_*
  90. * @since 28.0.0
  91. */
  92. final public function getStatus(): int {
  93. return $this->status;
  94. }
  95. /**
  96. * @psalm-param self::STATUS_* $status
  97. * @since 28.0.0
  98. */
  99. final public function setStatus(int $status): void {
  100. $this->status = $status;
  101. }
  102. /**
  103. * @param ?DateTime $at
  104. * @since 28.0.0
  105. */
  106. final public function setCompletionExpectedAt(?DateTime $at): void {
  107. $this->completionExpectedAt = $at;
  108. }
  109. /**
  110. * @return ?DateTime
  111. * @since 28.0.0
  112. */
  113. final public function getCompletionExpectedAt(): ?DateTime {
  114. return $this->completionExpectedAt;
  115. }
  116. /**
  117. * @return int|null
  118. * @since 28.0.0
  119. */
  120. final public function getId(): ?int {
  121. return $this->id;
  122. }
  123. /**
  124. * @param int|null $id
  125. * @since 28.0.0
  126. */
  127. final public function setId(?int $id): void {
  128. $this->id = $id;
  129. }
  130. /**
  131. * @return string
  132. * @since 28.0.0
  133. */
  134. final public function getInput(): string {
  135. return $this->input;
  136. }
  137. /**
  138. * @return string
  139. * @since 28.0.0
  140. */
  141. final public function getAppId(): string {
  142. return $this->appId;
  143. }
  144. /**
  145. * @return null|string
  146. * @since 28.0.0
  147. */
  148. final public function getIdentifier(): ?string {
  149. return $this->identifier;
  150. }
  151. /**
  152. * @return string|null
  153. * @since 28.0.0
  154. */
  155. final public function getUserId(): ?string {
  156. return $this->userId;
  157. }
  158. /**
  159. * @psalm-return array{id: ?int, status: self::STATUS_*, userId: ?string, appId: string, input: string, identifier: ?string, numberOfImages: int, completionExpectedAt: ?int}
  160. * @since 28.0.0
  161. */
  162. public function jsonSerialize(): array {
  163. return [
  164. 'id' => $this->getId(),
  165. 'status' => $this->getStatus(),
  166. 'userId' => $this->getUserId(),
  167. 'appId' => $this->getAppId(),
  168. 'numberOfImages' => $this->getNumberOfImages(),
  169. 'input' => $this->getInput(),
  170. 'identifier' => $this->getIdentifier(),
  171. 'completionExpectedAt' => $this->getCompletionExpectedAt()->getTimestamp(),
  172. ];
  173. }
  174. }