InpxParser.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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({total: 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. const buf = await zipReader.extractToBuf(inpFile);
  57. await this.parseInp(buf, structure, parsedCallback);
  58. }
  59. if (this.chunk.length) {
  60. await parsedCallback(this.chunk);
  61. }
  62. } finally {
  63. zipReader.close();
  64. }
  65. }
  66. async parseInp(inpBuf, structure, parsedCallback) {
  67. const structLen = structure.length;
  68. const rows = inpBuf.toString().split('\n');
  69. for (const row of rows) {
  70. let line = row;
  71. if (!line)
  72. continue;
  73. if (line[line.length - 1] == '\x0D')
  74. line = line.substring(0, line.length - 1);
  75. //парсим запись
  76. const parts = line.split('\x04');
  77. const rec = {};
  78. const len = (parts.length > structLen ? structLen : parts.length);
  79. for (let i = 0; i < len; i++) {
  80. if (structure[i])
  81. rec[structure[i]] = parts[i];
  82. }
  83. //специальная обработка некоторых полей
  84. if (rec.author) {
  85. rec.author = rec.author.split(':').map(s => s.replace(/,/g, ' ').trim()).filter(s => s).join(',');
  86. }
  87. if (rec.genre) {
  88. rec.genre = rec.genre.split(':').filter(s => s).join(',');
  89. }
  90. rec.serno = parseInt(rec.serno, 10) || 0;
  91. rec.size = parseInt(rec.size, 10) || 0;
  92. rec.del = parseInt(rec.del, 10) || 0;
  93. rec.insno = parseInt(rec.insno, 10) || 0;
  94. rec.librate = parseInt(rec.librate, 10) || 0;
  95. //пушим
  96. this.chunk.push(rec);
  97. if (this.chunk.length >= 10000) {
  98. await parsedCallback(this.chunk);
  99. this.chunk = [];
  100. }
  101. }
  102. }
  103. get info() {
  104. return this.inpxInfo;
  105. }
  106. }
  107. module.exports = InpxParser;