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