1
0

Task.php 3.9 KB

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