ll_readdir.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. const APIError = require("../../api/APIError");
  20. const { chkperm, get_descendants } = require("../../helpers");
  21. const { TYPE_DIRECTORY } = require("../FSNodeContext");
  22. const { NodeUIDSelector, NodeRawEntrySelector } = require("../node/selectors");
  23. const { LLFilesystemOperation } = require("./definitions");
  24. class LLReadDir extends LLFilesystemOperation {
  25. async _run () {
  26. const { context } = this;
  27. const { subject, user, actor } = this.values;
  28. if ( ! await subject.exists() ) {
  29. throw APIError.create('subject_does_not_exist');
  30. }
  31. const svc_acl = context.get('services').get('acl');
  32. if ( ! await svc_acl.check(actor, subject, 'list') ) {
  33. throw await svc_acl.get_safe_acl_error(actor, subject, 'list');
  34. }
  35. const subject_uuid = await subject.get('uid');
  36. const svc = context.get('services');
  37. const svc_fsentry = svc.get('fsEntryService');
  38. const svc_fs = svc.get('filesystem');
  39. if (
  40. subject.isRoot ||
  41. (await subject.isUserDirectory() && subject.name !== user.username)
  42. ) {
  43. this.checkpoint('before call to get_descendants')
  44. const entries = await get_descendants(
  45. await subject.get('path'),
  46. user,
  47. 1, true,
  48. )
  49. this.checkpoint('after call to get_descendants')
  50. const children = await Promise.all(entries.map(async entry => {
  51. const node = await svc_fs.node(new NodeRawEntrySelector(entry));
  52. node.found_thumbnail = false;
  53. return node;
  54. }))
  55. this.checkpoint('after get children (2)');
  56. return children;
  57. }
  58. this.checkpoint('before get direct descendants')
  59. const child_uuids = await svc_fsentry
  60. .fast_get_direct_descendants(subject_uuid);
  61. this.checkpoint('after get direct descendants')
  62. const children = await Promise.all(child_uuids.map(async uuid => {
  63. return await svc_fs.node(new NodeUIDSelector(uuid));
  64. }));
  65. this.checkpoint('after get children');
  66. return children;
  67. }
  68. }
  69. module.exports = {
  70. LLReadDir,
  71. };