Task.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
  5. *
  6. * @author Marcel Klehr <mklehr@gmx.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OCP\TextToImage;
  24. use DateTime;
  25. use OCP\Files\AppData\IAppDataFactory;
  26. use OCP\Files\NotFoundException;
  27. use OCP\Files\NotPermittedException;
  28. use OCP\IImage;
  29. use OCP\Image;
  30. /**
  31. * This is a text to image task
  32. *
  33. * @since 28.0.0
  34. */
  35. final class Task implements \JsonSerializable {
  36. protected ?int $id = null;
  37. protected ?DateTime $completionExpectedAt = null;
  38. /**
  39. * @since 28.0.0
  40. */
  41. public const STATUS_FAILED = 4;
  42. /**
  43. * @since 28.0.0
  44. */
  45. public const STATUS_SUCCESSFUL = 3;
  46. /**
  47. * @since 28.0.0
  48. */
  49. public const STATUS_RUNNING = 2;
  50. /**
  51. * @since 28.0.0
  52. */
  53. public const STATUS_SCHEDULED = 1;
  54. /**
  55. * @since 28.0.0
  56. */
  57. public const STATUS_UNKNOWN = 0;
  58. /**
  59. * @psalm-var self::STATUS_*
  60. */
  61. protected int $status = self::STATUS_UNKNOWN;
  62. /**
  63. * @param string $input
  64. * @param string $appId
  65. * @param int $numberOfImages
  66. * @param string|null $userId
  67. * @param null|string $identifier An arbitrary identifier for this task. max length: 255 chars
  68. * @since 28.0.0
  69. */
  70. final public function __construct(
  71. protected string $input,
  72. protected string $appId,
  73. protected int $numberOfImages,
  74. protected ?string $userId,
  75. protected ?string $identifier = '',
  76. ) {
  77. }
  78. /**
  79. * @return IImage[]|null
  80. * @since 28.0.0
  81. */
  82. final public function getOutputImages(): ?array {
  83. $appData = \OCP\Server::get(IAppDataFactory::class)->get('core');
  84. try {
  85. $folder = $appData->getFolder('text2image')->getFolder((string)$this->getId());
  86. $images = [];
  87. for ($i = 0; $i < $this->getNumberOfImages(); $i++) {
  88. $image = new Image();
  89. $image->loadFromFileHandle($folder->getFile((string) $i)->read());
  90. $images[] = $image;
  91. }
  92. return $images;
  93. } catch (NotFoundException|NotPermittedException) {
  94. return null;
  95. }
  96. }
  97. /**
  98. * @return int
  99. * @since 28.0.0
  100. */
  101. final public function getNumberOfImages(): int {
  102. return $this->numberOfImages;
  103. }
  104. /**
  105. * @psalm-return self::STATUS_*
  106. * @since 28.0.0
  107. */
  108. final public function getStatus(): int {
  109. return $this->status;
  110. }
  111. /**
  112. * @psalm-param self::STATUS_* $status
  113. * @since 28.0.0
  114. */
  115. final public function setStatus(int $status): void {
  116. $this->status = $status;
  117. }
  118. /**
  119. * @param ?DateTime $at
  120. * @since 28.0.0
  121. */
  122. final public function setCompletionExpectedAt(?DateTime $at): void {
  123. $this->completionExpectedAt = $at;
  124. }
  125. /**
  126. * @return ?DateTime
  127. * @since 28.0.0
  128. */
  129. final public function getCompletionExpectedAt(): ?DateTime {
  130. return $this->completionExpectedAt;
  131. }
  132. /**
  133. * @return int|null
  134. * @since 28.0.0
  135. */
  136. final public function getId(): ?int {
  137. return $this->id;
  138. }
  139. /**
  140. * @param int|null $id
  141. * @since 28.0.0
  142. */
  143. final public function setId(?int $id): void {
  144. $this->id = $id;
  145. }
  146. /**
  147. * @return string
  148. * @since 28.0.0
  149. */
  150. final public function getInput(): string {
  151. return $this->input;
  152. }
  153. /**
  154. * @return string
  155. * @since 28.0.0
  156. */
  157. final public function getAppId(): string {
  158. return $this->appId;
  159. }
  160. /**
  161. * @return null|string
  162. * @since 28.0.0
  163. */
  164. final public function getIdentifier(): ?string {
  165. return $this->identifier;
  166. }
  167. /**
  168. * @return string|null
  169. * @since 28.0.0
  170. */
  171. final public function getUserId(): ?string {
  172. return $this->userId;
  173. }
  174. /**
  175. * @psalm-return array{id: ?int, status: self::STATUS_*, userId: ?string, appId: string, input: string, identifier: ?string, numberOfImages: int, completionExpectedAt: ?int}
  176. * @since 28.0.0
  177. */
  178. public function jsonSerialize(): array {
  179. return [
  180. 'id' => $this->getId(),
  181. 'status' => $this->getStatus(),
  182. 'userId' => $this->getUserId(),
  183. 'appId' => $this->getAppId(),
  184. 'numberOfImages' => $this->getNumberOfImages(),
  185. 'input' => $this->getInput(),
  186. 'identifier' => $this->getIdentifier(),
  187. 'completionExpectedAt' => $this->getCompletionExpectedAt()->getTimestamp(),
  188. ];
  189. }
  190. }