index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. const { app } = require('electron');
  2. const { autoUpdater } = require("electron-updater");
  3. const is = require('electron-is');
  4. const Log = require('ee-core/log');
  5. const Conf = require('ee-core/config');
  6. const Electron = require('ee-core/electron');
  7. const EE = require('ee-core/ee');
  8. /**
  9. * 自动升级插件
  10. * @class
  11. */
  12. class AutoUpdaterAddon {
  13. constructor(app) {
  14. this.app = app;
  15. }
  16. /**
  17. * 创建
  18. */
  19. create () {
  20. Log.info('[addon:autoUpdater] load');
  21. const cfg = Conf.getValue('addons.autoUpdater');
  22. if ((is.windows() && cfg.windows)
  23. || (is.macOS() && cfg.macOS)
  24. || (is.linux() && cfg.linux))
  25. {
  26. // continue
  27. } else {
  28. return
  29. }
  30. // 是否检查更新
  31. if (cfg.force) {
  32. this.checkUpdate();
  33. }
  34. const status = {
  35. error: -1,
  36. available: 1,
  37. noAvailable: 2,
  38. downloading: 3,
  39. downloaded: 4,
  40. }
  41. const version = app.getVersion();
  42. Log.info('[addon:autoUpdater] current version: ', version);
  43. // 设置下载服务器地址
  44. let server = cfg.options.url;
  45. let lastChar = server.substring(server.length - 1);
  46. server = lastChar === '/' ? server : server + "/";
  47. //Log.info('[addon:autoUpdater] server: ', server);
  48. cfg.options.url = server;
  49. // 是否后台自动下载
  50. autoUpdater.autoDownload = cfg.force ? true : false;
  51. try {
  52. autoUpdater.setFeedURL(cfg.options);
  53. } catch (error) {
  54. Log.error('[addon:autoUpdater] setFeedURL error : ', error);
  55. }
  56. autoUpdater.on('checking-for-update', () => {
  57. //sendStatusToWindow('正在检查更新...');
  58. })
  59. autoUpdater.on('update-available', (info) => {
  60. info.status = status.available;
  61. info.desc = '有可用更新';
  62. this.sendStatusToWindow(info);
  63. })
  64. autoUpdater.on('update-not-available', (info) => {
  65. info.status = status.noAvailable;
  66. info.desc = '没有可用更新';
  67. this.sendStatusToWindow(info);
  68. })
  69. autoUpdater.on('error', (err) => {
  70. let info = {
  71. status: status.error,
  72. desc: err
  73. }
  74. this.sendStatusToWindow(info);
  75. })
  76. autoUpdater.on('download-progress', (progressObj) => {
  77. let percentNumber = parseInt(progressObj.percent);
  78. let totalSize = this.bytesChange(progressObj.total);
  79. let transferredSize = this.bytesChange(progressObj.transferred);
  80. let text = '已下载 ' + percentNumber + '%';
  81. text = text + ' (' + transferredSize + "/" + totalSize + ')';
  82. let info = {
  83. status: status.downloading,
  84. desc: text,
  85. percentNumber: percentNumber,
  86. totalSize: totalSize,
  87. transferredSize: transferredSize
  88. }
  89. Log.info('[addon:autoUpdater] progress: ', text);
  90. this.sendStatusToWindow(info);
  91. })
  92. autoUpdater.on('update-downloaded', (info) => {
  93. info.status = status.downloaded;
  94. info.desc = '下载完成';
  95. this.sendStatusToWindow(info);
  96. // quit and update
  97. EE.app.appQuit();
  98. autoUpdater.quitAndInstall();
  99. });
  100. }
  101. /**
  102. * 检查更新
  103. */
  104. checkUpdate () {
  105. autoUpdater.checkForUpdates();
  106. }
  107. /**
  108. * 下载更新
  109. */
  110. download () {
  111. autoUpdater.downloadUpdate();
  112. }
  113. /**
  114. * 向前端发消息
  115. */
  116. sendStatusToWindow(content = {}) {
  117. const textJson = JSON.stringify(content);
  118. const channel = 'app.updater';
  119. Electron.mainWindow.webContents.send(channel, textJson);
  120. }
  121. /**
  122. * 单位转换
  123. */
  124. bytesChange (limit) {
  125. let size = "";
  126. if(limit < 0.1 * 1024){
  127. size = limit.toFixed(2) + "B";
  128. }else if(limit < 0.1 * 1024 * 1024){
  129. size = (limit/1024).toFixed(2) + "KB";
  130. }else if(limit < 0.1 * 1024 * 1024 * 1024){
  131. size = (limit/(1024 * 1024)).toFixed(2) + "MB";
  132. }else{
  133. size = (limit/(1024 * 1024 * 1024)).toFixed(2) + "GB";
  134. }
  135. let sizeStr = size + "";
  136. let index = sizeStr.indexOf(".");
  137. let dou = sizeStr.substring(index + 1 , index + 3);
  138. if(dou == "00"){
  139. return sizeStr.substring(0, index) + sizeStr.substring(index + 3, index + 5);
  140. }
  141. return size;
  142. }
  143. }
  144. AutoUpdaterAddon.toString = () => '[class AutoUpdaterAddon]';
  145. module.exports = AutoUpdaterAddon;