reader.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import axios from 'axios';
  2. import * as utils from '../share/utils';
  3. const api = axios.create({
  4. baseURL: '/api/reader'
  5. });
  6. const workerApi = axios.create({
  7. baseURL: '/api/worker'
  8. });
  9. class Reader {
  10. async loadBook(url, callback) {
  11. const refreshPause = 300;
  12. if (!callback) callback = () => {};
  13. let response = await api.post('/load-book', {type: 'url', url});
  14. const workerId = response.data.workerId;
  15. if (!workerId)
  16. throw new Error('Неверный ответ api');
  17. callback({totalSteps: 4});
  18. let i = 0;
  19. while (1) {// eslint-disable-line no-constant-condition
  20. callback(response.data);
  21. if (response.data.state == 'finish') {//воркер закончил работу, можно скачивать кешированный на сервере файл
  22. callback({step: 4});
  23. const book = await this.loadCachedBook(response.data.path, callback);
  24. return Object.assign({}, response.data, {data: book.data});
  25. }
  26. if (response.data.state == 'error') {
  27. let errMes = response.data.error;
  28. if (errMes.indexOf('getaddrinfo') >= 0 ||
  29. errMes.indexOf('ECONNRESET') >= 0 ||
  30. errMes.indexOf('EINVAL') >= 0 ||
  31. errMes.indexOf('404') >= 0)
  32. errMes = `Ресурс не найден по адресу: ${response.data.url}`;
  33. throw new Error(errMes);
  34. }
  35. if (i > 0)
  36. await utils.sleep(refreshPause);
  37. i++;
  38. if (i > 120*1000/refreshPause) {//2 мин ждем телодвижений воркера
  39. throw new Error('Слишком долгое время ожидания');
  40. }
  41. //проверка воркера
  42. const prevProgress = response.data.progress;
  43. const prevState = response.data.state;
  44. response = await workerApi.post('/get-state', {workerId});
  45. i = (prevProgress != response.data.progress || prevState != response.data.state ? 1 : i);
  46. }
  47. }
  48. async loadCachedBook(url, callback){
  49. const response = await axios.head(url);
  50. let estSize = 1000000;
  51. if (response.headers['content-length']) {
  52. estSize = response.headers['content-length'];
  53. }
  54. callback({state: 'loading', progress: 0});
  55. const options = {
  56. onDownloadProgress: progress => {
  57. while (progress.loaded > estSize) estSize *= 1.5;
  58. if (callback)
  59. callback({progress: Math.round((progress.loaded*100)/estSize)});
  60. }
  61. }
  62. //загрузка
  63. return await axios.get(url, options);
  64. }
  65. async uploadFile(file, maxUploadFileSize, callback) {
  66. if (!maxUploadFileSize)
  67. maxUploadFileSize = 10*1024*1024;
  68. if (file.size > maxUploadFileSize)
  69. throw new Error(`Размер файла превышает ${maxUploadFileSize} байт`);
  70. let formData = new FormData();
  71. formData.append('file', file);
  72. const options = {
  73. headers: {
  74. 'Content-Type': 'multipart/form-data'
  75. },
  76. onUploadProgress: progress => {
  77. const total = (progress.total ? progress.total : progress.loaded + 200000);
  78. if (callback)
  79. callback({state: 'upload', progress: Math.round((progress.loaded*100)/total)});
  80. }
  81. };
  82. let response = await api.post('/upload-file', formData, options);
  83. if (response.data.state == 'error')
  84. throw new Error(response.data.error);
  85. const url = response.data.url;
  86. if (!url)
  87. throw new Error('Неверный ответ api');
  88. return url;
  89. }
  90. async storage(request) {
  91. let response = await api.post('/storage', request);
  92. const state = response.data.state;
  93. if (!state)
  94. throw new Error('Неверный ответ api');
  95. return response.data;
  96. }
  97. }
  98. export default new Reader();