OrphanHelper.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Files_Sharing;
  23. use OC\User\NoUserException;
  24. use OCP\DB\QueryBuilder\IQueryBuilder;
  25. use OCP\Files\IRootFolder;
  26. use OCP\IDBConnection;
  27. class OrphanHelper {
  28. private IDBConnection $connection;
  29. private IRootFolder $rootFolder;
  30. public function __construct(
  31. IDBConnection $connection,
  32. IRootFolder $rootFolder
  33. ) {
  34. $this->connection = $connection;
  35. $this->rootFolder = $rootFolder;
  36. }
  37. public function isShareValid(string $owner, int $fileId): bool {
  38. try {
  39. $userFolder = $this->rootFolder->getUserFolder($owner);
  40. } catch (NoUserException $e) {
  41. return false;
  42. }
  43. $node = $userFolder->getFirstNodeById($fileId);
  44. return $node !== null;
  45. }
  46. /**
  47. * @param int[] $ids
  48. * @return void
  49. */
  50. public function deleteShares(array $ids): void {
  51. $query = $this->connection->getQueryBuilder();
  52. $query->delete('share')
  53. ->where($query->expr()->in('id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
  54. $query->executeStatement();
  55. }
  56. public function fileExists(int $fileId): bool {
  57. $query = $this->connection->getQueryBuilder();
  58. $query->select('fileid')
  59. ->from('filecache')
  60. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  61. return $query->executeQuery()->fetchOne() !== false;
  62. }
  63. /**
  64. * @return \Traversable<int, array{id: int, owner: string, fileid: int, target: string}>
  65. */
  66. public function getAllShares() {
  67. $query = $this->connection->getQueryBuilder();
  68. $query->select('id', 'file_source', 'uid_owner', 'file_target')
  69. ->from('share')
  70. ->where($query->expr()->eq('item_type', $query->createNamedParameter('file')))
  71. ->orWhere($query->expr()->eq('item_type', $query->createNamedParameter('folder')));
  72. $result = $query->executeQuery();
  73. while ($row = $result->fetch()) {
  74. yield [
  75. 'id' => (int)$row['id'],
  76. 'owner' => (string)$row['uid_owner'],
  77. 'fileid' => (int)$row['file_source'],
  78. 'target' => (string)$row['file_target'],
  79. ];
  80. }
  81. }
  82. }