OrphanHelper.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 OCP\DB\QueryBuilder\IQueryBuilder;
  24. use OCP\Files\IRootFolder;
  25. use OCP\IDBConnection;
  26. class OrphanHelper {
  27. private IDBConnection $connection;
  28. private IRootFolder $rootFolder;
  29. public function __construct(
  30. IDBConnection $connection,
  31. IRootFolder $rootFolder
  32. ) {
  33. $this->connection = $connection;
  34. $this->rootFolder = $rootFolder;
  35. }
  36. public function isShareValid(string $owner, int $fileId): bool {
  37. $userFolder = $this->rootFolder->getUserFolder($owner);
  38. $nodes = $userFolder->getById($fileId);
  39. return count($nodes) > 0;
  40. }
  41. /**
  42. * @param int[] $ids
  43. * @return void
  44. */
  45. public function deleteShares(array $ids): void {
  46. $query = $this->connection->getQueryBuilder();
  47. $query->delete('share')
  48. ->where($query->expr()->in('id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
  49. $query->executeStatement();
  50. }
  51. public function fileExists(int $fileId): bool {
  52. $query = $this->connection->getQueryBuilder();
  53. $query->select('fileid')
  54. ->from('filecache')
  55. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  56. return $query->executeQuery()->fetchOne() !== false;
  57. }
  58. /**
  59. * @return \Traversable<int, array{id: int, owner: string, fileid: int, target: string}>
  60. */
  61. public function getAllShares() {
  62. $query = $this->connection->getQueryBuilder();
  63. $query->select('id', 'file_source', 'uid_owner', 'file_target')
  64. ->from('share')
  65. ->where($query->expr()->eq('item_type', $query->createNamedParameter('file')))
  66. ->orWhere($query->expr()->eq('item_type', $query->createNamedParameter('folder')));
  67. $result = $query->executeQuery();
  68. while ($row = $result->fetch()) {
  69. yield [
  70. 'id' => (int)$row['id'],
  71. 'owner' => (string)$row['uid_owner'],
  72. 'fileid' => (int)$row['file_source'],
  73. 'target' => (string)$row['file_target'],
  74. ];
  75. }
  76. }
  77. }