FSAccessContext.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 FSNodeContext = require("./FSNodeContext");
  20. const { NodePathSelector, NodeUIDSelector, NodeInternalIDSelector } = require("./node/selectors");
  21. /**
  22. * Container for access implementations.
  23. *
  24. * Access implementations may vary depending on region,
  25. * user privileges, and other factors.
  26. *
  27. * @class FSAccessContext
  28. */
  29. module.exports = class FSAccessContext {
  30. constructor () {
  31. this.fsEntryFetcher = null;
  32. }
  33. /**
  34. * get_entry_by_path() returns a filesystem entry using
  35. * the path to the entry. Use this method when you need
  36. * to get a filesystem entry but don't need to collect
  37. * any other information about the entry.
  38. *
  39. * @warning The entry returned by this method is not
  40. * client-safe. Use FSNodeContext to get a client-safe
  41. * entry by calling it's fetchEntry() method.
  42. *
  43. * @param {*} path
  44. * @returns
  45. * @deprecated use get_entry({ path }) instead
  46. */
  47. async get_entry_by_path (path) {
  48. return await this.get_entry({ path });
  49. }
  50. /**
  51. * get_entry() returns a filesystem entry using
  52. * path, uid, or id associated with a filesystem
  53. * node. Use this method when you need to get a
  54. * filesystem entry but don't need to collect any
  55. * other information about the entry.
  56. *
  57. * @warning The entry returned by this method is not
  58. * client-safe. Use FSNodeContext to get a client-safe
  59. * entry by calling it's fetchEntry() method.
  60. *
  61. * @param {*} param0 options for getting the entry
  62. * @param {*} param0.path
  63. * @param {*} param0.uid
  64. * @param {*} param0.id please use mysql_id instead
  65. * @param {*} param0.mysql_id
  66. */
  67. async get_entry ({ path, uid, id, mysql_id, ...options }) {
  68. let fsNode = await this.node({ path, uid, id, mysql_id });
  69. await fsNode.fetchEntry(options);
  70. return fsNode.entry;
  71. }
  72. /**
  73. * node() returns a filesystem node using path, uid,
  74. * or id associated with a filesystem node. Use this
  75. * method when you need to get a filesystem node and
  76. * need to collect information about the entry.
  77. *
  78. * @param {*} location - path, uid, or id associated with a filesystem node
  79. * @returns
  80. */
  81. async node (selector) {
  82. if ( typeof selector === 'string' ) {
  83. if ( selector.startsWith('/') ) {
  84. selector = new NodePathSelector(selector);
  85. } else {
  86. selector = new NodeUIDSelector(selector);
  87. }
  88. }
  89. // TEMP: remove when these objects aren't used anymore
  90. if (
  91. typeof selector === 'object' &&
  92. selector.constructor.name === 'Object'
  93. ) {
  94. if ( selector.path ) {
  95. selector = new NodePathSelector(selector.path);
  96. } else if ( selector.uid ) {
  97. selector = new NodeUIDSelector(selector.uid);
  98. } else {
  99. selector = new NodeInternalIDSelector(
  100. 'mysql', selector.mysql_id);
  101. }
  102. }
  103. let fsNode = new FSNodeContext({
  104. services: this.services,
  105. selector,
  106. fs: this
  107. });
  108. return fsNode;
  109. }
  110. };