FilesMetadata.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 OC\FilesMetadata\Model;
  8. use JsonException;
  9. use OCP\FilesMetadata\Exceptions\FilesMetadataKeyFormatException;
  10. use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
  11. use OCP\FilesMetadata\Exceptions\FilesMetadataTypeException;
  12. use OCP\FilesMetadata\Model\IFilesMetadata;
  13. use OCP\FilesMetadata\Model\IMetadataValueWrapper;
  14. /**
  15. * Model that represent metadata linked to a specific file.
  16. *
  17. * @inheritDoc
  18. * @since 28.0.0
  19. */
  20. class FilesMetadata implements IFilesMetadata {
  21. /** @var array<string, MetadataValueWrapper> */
  22. private array $metadata = [];
  23. private bool $updated = false;
  24. private int $lastUpdate = 0;
  25. private string $syncToken = '';
  26. public function __construct(
  27. private int $fileId = 0
  28. ) {
  29. }
  30. /**
  31. * @inheritDoc
  32. * @return int related file id
  33. * @since 28.0.0
  34. */
  35. public function getFileId(): int {
  36. return $this->fileId;
  37. }
  38. /**
  39. * @inheritDoc
  40. * @return int timestamp
  41. * @since 28.0.0
  42. */
  43. public function lastUpdateTimestamp(): int {
  44. return $this->lastUpdate;
  45. }
  46. /**
  47. * @inheritDoc
  48. * @return string token
  49. * @since 28.0.0
  50. */
  51. public function getSyncToken(): string {
  52. return $this->syncToken;
  53. }
  54. /**
  55. * @inheritDoc
  56. * @return string[] list of keys
  57. * @since 28.0.0
  58. */
  59. public function getKeys(): array {
  60. return array_keys($this->metadata);
  61. }
  62. /**
  63. * @param string $needle metadata key to search
  64. *
  65. * @inheritDoc
  66. * @return bool TRUE if key exist
  67. * @since 28.0.0
  68. */
  69. public function hasKey(string $needle): bool {
  70. return (in_array($needle, $this->getKeys()));
  71. }
  72. /**
  73. * @inheritDoc
  74. * @return string[] list of indexes
  75. * @since 28.0.0
  76. */
  77. public function getIndexes(): array {
  78. $indexes = [];
  79. foreach ($this->getKeys() as $key) {
  80. if ($this->metadata[$key]->isIndexed()) {
  81. $indexes[] = $key;
  82. }
  83. }
  84. return $indexes;
  85. }
  86. /**
  87. * @param string $key metadata key
  88. *
  89. * @inheritDoc
  90. * @return bool TRUE if key exists and is set as indexed
  91. * @since 28.0.0
  92. */
  93. public function isIndex(string $key): bool {
  94. return $this->metadata[$key]?->isIndexed() ?? false;
  95. }
  96. /**
  97. * @param string $key metadata key
  98. *
  99. * @inheritDoc
  100. * @return int edit permission
  101. * @throws FilesMetadataNotFoundException
  102. * @since 28.0.0
  103. */
  104. public function getEditPermission(string $key): int {
  105. if (!array_key_exists($key, $this->metadata)) {
  106. throw new FilesMetadataNotFoundException();
  107. }
  108. return $this->metadata[$key]->getEditPermission();
  109. }
  110. /**
  111. * @param string $key metadata key
  112. * @param int $permission edit permission
  113. *
  114. * @inheritDoc
  115. * @throws FilesMetadataNotFoundException
  116. * @since 28.0.0
  117. */
  118. public function setEditPermission(string $key, int $permission): void {
  119. if (!array_key_exists($key, $this->metadata)) {
  120. throw new FilesMetadataNotFoundException();
  121. }
  122. $this->metadata[$key]->setEditPermission($permission);
  123. }
  124. public function getEtag(string $key): string {
  125. if (!array_key_exists($key, $this->metadata)) {
  126. return '';
  127. }
  128. return $this->metadata[$key]->getEtag();
  129. }
  130. public function setEtag(string $key, string $etag): void {
  131. if (!array_key_exists($key, $this->metadata)) {
  132. throw new FilesMetadataNotFoundException();
  133. }
  134. $this->metadata[$key]->setEtag($etag);
  135. }
  136. /**
  137. * @param string $key metadata key
  138. *
  139. * @inheritDoc
  140. * @return string metadata value
  141. * @throws FilesMetadataNotFoundException
  142. * @throws FilesMetadataTypeException
  143. * @since 28.0.0
  144. */
  145. public function getString(string $key): string {
  146. if (!array_key_exists($key, $this->metadata)) {
  147. throw new FilesMetadataNotFoundException();
  148. }
  149. return $this->metadata[$key]->getValueString();
  150. }
  151. /**
  152. * @param string $key metadata key
  153. *
  154. * @inheritDoc
  155. * @return int metadata value
  156. * @throws FilesMetadataNotFoundException
  157. * @throws FilesMetadataTypeException
  158. * @since 28.0.0
  159. */
  160. public function getInt(string $key): int {
  161. if (!array_key_exists($key, $this->metadata)) {
  162. throw new FilesMetadataNotFoundException();
  163. }
  164. return $this->metadata[$key]->getValueInt();
  165. }
  166. /**
  167. * @param string $key metadata key
  168. *
  169. * @inheritDoc
  170. * @return float metadata value
  171. * @throws FilesMetadataNotFoundException
  172. * @throws FilesMetadataTypeException
  173. * @since 28.0.0
  174. */
  175. public function getFloat(string $key): float {
  176. if (!array_key_exists($key, $this->metadata)) {
  177. throw new FilesMetadataNotFoundException();
  178. }
  179. return $this->metadata[$key]->getValueFloat();
  180. }
  181. /**
  182. * @param string $key metadata key
  183. *
  184. * @inheritDoc
  185. * @return bool metadata value
  186. * @throws FilesMetadataNotFoundException
  187. * @throws FilesMetadataTypeException
  188. * @since 28.0.0
  189. */
  190. public function getBool(string $key): bool {
  191. if (!array_key_exists($key, $this->metadata)) {
  192. throw new FilesMetadataNotFoundException();
  193. }
  194. return $this->metadata[$key]->getValueBool();
  195. }
  196. /**
  197. * @param string $key metadata key
  198. *
  199. * @inheritDoc
  200. * @return array metadata value
  201. * @throws FilesMetadataNotFoundException
  202. * @throws FilesMetadataTypeException
  203. * @since 28.0.0
  204. */
  205. public function getArray(string $key): array {
  206. if (!array_key_exists($key, $this->metadata)) {
  207. throw new FilesMetadataNotFoundException();
  208. }
  209. return $this->metadata[$key]->getValueArray();
  210. }
  211. /**
  212. * @param string $key metadata key
  213. *
  214. * @inheritDoc
  215. * @return string[] metadata value
  216. * @throws FilesMetadataNotFoundException
  217. * @throws FilesMetadataTypeException
  218. * @since 28.0.0
  219. */
  220. public function getStringList(string $key): array {
  221. if (!array_key_exists($key, $this->metadata)) {
  222. throw new FilesMetadataNotFoundException();
  223. }
  224. return $this->metadata[$key]->getValueStringList();
  225. }
  226. /**
  227. * @param string $key metadata key
  228. *
  229. * @inheritDoc
  230. * @return int[] metadata value
  231. * @throws FilesMetadataNotFoundException
  232. * @throws FilesMetadataTypeException
  233. * @since 28.0.0
  234. */
  235. public function getIntList(string $key): array {
  236. if (!array_key_exists($key, $this->metadata)) {
  237. throw new FilesMetadataNotFoundException();
  238. }
  239. return $this->metadata[$key]->getValueIntList();
  240. }
  241. /**
  242. * @param string $key metadata key
  243. *
  244. * @inheritDoc
  245. * @return string value type
  246. * @throws FilesMetadataNotFoundException
  247. * @see IMetadataValueWrapper::TYPE_STRING
  248. * @see IMetadataValueWrapper::TYPE_INT
  249. * @see IMetadataValueWrapper::TYPE_FLOAT
  250. * @see IMetadataValueWrapper::TYPE_BOOL
  251. * @see IMetadataValueWrapper::TYPE_ARRAY
  252. * @see IMetadataValueWrapper::TYPE_STRING_LIST
  253. * @see IMetadataValueWrapper::TYPE_INT_LIST
  254. * @since 28.0.0
  255. */
  256. public function getType(string $key): string {
  257. if (!array_key_exists($key, $this->metadata)) {
  258. throw new FilesMetadataNotFoundException();
  259. }
  260. return $this->metadata[$key]->getType();
  261. }
  262. /**
  263. * @param string $key metadata key
  264. * @param string $value metadata value
  265. * @param bool $index set TRUE if value must be indexed
  266. *
  267. * @inheritDoc
  268. * @return self
  269. * @throws FilesMetadataKeyFormatException
  270. * @since 28.0.0
  271. */
  272. public function setString(string $key, string $value, bool $index = false): IFilesMetadata {
  273. $this->confirmKeyFormat($key);
  274. try {
  275. if ($this->getString($key) === $value && $index === $this->isIndex($key)) {
  276. return $this; // we ignore if value and index have not changed
  277. }
  278. } catch (FilesMetadataNotFoundException|FilesMetadataTypeException $e) {
  279. // if value does not exist, or type has changed, we keep on the writing
  280. }
  281. $meta = new MetadataValueWrapper(IMetadataValueWrapper::TYPE_STRING);
  282. $this->updated = true;
  283. $this->metadata[$key] = $meta->setValueString($value)->setIndexed($index);
  284. return $this;
  285. }
  286. /**
  287. * @param string $key metadata key
  288. * @param int $value metadata value
  289. * @param bool $index set TRUE if value must be indexed
  290. *
  291. * @inheritDoc
  292. * @return self
  293. * @throws FilesMetadataKeyFormatException
  294. * @since 28.0.0
  295. */
  296. public function setInt(string $key, int $value, bool $index = false): IFilesMetadata {
  297. $this->confirmKeyFormat($key);
  298. try {
  299. if ($this->getInt($key) === $value && $index === $this->isIndex($key)) {
  300. return $this; // we ignore if value have not changed
  301. }
  302. } catch (FilesMetadataNotFoundException|FilesMetadataTypeException $e) {
  303. // if value does not exist, or type has changed, we keep on the writing
  304. }
  305. $meta = new MetadataValueWrapper(IMetadataValueWrapper::TYPE_INT);
  306. $this->metadata[$key] = $meta->setValueInt($value)->setIndexed($index);
  307. $this->updated = true;
  308. return $this;
  309. }
  310. /**
  311. * @param string $key metadata key
  312. * @param float $value metadata value
  313. *
  314. * @inheritDoc
  315. * @return self
  316. * @throws FilesMetadataKeyFormatException
  317. * @since 28.0.0
  318. */
  319. public function setFloat(string $key, float $value, bool $index = false): IFilesMetadata {
  320. $this->confirmKeyFormat($key);
  321. try {
  322. if ($this->getFloat($key) === $value && $index === $this->isIndex($key)) {
  323. return $this; // we ignore if value have not changed
  324. }
  325. } catch (FilesMetadataNotFoundException|FilesMetadataTypeException $e) {
  326. // if value does not exist, or type has changed, we keep on the writing
  327. }
  328. $meta = new MetadataValueWrapper(IMetadataValueWrapper::TYPE_FLOAT);
  329. $this->metadata[$key] = $meta->setValueFloat($value)->setIndexed($index);
  330. $this->updated = true;
  331. return $this;
  332. }
  333. /**
  334. * @param string $key metadata key
  335. * @param bool $value metadata value
  336. * @param bool $index set TRUE if value must be indexed
  337. *
  338. * @inheritDoc
  339. * @return self
  340. * @throws FilesMetadataKeyFormatException
  341. * @since 28.0.0
  342. */
  343. public function setBool(string $key, bool $value, bool $index = false): IFilesMetadata {
  344. $this->confirmKeyFormat($key);
  345. try {
  346. if ($this->getBool($key) === $value && $index === $this->isIndex($key)) {
  347. return $this; // we ignore if value have not changed
  348. }
  349. } catch (FilesMetadataNotFoundException|FilesMetadataTypeException $e) {
  350. // if value does not exist, or type has changed, we keep on the writing
  351. }
  352. $meta = new MetadataValueWrapper(IMetadataValueWrapper::TYPE_BOOL);
  353. $this->metadata[$key] = $meta->setValueBool($value)->setIndexed($index);
  354. $this->updated = true;
  355. return $this;
  356. }
  357. /**
  358. * @param string $key metadata key
  359. * @param array $value metadata value
  360. *
  361. * @inheritDoc
  362. * @return self
  363. * @throws FilesMetadataKeyFormatException
  364. * @since 28.0.0
  365. */
  366. public function setArray(string $key, array $value): IFilesMetadata {
  367. $this->confirmKeyFormat($key);
  368. try {
  369. if ($this->getArray($key) === $value) {
  370. return $this; // we ignore if value have not changed
  371. }
  372. } catch (FilesMetadataNotFoundException|FilesMetadataTypeException $e) {
  373. // if value does not exist, or type has changed, we keep on the writing
  374. }
  375. $meta = new MetadataValueWrapper(IMetadataValueWrapper::TYPE_ARRAY);
  376. $this->metadata[$key] = $meta->setValueArray($value);
  377. $this->updated = true;
  378. return $this;
  379. }
  380. /**
  381. * @param string $key metadata key
  382. * @param string[] $value metadata value
  383. * @param bool $index set TRUE if each values from the list must be indexed
  384. *
  385. * @inheritDoc
  386. * @return self
  387. * @throws FilesMetadataKeyFormatException
  388. * @since 28.0.0
  389. */
  390. public function setStringList(string $key, array $value, bool $index = false): IFilesMetadata {
  391. $this->confirmKeyFormat($key);
  392. try {
  393. if ($this->getStringList($key) === $value) {
  394. return $this; // we ignore if value have not changed
  395. }
  396. } catch (FilesMetadataNotFoundException|FilesMetadataTypeException $e) {
  397. // if value does not exist, or type has changed, we keep on the writing
  398. }
  399. $meta = new MetadataValueWrapper(IMetadataValueWrapper::TYPE_STRING_LIST);
  400. $this->metadata[$key] = $meta->setValueStringList($value)->setIndexed($index);
  401. $this->updated = true;
  402. return $this;
  403. }
  404. /**
  405. * @param string $key metadata key
  406. * @param int[] $value metadata value
  407. * @param bool $index set TRUE if each values from the list must be indexed
  408. *
  409. * @inheritDoc
  410. * @return self
  411. * @throws FilesMetadataKeyFormatException
  412. * @since 28.0.0
  413. */
  414. public function setIntList(string $key, array $value, bool $index = false): IFilesMetadata {
  415. $this->confirmKeyFormat($key);
  416. try {
  417. if ($this->getIntList($key) === $value) {
  418. return $this; // we ignore if value have not changed
  419. }
  420. } catch (FilesMetadataNotFoundException|FilesMetadataTypeException $e) {
  421. // if value does not exist, or type has changed, we keep on the writing
  422. }
  423. $valueWrapper = new MetadataValueWrapper(IMetadataValueWrapper::TYPE_INT_LIST);
  424. $this->metadata[$key] = $valueWrapper->setValueIntList($value)->setIndexed($index);
  425. $this->updated = true;
  426. return $this;
  427. }
  428. /**
  429. * @param string $key metadata key
  430. *
  431. * @inheritDoc
  432. * @return self
  433. * @since 28.0.0
  434. */
  435. public function unset(string $key): IFilesMetadata {
  436. if (!array_key_exists($key, $this->metadata)) {
  437. return $this;
  438. }
  439. unset($this->metadata[$key]);
  440. $this->updated = true;
  441. return $this;
  442. }
  443. /**
  444. * @param string $keyPrefix metadata key prefix
  445. *
  446. * @inheritDoc
  447. * @return self
  448. * @since 28.0.0
  449. */
  450. public function removeStartsWith(string $keyPrefix): IFilesMetadata {
  451. if ($keyPrefix === '') {
  452. return $this;
  453. }
  454. foreach ($this->getKeys() as $key) {
  455. if (str_starts_with($key, $keyPrefix)) {
  456. $this->unset($key);
  457. }
  458. }
  459. return $this;
  460. }
  461. /**
  462. * @param string $key
  463. *
  464. * @return void
  465. * @throws FilesMetadataKeyFormatException
  466. */
  467. private function confirmKeyFormat(string $key): void {
  468. $acceptedChars = ['-', '_'];
  469. if (ctype_alnum(str_replace($acceptedChars, '', $key))) {
  470. return;
  471. }
  472. throw new FilesMetadataKeyFormatException('key can only contains alphanumerical characters, and dash (-, _)');
  473. }
  474. /**
  475. * @inheritDoc
  476. * @return bool TRUE if metadata have been modified
  477. * @since 28.0.0
  478. */
  479. public function updated(): bool {
  480. return $this->updated;
  481. }
  482. public function jsonSerialize(bool $emptyValues = false): array {
  483. $data = [];
  484. foreach ($this->metadata as $metaKey => $metaValueWrapper) {
  485. $data[$metaKey] = $metaValueWrapper->jsonSerialize($emptyValues);
  486. }
  487. return $data;
  488. }
  489. /**
  490. * @return array<string, string|int|bool|float|string[]|int[]>
  491. */
  492. public function asArray(): array {
  493. $data = [];
  494. foreach ($this->metadata as $metaKey => $metaValueWrapper) {
  495. try {
  496. $data[$metaKey] = $metaValueWrapper->getValueAny();
  497. } catch (FilesMetadataNotFoundException $e) {
  498. // ignore exception
  499. }
  500. }
  501. return $data;
  502. }
  503. /**
  504. * @param array $data
  505. *
  506. * @inheritDoc
  507. * @return IFilesMetadata
  508. * @since 28.0.0
  509. */
  510. public function import(array $data): IFilesMetadata {
  511. foreach ($data as $k => $v) {
  512. $valueWrapper = new MetadataValueWrapper();
  513. $this->metadata[$k] = $valueWrapper->import($v);
  514. }
  515. $this->updated = false;
  516. return $this;
  517. }
  518. /**
  519. * import data from database to configure this model
  520. *
  521. * @param array $data
  522. * @param string $prefix
  523. *
  524. * @return IFilesMetadata
  525. * @throws FilesMetadataNotFoundException
  526. * @since 28.0.0
  527. */
  528. public function importFromDatabase(array $data, string $prefix = ''): IFilesMetadata {
  529. try {
  530. $this->syncToken = $data[$prefix . 'sync_token'] ?? '';
  531. return $this->import(
  532. json_decode(
  533. $data[$prefix . 'json'] ?? '[]',
  534. true,
  535. 512,
  536. JSON_THROW_ON_ERROR
  537. )
  538. );
  539. } catch (JsonException) {
  540. throw new FilesMetadataNotFoundException();
  541. }
  542. }
  543. }