1
0

OrphanHelper.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 OCA\Files_Sharing;
  8. use OC\User\NoUserException;
  9. use OCP\DB\QueryBuilder\IQueryBuilder;
  10. use OCP\Files\IRootFolder;
  11. use OCP\IDBConnection;
  12. class OrphanHelper {
  13. public function __construct(
  14. private IDBConnection $connection,
  15. private IRootFolder $rootFolder,
  16. ) {
  17. }
  18. public function isShareValid(string $owner, int $fileId): bool {
  19. try {
  20. $userFolder = $this->rootFolder->getUserFolder($owner);
  21. } catch (NoUserException $e) {
  22. return false;
  23. }
  24. $node = $userFolder->getFirstNodeById($fileId);
  25. return $node !== null;
  26. }
  27. /**
  28. * @param int[] $ids
  29. * @return void
  30. */
  31. public function deleteShares(array $ids): void {
  32. $query = $this->connection->getQueryBuilder();
  33. $query->delete('share')
  34. ->where($query->expr()->in('id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
  35. $query->executeStatement();
  36. }
  37. public function fileExists(int $fileId): bool {
  38. $query = $this->connection->getQueryBuilder();
  39. $query->select('fileid')
  40. ->from('filecache')
  41. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  42. return $query->executeQuery()->fetchOne() !== false;
  43. }
  44. /**
  45. * @return \Traversable<int, array{id: int, owner: string, fileid: int, target: string}>
  46. */
  47. public function getAllShares() {
  48. $query = $this->connection->getQueryBuilder();
  49. $query->select('id', 'file_source', 'uid_owner', 'file_target')
  50. ->from('share')
  51. ->where($query->expr()->eq('item_type', $query->createNamedParameter('file')))
  52. ->orWhere($query->expr()->eq('item_type', $query->createNamedParameter('folder')));
  53. $result = $query->executeQuery();
  54. while ($row = $result->fetch()) {
  55. yield [
  56. 'id' => (int)$row['id'],
  57. 'owner' => (string)$row['uid_owner'],
  58. 'fileid' => (int)$row['file_source'],
  59. 'target' => (string)$row['file_target'],
  60. ];
  61. }
  62. }
  63. }