✨ 增加镜像源下载
This commit is contained in:
parent
357cc473c7
commit
1f246e3966
@ -1,27 +1,31 @@
|
||||
import electron, { app, BrowserWindow, ipcMain, net, shell } from "electron";
|
||||
import {deleteVersionById, insertVersion, listVersion} from "../storage/version";
|
||||
import {
|
||||
deleteVersionById,
|
||||
insertVersion,
|
||||
listVersion
|
||||
} from "../storage/version";
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const zlib = require("zlib");
|
||||
const { download } = require("electron-dl");
|
||||
const AdmZip = require('adm-zip');
|
||||
const log = require('electron-log');
|
||||
const AdmZip = require("adm-zip");
|
||||
const log = require("electron-log");
|
||||
|
||||
const versionRelation = {
|
||||
"win32_x64": ["window", "amd64"],
|
||||
"win32_arm64": ["window", "arm64"],
|
||||
"win32_ia32": ["window", "386"],
|
||||
"darwin_arm64": ["darwin", "arm64"],
|
||||
"darwin_x64": ["darwin", "amd64"],
|
||||
"darwin_amd64": ["darwin", "amd64"],
|
||||
"linux_x64": ["linux", "amd64"],
|
||||
"linux_arm64": ["linux", "arm64"],
|
||||
}
|
||||
win32_x64: ["window", "amd64"],
|
||||
win32_arm64: ["window", "arm64"],
|
||||
win32_ia32: ["window", "386"],
|
||||
darwin_arm64: ["darwin", "arm64"],
|
||||
darwin_x64: ["darwin", "amd64"],
|
||||
darwin_amd64: ["darwin", "amd64"],
|
||||
linux_x64: ["linux", "amd64"],
|
||||
linux_arm64: ["linux", "arm64"]
|
||||
};
|
||||
const platform = process.platform;
|
||||
const arch = process.arch;
|
||||
let currArch = `${platform}_${arch}`
|
||||
const frpArch = versionRelation[currArch]
|
||||
let currArch = `${platform}_${arch}`;
|
||||
const frpArch = versionRelation[currArch];
|
||||
|
||||
const unTarGZ = (tarGzPath: string, targetPath: string) => {
|
||||
const tar = require("tar");
|
||||
@ -47,7 +51,9 @@ const unTarGZ = (tarGzPath: string, targetPath: string) => {
|
||||
|
||||
const unZip = (zipPath: string, targetPath: string) => {
|
||||
if (!fs.existsSync(path.join(targetPath, path.basename(zipPath, ".zip")))) {
|
||||
fs.mkdirSync(path.join(targetPath, path.basename(zipPath, ".zip")), {recursive: true});
|
||||
fs.mkdirSync(path.join(targetPath, path.basename(zipPath, ".zip")), {
|
||||
recursive: true
|
||||
});
|
||||
}
|
||||
log.debug(`开始解压zip:${zipPath} 目标目录:${targetPath}`);
|
||||
/**
|
||||
@ -65,12 +71,12 @@ const unZip = (zipPath: string, targetPath: string) => {
|
||||
// console.error('Error extracting file:', err);
|
||||
// });
|
||||
|
||||
const zip = new AdmZip(zipPath)
|
||||
const zip = new AdmZip(zipPath);
|
||||
zip.extractAllTo(targetPath, true); // 第二个参数为 true,表示覆盖已存在的文件
|
||||
const frpcPath = path.join("frp", path.basename(zipPath, ".zip"));
|
||||
log.debug(`解压完成 解压后目录:${frpcPath}`);
|
||||
return frpcPath
|
||||
}
|
||||
return frpcPath;
|
||||
};
|
||||
|
||||
export const initGitHubApi = () => {
|
||||
// 版本
|
||||
@ -82,23 +88,31 @@ export const initGitHubApi = () => {
|
||||
|
||||
const getAdaptiveAsset = versionId => {
|
||||
const { assets } = getVersion(versionId);
|
||||
const asset = assets.find(
|
||||
f => {
|
||||
const asset = assets.find(f => {
|
||||
// const a = versionRelation[currArch]
|
||||
const a = frpArch
|
||||
const a = frpArch;
|
||||
if (a) {
|
||||
const flag = a.every(item => f.name.includes(item))
|
||||
const flag = a.every(item => f.name.includes(item));
|
||||
return flag;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
);
|
||||
});
|
||||
if (asset) {
|
||||
log.info(`找到对应版本 ${asset.name}`)
|
||||
log.info(`找到对应版本 ${asset.name}`);
|
||||
}
|
||||
return asset;
|
||||
};
|
||||
|
||||
const formatBytes = (bytes: number, decimals: number = 2): string => {
|
||||
if (bytes === 0) return "0 Bytes";
|
||||
const k = 1024; // 1 KB = 1024 Bytes
|
||||
const dm = decimals < 0 ? 0 : decimals; // 确保小数位数不小于 0
|
||||
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
|
||||
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k)); // 计算单位索引
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i]; // 返回格式化的字符串
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取github上的frp所有版本
|
||||
*/
|
||||
@ -117,15 +131,23 @@ export const initGitHubApi = () => {
|
||||
// const borderContent: Electron.WebContents =
|
||||
// BrowserWindow.getFocusedWindow().webContents;
|
||||
const downloadPath = path.join(app.getPath("userData"), "download");
|
||||
log.info(`开始获取frp版本 当前架构:${currArch} 对应frp架构:${frpArch}`)
|
||||
log.info(
|
||||
`开始获取frp版本 当前架构:${currArch} 对应frp架构:${frpArch}`
|
||||
);
|
||||
const returnVersionsData = versions
|
||||
.filter(f => getAdaptiveAsset(f.id))
|
||||
.map(m => {
|
||||
const asset = getAdaptiveAsset(m.id);
|
||||
const download_count = m.assets.reduce(
|
||||
(sum, item) => sum + item.download_count,
|
||||
0
|
||||
);
|
||||
if (asset) {
|
||||
const absPath = `${downloadPath}/${asset.name}`;
|
||||
m.absPath = absPath;
|
||||
m.download_completed = fs.existsSync(absPath);
|
||||
m.download_count = download_count;
|
||||
m.size = formatBytes(asset.size);
|
||||
}
|
||||
return m;
|
||||
});
|
||||
@ -140,32 +162,43 @@ export const initGitHubApi = () => {
|
||||
* 下载请求
|
||||
*/
|
||||
ipcMain.on("github.download", async (event, args) => {
|
||||
const version = getVersion(args);
|
||||
const asset = getAdaptiveAsset(args);
|
||||
const { versionId, mirror } = args;
|
||||
const version = getVersion(versionId);
|
||||
const asset = getAdaptiveAsset(versionId);
|
||||
const { browser_download_url } = asset;
|
||||
log.info(`开始下载frp url:${browser_download_url} asset:${asset.name}`)
|
||||
|
||||
let url = browser_download_url;
|
||||
if (mirror === "ghproxy") {
|
||||
url = "https://mirror.ghproxy.com/" + url;
|
||||
}
|
||||
|
||||
log.info(`开始下载frp url:${url} asset:${asset.name}`);
|
||||
// 数据目录
|
||||
await download(BrowserWindow.getFocusedWindow(), browser_download_url, {
|
||||
await download(BrowserWindow.getFocusedWindow(), url, {
|
||||
filename: `${asset.name}`,
|
||||
directory: path.join(app.getPath("userData"), "download"),
|
||||
onProgress: progress => {
|
||||
event.reply("Download.frpVersionDownloadOnProgress", {
|
||||
id: args,
|
||||
id: versionId,
|
||||
progress: progress
|
||||
});
|
||||
},
|
||||
onCompleted: () => {
|
||||
log.info(`frp下载完成 url:${browser_download_url} asset:${asset.name}`)
|
||||
const targetPath = path.resolve(path.join(app.getPath("userData"), "frp"));
|
||||
const ext = path.extname(asset.name)
|
||||
let frpcVersionPath = ""
|
||||
if (ext === '.zip') {
|
||||
frpcVersionPath = unZip(path.join(
|
||||
log.info(`frp下载完成 url:${url} asset:${asset.name}`);
|
||||
const targetPath = path.resolve(
|
||||
path.join(app.getPath("userData"), "frp")
|
||||
);
|
||||
const ext = path.extname(asset.name);
|
||||
let frpcVersionPath = "";
|
||||
if (ext === ".zip") {
|
||||
frpcVersionPath = unZip(
|
||||
path.join(
|
||||
path.join(app.getPath("userData"), "download"),
|
||||
`${asset.name}`
|
||||
),
|
||||
targetPath)
|
||||
} else if (ext === '.gz' && asset.name.includes(".tar.gz")) {
|
||||
targetPath
|
||||
);
|
||||
} else if (ext === ".gz" && asset.name.includes(".tar.gz")) {
|
||||
frpcVersionPath = unTarGZ(
|
||||
path.join(
|
||||
path.join(app.getPath("userData"), "download"),
|
||||
@ -180,7 +213,7 @@ export const initGitHubApi = () => {
|
||||
if (!err) {
|
||||
listVersion((err, doc) => {
|
||||
event.reply("Config.versions.hook", { err, data: doc });
|
||||
event.reply("Download.frpVersionDownloadOnCompleted", args);
|
||||
event.reply("Download.frpVersionDownloadOnCompleted", versionId);
|
||||
version.download_completed = true;
|
||||
});
|
||||
}
|
||||
@ -196,14 +229,17 @@ export const initGitHubApi = () => {
|
||||
const { absPath, id } = args;
|
||||
if (fs.existsSync(absPath)) {
|
||||
deleteVersionById(id, () => {
|
||||
fs.unlinkSync(absPath)
|
||||
})
|
||||
fs.unlinkSync(absPath);
|
||||
});
|
||||
}
|
||||
listVersion((err, doc) => {
|
||||
event.reply("Config.versions.hook", { err, data: doc });
|
||||
event.reply("Download.deleteVersion.hook", { err: null, data: "删除成功" });
|
||||
event.reply("Download.deleteVersion.hook", {
|
||||
err: null,
|
||||
data: "删除成功"
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
|
||||
/**
|
||||
* 获取最后版本
|
||||
@ -240,5 +276,5 @@ export const initGitHubApi = () => {
|
||||
});
|
||||
});
|
||||
request.end();
|
||||
})
|
||||
});
|
||||
};
|
||||
|
@ -20,3 +20,20 @@
|
||||
|
||||
html {
|
||||
}
|
||||
|
||||
.h2 {
|
||||
color: #5a3daa;
|
||||
font-size: 16px;
|
||||
font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
|
||||
"Microsoft YaHei", "微软雅黑", Arial, sans-serif;
|
||||
font-weight: 700;
|
||||
padding: 6px 10px 6px 15px;
|
||||
border-left: 5px solid #5a3daa;
|
||||
border-radius: 4px;
|
||||
background-color: #eeebf6;
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.left-border {
|
||||
border-left: 5px solid #5a3daa;
|
||||
}
|
||||
|
@ -950,19 +950,6 @@ onUnmounted(() => {
|
||||
</template>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.h2 {
|
||||
color: #5a3daa;
|
||||
font-size: 16px;
|
||||
font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
|
||||
"Microsoft YaHei", "微软雅黑", Arial, sans-serif;
|
||||
font-weight: 700;
|
||||
padding: 6px 10px 6px 15px;
|
||||
border-left: 5px solid #5a3daa;
|
||||
border-radius: 4px;
|
||||
background-color: #eeebf6;
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.button-input {
|
||||
width: calc(100% - 68px);
|
||||
}
|
||||
|
7
types/global.d.ts
vendored
7
types/global.d.ts
vendored
@ -45,6 +45,8 @@ declare global {
|
||||
name: string;
|
||||
published_at: string;
|
||||
download_completed: boolean;
|
||||
size: string;
|
||||
download_count: number;
|
||||
absPath: string;
|
||||
assets: Asset[]
|
||||
};
|
||||
@ -75,6 +77,11 @@ declare global {
|
||||
transportHeartbeatTimeout: number;
|
||||
};
|
||||
|
||||
type GitHubMirror = {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
export {};
|
Loading…
Reference in New Issue
Block a user