bookManager.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import localForage from 'localforage';
  2. import * as utils from '../../../share/utils';
  3. import BookParser from './BookParser';
  4. const maxDataSize = 300*1024*1024;//compressed bytes
  5. const bmMetaStore = localForage.createInstance({
  6. name: 'bmMetaStore'
  7. });
  8. const bmDataStore = localForage.createInstance({
  9. name: 'bmDataStore'
  10. });
  11. const bmRecentStore = localForage.createInstance({
  12. name: 'bmRecentStore'
  13. });
  14. const bmCacheStore = localForage.createInstance({
  15. name: 'bmCacheStore'
  16. });
  17. class BookManager {
  18. async init(settings) {
  19. this.settings = settings;
  20. //bmCacheStore нужен только для ускорения загрузки читалки
  21. this.booksCached = await bmCacheStore.getItem('books');
  22. if (!this.booksCached)
  23. this.booksCached = {};
  24. this.recent = await bmCacheStore.getItem('recent');
  25. this.recentLast = await bmCacheStore.getItem('recent-last');
  26. if (this.recentLast)
  27. this.recent[this.recentLast.key] = this.recentLast;
  28. this.books = Object.assign({}, this.booksCached);
  29. this.recentChanged1 = true;
  30. this.recentChanged2 = true;
  31. if (!this.books || !this.recent) {
  32. this.books = {};
  33. this.recent = {};
  34. await this.loadMeta(true);
  35. } else {
  36. this.loadMeta(false);
  37. }
  38. }
  39. //долгая загрузка из хранилища,
  40. //хранение в отдельных записях дает относительно
  41. //нормальное поведение при нескольких вкладках с читалкой в браузере
  42. async loadMeta(immediate) {
  43. if (!immediate)
  44. await utils.sleep(2000);
  45. let len = await bmMetaStore.length();
  46. for (let i = 0; i < len; i++) {
  47. const key = await bmMetaStore.key(i);
  48. const keySplit = key.split('-');
  49. if (keySplit.length == 2 && keySplit[0] == 'bmMeta') {
  50. let meta = await bmMetaStore.getItem(key);
  51. const oldBook = this.books[meta.key];
  52. this.books[meta.key] = meta;
  53. if (oldBook && oldBook.parsed) {
  54. this.books[meta.key].parsed = oldBook.parsed;
  55. }
  56. }
  57. }
  58. len = await bmRecentStore.length();
  59. for (let i = 0; i < len; i++) {
  60. const key = await bmRecentStore.key(i);
  61. let r = await bmRecentStore.getItem(key);
  62. this.recent[r.key] = r;
  63. }
  64. await this.cleanBooks();
  65. await this.cleanRecentBooks();
  66. this.booksCached = {};
  67. for (const key in this.books) {
  68. this.booksCached[key] = this.metaOnly(this.books[key]);
  69. }
  70. await bmCacheStore.setItem('books', this.booksCached);
  71. await bmCacheStore.setItem('recent', this.recent);
  72. }
  73. async cleanBooks() {
  74. while (1) {// eslint-disable-line no-constant-condition
  75. let size = 0;
  76. let min = Date.now();
  77. let toDel = null;
  78. for (let key in this.books) {
  79. let book = this.books[key];
  80. const bookLength = (book.length ? book.length : 0);
  81. size += (book.dataCompressedLength ? book.dataCompressedLength : bookLength);
  82. if (book.addTime < min) {
  83. toDel = book;
  84. min = book.addTime;
  85. }
  86. }
  87. if (size > maxDataSize && toDel) {
  88. await this._delBook(toDel);
  89. } else {
  90. break;
  91. }
  92. }
  93. }
  94. async addBook(newBook, callback) {
  95. if (!this.books)
  96. await this.init();
  97. let meta = {url: newBook.url, path: newBook.path};
  98. meta.key = this.keyFromUrl(meta.url);
  99. meta.addTime = Date.now();
  100. const cb = (perc) => {
  101. const p = Math.round(80*perc/100);
  102. callback(p);
  103. };
  104. const result = await this.parseBook(meta, newBook.data, cb);
  105. result.dataCompressed = true;
  106. let data = newBook.data;
  107. if (result.dataCompressed) {
  108. data = utils.pako.deflate(data, {level: 9});
  109. result.dataCompressedLength = data.byteLength;
  110. }
  111. callback(90);
  112. this.books[meta.key] = result;
  113. this.booksCached[meta.key] = this.metaOnly(result);
  114. await bmMetaStore.setItem(`bmMeta-${meta.key}`, this.metaOnly(result));
  115. await bmDataStore.setItem(`bmData-${meta.key}`, data);
  116. await bmCacheStore.setItem('books', this.booksCached);
  117. callback(100);
  118. return result;
  119. }
  120. hasBookParsed(meta) {
  121. if (!this.books)
  122. return false;
  123. if (!meta.url)
  124. return false;
  125. if (!meta.key)
  126. meta.key = this.keyFromUrl(meta.url);
  127. let book = this.books[meta.key];
  128. return !!(book && book.parsed);
  129. }
  130. async getBook(meta, callback) {
  131. if (!this.books)
  132. await this.init();
  133. let result = undefined;
  134. if (!meta.key)
  135. meta.key = this.keyFromUrl(meta.url);
  136. result = this.books[meta.key];
  137. if (result && !result.parsed) {
  138. let data = await bmDataStore.getItem(`bmData-${meta.key}`);
  139. callback(10);
  140. await utils.sleep(10);
  141. if (result.dataCompressed) {
  142. data = utils.pako.inflate(data, {to: 'string'});
  143. }
  144. callback(20);
  145. const cb = (perc) => {
  146. const p = 20 + Math.round(80*perc/100);
  147. callback(p);
  148. };
  149. result = await this.parseBook(result, data, cb);
  150. this.books[meta.key] = result;
  151. }
  152. return result;
  153. }
  154. async _delBook(meta) {
  155. await bmMetaStore.removeItem(`bmMeta-${meta.key}`);
  156. await bmDataStore.removeItem(`bmData-${meta.key}`);
  157. delete this.books[meta.key];
  158. delete this.booksCached[meta.key];
  159. }
  160. async delBook(meta) {
  161. if (!this.books)
  162. await this.init();
  163. await this._delBook(meta);
  164. await bmCacheStore.setItem('books', this.booksCached);
  165. }
  166. async parseBook(meta, data, callback) {
  167. if (!this.books)
  168. await this.init();
  169. const parsed = new BookParser(this.settings);
  170. const parsedMeta = await parsed.parse(data, callback);
  171. const result = Object.assign({}, meta, parsedMeta, {
  172. length: data.length,
  173. textLength: parsed.textLength,
  174. parsed
  175. });
  176. return result;
  177. }
  178. metaOnly(book) {
  179. let result = Object.assign({}, book);
  180. delete result.data;//можно будет убрать эту строку со временем
  181. delete result.parsed;
  182. return result;
  183. }
  184. keyFromUrl(url) {
  185. return utils.stringToHex(url);
  186. }
  187. async setRecentBook(value, noTouch) {
  188. if (!this.recent)
  189. await this.init();
  190. const result = this.metaOnly(value);
  191. if (!noTouch)
  192. result.touchTime = Date.now();
  193. if (result.textLength && !result.bookPos && result.bookPosPercent)
  194. result.bookPos = Math.round(result.bookPosPercent*result.textLength);
  195. this.recent[result.key] = result;
  196. await bmRecentStore.setItem(result.key, result);
  197. //кэшируем, аккуратно
  198. if (!(this.recentLast && this.recentLast.key == result.key)) {
  199. await bmCacheStore.setItem('recent', this.recent);
  200. }
  201. this.recentLast = result;
  202. await bmCacheStore.setItem('recent-last', this.recentLast);
  203. this.recentChanged1 = true;
  204. this.recentChanged2 = true;
  205. return result;
  206. }
  207. async getRecentBook(value) {
  208. if (!this.recent)
  209. await this.init();
  210. return this.recent[value.key];
  211. }
  212. async delRecentBook(value) {
  213. if (!this.recent)
  214. await this.init();
  215. await bmRecentStore.removeItem(value.key);
  216. delete this.recent[value.key];
  217. await bmCacheStore.setItem('recent', this.recent);
  218. this.recentChanged1 = true;
  219. this.recentChanged2 = true;
  220. }
  221. async cleanRecentBooks() {
  222. if (!this.recent)
  223. await this.init();
  224. if (Object.keys(this.recent).length > 1000) {
  225. let min = Date.now();
  226. let found = null;
  227. for (let key in this.recent) {
  228. const book = this.recent[key];
  229. if (book.touchTime < min) {
  230. min = book.touchTime;
  231. found = book;
  232. }
  233. }
  234. if (found) {
  235. await this.delRecentBook(found);
  236. await this.cleanRecentBooks();
  237. }
  238. }
  239. }
  240. mostRecentBook() {
  241. if (!this.recentChanged1 && this.mostRecentCached) {
  242. return this.mostRecentCached;
  243. }
  244. let max = 0;
  245. let result = null;
  246. for (let key in this.recent) {
  247. const book = this.recent[key];
  248. if (book.touchTime > max) {
  249. max = book.touchTime;
  250. result = book;
  251. }
  252. }
  253. this.mostRecentCached = result;
  254. this.recentChanged1 = false;
  255. return result;
  256. }
  257. getSortedRecent() {
  258. if (!this.recentChanged2 && this.sortedRecentCached) {
  259. return this.sortedRecentCached;
  260. }
  261. let result = Object.values(this.recent);
  262. result.sort((a, b) => b.touchTime - a.touchTime);
  263. this.sortedRecentCached = result;
  264. this.recentChanged2 = false;
  265. return result;
  266. }
  267. }
  268. export default new BookManager();