github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/client/src/app/utils/comm.js (about) 1 import path from "path"; 2 import os from "os"; 3 import {App, downloadUrl, ResDir, WorkDir} from "./consts"; 4 import {app} from "electron"; 5 import {logInfo} from "./log"; 6 7 import {promisify} from 'node:util'; 8 import stream from 'node:stream'; 9 import fs from 'node:fs'; 10 import got from 'got'; 11 import crypto from "crypto"; 12 const pipeline = promisify(stream.pipeline); 13 14 export function mkdir(dir) { 15 const pth = path.join(WorkDir, dir); 16 fs.mkdirSync(pth, {recursive:true}) 17 18 return pth 19 } 20 21 export function getResDir(dir) { 22 const pth = path.resolve(process.resourcesPath, dir); 23 return pth 24 } 25 26 export function getDownloadPath(version) { 27 const pth = path.join(WorkDir, 'tmp', 'download', `${version}.zip`); 28 return pth 29 } 30 31 32 export function getCurrVersion() { 33 let currVersionStr = '0'; 34 35 const {versionPath} = getResPath() 36 if (fs.existsSync(versionPath)) { 37 const content = fs.readFileSync(versionPath) 38 let json = JSON.parse(content); 39 currVersionStr = json.version; 40 } else { 41 currVersionStr = app.getVersion(); 42 } 43 44 const currVersion = parseFloat(currVersionStr); 45 46 return {currVersion, currVersionStr}; 47 } 48 49 export async function getRemoteVersion() { 50 const versionUrl = getVersionUrl(); 51 52 const json = await got.get(versionUrl).json(); 53 const newVersionStr = json.version; 54 const newVersion = parseFloat(newVersionStr); 55 const forceUpdate = json.force; 56 57 return { 58 newVersion, 59 newVersionStr, 60 forceUpdate, 61 } 62 } 63 64 export function changeVersion(newVersion) { 65 const pth = path.join(ResDir, 'version.json'); 66 logInfo(`ResDir=${ResDir}, pth=${pth}`) 67 68 let json = {} 69 if (fs.existsSync(pth)) { 70 const content = fs.readFileSync(pth).toString() 71 json = JSON.parse(content); 72 } 73 74 json.version = newVersion; 75 fs.writeFileSync(pth, JSON.stringify(json)); 76 } 77 78 export function getResPath() { 79 const versionPath = path.resolve(ResDir, 'version.json') 80 const uiPath = path.resolve(ResDir, 'ui'); 81 const serverPath = getBinPath('server') 82 83 return { 84 versionPath, uiPath, serverPath 85 } 86 } 87 88 export function getBinPath(name) { 89 const platform = os.platform(); // 'darwin', 'linux', 'win32' 90 const execPath = `bin/${platform}/${name}${platform === 'win32' ? '.exe' : ''}`; 91 const pth = path.join(ResDir, execPath); 92 93 return pth 94 } 95 96 export function computerFileMd5(pth) { 97 const buffer = fs.readFileSync(pth); 98 const hash = crypto.createHash('md5'); 99 hash.update(buffer, 'utf8'); 100 const md5 = hash.digest('hex') + ''; 101 return md5.trim() 102 } 103 104 export function getVersionUrl() { 105 const url = new URL(`${App}/version.json`, downloadUrl) + '?ts=' + Date.now(); 106 logInfo(`versionUrl=${url}`) 107 return url 108 } 109 export function getAppUrl(version) { 110 const platform = getPlatform(); // 'darwin', 'linux', 'win32', 'win64' 111 logInfo(`platform=${platform}`) 112 const url = new URL(`${App}/${version}/${platform}/${App}-upgrade.zip`, downloadUrl) + '?ts=' + Date.now(); 113 logInfo(`appUrl=${url}`) 114 return url 115 } 116 117 export function getPlatform() { 118 let platform = os.platform(); // 'darwin', 'linux', 'win32' 119 120 if (platform === 'win32' && ['arm64', 'ppc64', 'x64', 's390x'].includes(os.arch())) { 121 platform = 'win64' 122 } 123 124 return platform 125 } 126 127 export async function checkMd5(version, file) { 128 const platform = getPlatform(); // 'darwin', 'linux', 'win32' 129 const url = new URL(`${App}/${version}/${platform}/${App}-upgrade.zip.md5`, downloadUrl) + '?ts=' + Date.now(); 130 131 logInfo(`md5Url=${url}, file=${file}`) 132 133 const md5Remote = (await got.get(url).text() + '').trim(); 134 const md5File = computerFileMd5(file) 135 const pass = md5Remote === md5File 136 137 logInfo(`md5Remote=${md5Remote}, md5File=${md5File}, pass=${pass}`) 138 139 return pass 140 }