MegaStorage.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. const ZipStreamer = require('../ZipStreamer');
  4. const utils = require('../utils');
  5. class MegaStorage {
  6. constructor() {
  7. this.inited = false;
  8. }
  9. async init(config) {
  10. this.config = config;
  11. this.megaStorageDir = config.megaStorageDir;
  12. this.statsPath = `${this.megaStorageDir}/stats.json`;
  13. this.compressLevel = (config.compressLevel ? config.compressLevel : 4);
  14. await fs.ensureDir(this.megaStorageDir);
  15. this.readingFiles = false;
  16. this.stats = {};
  17. if (await fs.pathExists(this.statsPath)) {
  18. this.stats = Object.assign({},
  19. JSON.parse(await fs.readFile(this.statsPath, 'utf8')),
  20. this.stats
  21. );
  22. }
  23. this.inited = true;
  24. }
  25. async nameHash(filename) {
  26. const hash = utils.toBase36(await utils.getFileHash(filename, 'sha1'));
  27. const hashPath = `${hash.substr(0, 2)}/${hash.substr(2, 2)}/${hash}`;
  28. const fullHashPath = `${this.megaStorageDir}/${hashPath}`;
  29. return {
  30. filename,
  31. hash,
  32. hashPath,
  33. fullHashPath,
  34. zipPath: `${fullHashPath}.zip`,
  35. descPath: `${fullHashPath}.desc`,
  36. };
  37. }
  38. async checkFileExists(nameHash) {
  39. return await fs.pathExists(nameHash.zipPath);
  40. }
  41. async addFile(nameHash, desc = null, force = false) {
  42. if (await this.checkFileExists(nameHash) && !force)
  43. return false;
  44. await fs.ensureDir(path.dirname(nameHash.zipPath));
  45. const zip = new ZipStreamer();
  46. let entry = {};
  47. let resultFile = await zip.pack(nameHash.zipPath, [nameHash.filename], {zlib: {level: this.compressLevel}}, (ent) => {
  48. entry = ent;
  49. });
  50. if (desc) {
  51. desc = Object.assign({}, desc, {fileSize: entry.size, zipFileSize: resultFile.size});
  52. this.updateDesc(nameHash, desc);
  53. }
  54. return desc;
  55. }
  56. async updateDesc(nameHash, desc) {
  57. await fs.writeFile(nameHash.descPath, JSON.stringify(desc, null, 2));
  58. }
  59. async _findFiles(callback, dir) {
  60. if (!callback || !this.readingFiles)
  61. return;
  62. if (!dir)
  63. dir = this.megaStorageDir;
  64. let result;
  65. const files = await fs.readdir(dir, { withFileTypes: true });
  66. for (const file of files) {
  67. if (!this.readingFiles)
  68. return;
  69. const found = path.resolve(dir, file.name);
  70. if (file.isDirectory())
  71. result = await this._findFiles(callback, found);
  72. else
  73. callback(found);
  74. }
  75. return result;
  76. }
  77. async startFindFiles(callback, dir) {
  78. this.readingFiles = true;
  79. try {
  80. return await this._findFiles(callback, dir);
  81. } finally {
  82. this.readingFiles = false;
  83. }
  84. }
  85. async stopFindFiles() {
  86. this.readingFiles = false;
  87. }
  88. async getStats(gather = false) {
  89. }
  90. }
  91. module.exports = MegaStorage;