No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

persistence.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Persistence = void 0;
  4. const fs_1 = require("fs");
  5. const rimraf = require("rimraf");
  6. const fs = require("fs");
  7. const fse = require("fs-extra");
  8. const path = require("path");
  9. const uuid = require("uuid");
  10. class Persistence {
  11. constructor(dirPath) {
  12. this._diskPathMap = new Map();
  13. this.reset(dirPath);
  14. }
  15. reset(dirPath) {
  16. this._dirPath = dirPath;
  17. (0, fs_1.mkdirSync)(dirPath, {
  18. recursive: true,
  19. });
  20. this._diskPathMap = new Map();
  21. }
  22. get dirPath() {
  23. return this._dirPath;
  24. }
  25. appendBytes(fileName, bytes) {
  26. if (!this._diskPathMap.has(fileName)) {
  27. this._diskPathMap.set(fileName, this.generateNewDiskName());
  28. }
  29. const filepath = this.getDiskPath(fileName);
  30. fs.appendFileSync(filepath, bytes);
  31. return filepath;
  32. }
  33. readBytes(fileName, size, fileOffset) {
  34. let fd;
  35. try {
  36. fd = (0, fs_1.openSync)(this.getDiskPath(fileName), "r");
  37. const buf = Buffer.alloc(size);
  38. const offset = fileOffset && fileOffset > 0 ? fileOffset : 0;
  39. (0, fs_1.readSync)(fd, buf, 0, size, offset);
  40. return buf;
  41. }
  42. finally {
  43. if (fd) {
  44. (0, fs_1.closeSync)(fd);
  45. }
  46. }
  47. }
  48. deleteFile(fileName, failSilently = false) {
  49. try {
  50. (0, fs_1.unlinkSync)(this.getDiskPath(fileName));
  51. }
  52. catch (err) {
  53. if (!failSilently) {
  54. throw err;
  55. }
  56. }
  57. this._diskPathMap.delete(fileName);
  58. }
  59. deleteAll() {
  60. return new Promise((resolve, reject) => {
  61. rimraf(this._dirPath, (err) => {
  62. if (err) {
  63. reject(err);
  64. }
  65. else {
  66. this._diskPathMap = new Map();
  67. resolve();
  68. }
  69. });
  70. });
  71. }
  72. renameFile(oldName, newName) {
  73. const oldNameId = this.getDiskFileName(oldName);
  74. this._diskPathMap.set(newName, oldNameId);
  75. this._diskPathMap.delete(oldName);
  76. }
  77. getDiskPath(fileName) {
  78. const shortenedDiskPath = this.getDiskFileName(fileName);
  79. return path.join(this._dirPath, encodeURIComponent(shortenedDiskPath));
  80. }
  81. getDiskFileName(fileName) {
  82. return this._diskPathMap.get(fileName);
  83. }
  84. copyFromExternalPath(sourcePath, newName) {
  85. this._diskPathMap.set(newName, this.generateNewDiskName());
  86. fse.copyFileSync(sourcePath, this.getDiskPath(newName));
  87. }
  88. generateNewDiskName() {
  89. return uuid.v4();
  90. }
  91. }
  92. exports.Persistence = Persistence;