OrphanHelper.php 2.1 KB

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