InpxParser.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const path = require('path');
  2. const ZipReader = require('./ZipReader');
  3. const collectionInfo = 'collection.info';
  4. const structureInfo = 'structure.info';
  5. const versionInfo = 'version.info';
  6. const defaultStructure = 'AUTHOR;GENRE;TITLE;SERIES;SERNO;FILE;SIZE;LIBID;DEL;EXT;DATE;LANG;LIBRATE;KEYWORDS';
  7. class InpxParser {
  8. constructor() {
  9. this.inpxInfo = {};
  10. }
  11. async safeExtractToString(zipReader, fileName) {
  12. let result = '';
  13. try {
  14. result = (await zipReader.extractToBuf(fileName)).toString().trim();
  15. } catch (e) {
  16. //quiet
  17. }
  18. return result;
  19. }
  20. async parse(inpxFile, readFileCallback, parsedCallback) {
  21. if (!readFileCallback)
  22. readFileCallback = async() => {};
  23. if (!parsedCallback)
  24. parsedCallback = async() => {};
  25. const zipReader = new ZipReader();
  26. await zipReader.open(inpxFile);
  27. try {
  28. const info = this.inpxInfo;
  29. //посчитаем inp-файлы
  30. const entries = Object.values(zipReader.entries);
  31. const inpFiles = [];
  32. for (const entry of entries) {
  33. if (!entry.isDirectory && path.extname(entry.name) == '.inp')
  34. inpFiles.push(entry.name);
  35. }
  36. //плюс 3 файла .info
  37. await readFileCallback({totalFiles: inpFiles.length + 3});
  38. let current = 0;
  39. //info
  40. await readFileCallback({fileName: collectionInfo, current: ++current});
  41. info.collection = await this.safeExtractToString(zipReader, collectionInfo);
  42. await readFileCallback({fileName: structureInfo, current: ++current});
  43. info.structure = await this.safeExtractToString(zipReader, structureInfo);
  44. await readFileCallback({fileName: versionInfo, current: ++current});
  45. info.version = await this.safeExtractToString(zipReader, versionInfo);
  46. //структура
  47. let inpxStructure = info.structure;
  48. if (!inpxStructure)
  49. inpxStructure = defaultStructure;
  50. inpxStructure = inpxStructure.toLowerCase();
  51. const structure = inpxStructure.split(';');
  52. //парсим inp-файлы
  53. this.chunk = [];
  54. for (const inpFile of inpFiles) {
  55. await readFileCallback({fileName: inpFile, current: ++current});
  56. await this.parseInp(zipReader, inpFile, structure, parsedCallback);
  57. }
  58. if (this.chunk.length) {
  59. await parsedCallback(this.chunk);
  60. }
  61. } finally {
  62. await zipReader.close();
  63. }
  64. }
  65. async parseInp(zipReader, inpFile, structure, parsedCallback) {
  66. const inpBuf = await zipReader.extractToBuf(inpFile);
  67. const rows = inpBuf.toString().split('\n');
  68. const defaultFolder = `${path.basename(inpFile, '.inp')}.zip`;
  69. const structLen = structure.length;
  70. for (const row of rows) {
  71. let line = row;
  72. if (!line)
  73. continue;
  74. if (line[line.length - 1] == '\x0D')
  75. line = line.substring(0, line.length - 1);
  76. //парсим запись
  77. const parts = line.split('\x04');
  78. const rec = {};
  79. const len = (parts.length > structLen ? structLen : parts.length);
  80. for (let i = 0; i < len; i++) {
  81. if (structure[i])
  82. rec[structure[i]] = parts[i];
  83. }
  84. //специальная обработка некоторых полей
  85. if (rec.author) {
  86. rec.author = rec.author.split(':').map(s => s.replace(/,/g, ' ').trim()).filter(s => s).join(',');
  87. }
  88. if (rec.genre) {
  89. rec.genre = rec.genre.split(':').filter(s => s).join(',');
  90. }
  91. if (!rec.folder)
  92. rec.folder = defaultFolder;
  93. rec.serno = parseInt(rec.serno, 10) || 0;
  94. rec.size = parseInt(rec.size, 10) || 0;
  95. rec.del = parseInt(rec.del, 10) || 0;
  96. rec.insno = parseInt(rec.insno, 10) || 0;
  97. rec.librate = parseInt(rec.librate, 10) || 0;
  98. //пушим
  99. this.chunk.push(rec);
  100. if (this.chunk.length >= 10000) {
  101. await parsedCallback(this.chunk);
  102. this.chunk = [];
  103. }
  104. }
  105. }
  106. get info() {
  107. return this.inpxInfo;
  108. }
  109. }
  110. module.exports = InpxParser;