1
0

FilesMetadata.php 15 KB

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