🐛 版本过滤
This commit is contained in:
parent
eef54cfccc
commit
7c10ad1341
@ -1,121 +1,133 @@
|
|||||||
import { app, BrowserWindow, ipcMain, net } from "electron";
|
import {app, BrowserWindow, ipcMain, net} from "electron";
|
||||||
import { insertVersion } from "../storage/version";
|
import {insertVersion} from "../storage/version";
|
||||||
|
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const zlib = require("zlib");
|
const zlib = require("zlib");
|
||||||
const { download } = require("electron-dl");
|
const {download} = require("electron-dl");
|
||||||
|
|
||||||
|
|
||||||
|
const versionRelation = {
|
||||||
|
"win32_x64": ["window", "amd64"],
|
||||||
|
"win32_arm64": ["window", "arm64"],
|
||||||
|
"win32_ia32": ["window", "386"],
|
||||||
|
"darwin_arm64": ["darwin", "arm64"],
|
||||||
|
"darwin_amd64": ["darwin", "amd64"],
|
||||||
|
}
|
||||||
|
|
||||||
const unTarGZ = tarGzPath => {
|
const unTarGZ = tarGzPath => {
|
||||||
const tar = require("tar");
|
const tar = require("tar");
|
||||||
const targetPath = path.resolve(path.join(app.getPath("userData"), "frp"));
|
const targetPath = path.resolve(path.join(app.getPath("userData"), "frp"));
|
||||||
const unzip = zlib.createGunzip();
|
const unzip = zlib.createGunzip();
|
||||||
const readStream = fs.createReadStream(tarGzPath);
|
const readStream = fs.createReadStream(tarGzPath);
|
||||||
if (!fs.existsSync(unzip)) {
|
if (!fs.existsSync(unzip)) {
|
||||||
fs.mkdirSync(targetPath, { recursive: true });
|
fs.mkdirSync(targetPath, {recursive: true});
|
||||||
}
|
}
|
||||||
readStream.pipe(unzip).pipe(
|
readStream.pipe(unzip).pipe(
|
||||||
tar.extract({
|
tar.extract({
|
||||||
cwd: targetPath,
|
cwd: targetPath,
|
||||||
filter: filePath => path.basename(filePath) === "frpc"
|
filter: filePath => path.basename(filePath) === "frpc"
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return path.join("frp", path.basename(tarGzPath, ".tar.gz"));
|
return path.join("frp", path.basename(tarGzPath, ".tar.gz"));
|
||||||
// .on("finish", () => {
|
// .on("finish", () => {
|
||||||
// console.log("解压完成!");
|
// console.log("解压完成!");
|
||||||
// });
|
// });
|
||||||
};
|
};
|
||||||
export const initGitHubApi = () => {
|
export const initGitHubApi = () => {
|
||||||
// 版本
|
// 版本
|
||||||
let versions = [];
|
let versions = [];
|
||||||
|
|
||||||
const getVersion = versionId => {
|
const getVersion = versionId => {
|
||||||
return versions.find(f => f.id === versionId);
|
return versions.find(f => f.id === versionId);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAdaptiveAsset = versionId => {
|
const getAdaptiveAsset = versionId => {
|
||||||
const version = getVersion(versionId);
|
const {assets} = getVersion(versionId);
|
||||||
const arch = process.arch;
|
const arch = process.arch;
|
||||||
const platform = process.platform;
|
const platform = process.platform;
|
||||||
const { assets } = version;
|
const asset = assets.find(
|
||||||
const asset = assets.find(
|
f => {
|
||||||
f => f.name.indexOf(platform) != -1 && f.name.indexOf(arch) != -1
|
const a = versionRelation[`${platform}_${arch}`]
|
||||||
);
|
if (a) {
|
||||||
return asset;
|
const flag = a.every(item => f.name.includes(item))
|
||||||
};
|
return flag;
|
||||||
|
}
|
||||||
/**
|
return false;
|
||||||
* 获取github上的frp所有版本
|
|
||||||
*/
|
|
||||||
ipcMain.on("github.getFrpVersions", async event => {
|
|
||||||
const request = net.request({
|
|
||||||
method: "get",
|
|
||||||
url: "https://api.github.com/repos/fatedier/frp/releases"
|
|
||||||
});
|
|
||||||
request.on("response", response => {
|
|
||||||
let responseData: Buffer = Buffer.alloc(0);
|
|
||||||
response.on("data", (data: Buffer) => {
|
|
||||||
responseData = Buffer.concat([responseData, data]);
|
|
||||||
});
|
|
||||||
response.on("end", () => {
|
|
||||||
versions = JSON.parse(responseData.toString());
|
|
||||||
// const borderContent: Electron.WebContents =
|
|
||||||
// BrowserWindow.getFocusedWindow().webContents;
|
|
||||||
const downloadPath = path.join(app.getPath("userData"), "download");
|
|
||||||
const returnVersionsData = versions
|
|
||||||
.filter(f => getAdaptiveAsset(f.id))
|
|
||||||
.map(m => {
|
|
||||||
const asset = getAdaptiveAsset(m.id);
|
|
||||||
if (asset) {
|
|
||||||
// const absPath = `${downloadPath}/${asset.id}_${asset.name}`;
|
|
||||||
const absPath = `${downloadPath}/${asset.name}`;
|
|
||||||
m.download_completed = fs.existsSync(absPath);
|
|
||||||
} else {
|
|
||||||
console.log("buzhicih");
|
|
||||||
}
|
}
|
||||||
// m.download_completed = fs.existsSync(
|
|
||||||
// `${downloadPath}/${asset.id}_${asset.name}`
|
|
||||||
// );
|
|
||||||
return m;
|
|
||||||
});
|
|
||||||
event.reply("Download.frpVersionHook", returnVersionsData);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
request.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 下载请求
|
|
||||||
*/
|
|
||||||
ipcMain.on("github.download", async (event, args) => {
|
|
||||||
const version = getVersion(args);
|
|
||||||
const asset = getAdaptiveAsset(args);
|
|
||||||
const { browser_download_url } = asset;
|
|
||||||
// 数据目录
|
|
||||||
await download(BrowserWindow.getFocusedWindow(), browser_download_url, {
|
|
||||||
filename: `${asset.name}`,
|
|
||||||
directory: path.join(app.getPath("userData"), "download"),
|
|
||||||
onProgress: progress => {
|
|
||||||
event.reply("Download.frpVersionDownloadOnProgress", {
|
|
||||||
id: args,
|
|
||||||
progress: progress
|
|
||||||
});
|
|
||||||
},
|
|
||||||
onCompleted: () => {
|
|
||||||
const frpcVersionPath = unTarGZ(
|
|
||||||
path.join(
|
|
||||||
path.join(app.getPath("userData"), "download"),
|
|
||||||
`${asset.name}`
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
version["frpcVersionPath"] = frpcVersionPath;
|
if (asset) {
|
||||||
insertVersion(version, (err, document) => {
|
console.log(asset.name)
|
||||||
if (!err) {
|
}
|
||||||
event.reply("Download.frpVersionDownloadOnCompleted", args);
|
return asset;
|
||||||
version.download_completed = true;
|
};
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* 获取github上的frp所有版本
|
||||||
|
*/
|
||||||
|
ipcMain.on("github.getFrpVersions", async event => {
|
||||||
|
const request = net.request({
|
||||||
|
method: "get",
|
||||||
|
url: "https://api.github.com/repos/fatedier/frp/releases"
|
||||||
|
});
|
||||||
|
request.on("response", response => {
|
||||||
|
let responseData: Buffer = Buffer.alloc(0);
|
||||||
|
response.on("data", (data: Buffer) => {
|
||||||
|
responseData = Buffer.concat([responseData, data]);
|
||||||
|
});
|
||||||
|
response.on("end", () => {
|
||||||
|
versions = JSON.parse(responseData.toString());
|
||||||
|
// const borderContent: Electron.WebContents =
|
||||||
|
// BrowserWindow.getFocusedWindow().webContents;
|
||||||
|
const downloadPath = path.join(app.getPath("userData"), "download");
|
||||||
|
const returnVersionsData = versions
|
||||||
|
.filter(f => getAdaptiveAsset(f.id))
|
||||||
|
.map(m => {
|
||||||
|
const asset = getAdaptiveAsset(m.id);
|
||||||
|
if (asset) {
|
||||||
|
const absPath = `${downloadPath}/${asset.name}`;
|
||||||
|
m.download_completed = fs.existsSync(absPath);
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
});
|
||||||
|
event.reply("Download.frpVersionHook", returnVersionsData);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
request.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载请求
|
||||||
|
*/
|
||||||
|
ipcMain.on("github.download", async (event, args) => {
|
||||||
|
const version = getVersion(args);
|
||||||
|
const asset = getAdaptiveAsset(args);
|
||||||
|
const {browser_download_url} = asset;
|
||||||
|
// 数据目录
|
||||||
|
await download(BrowserWindow.getFocusedWindow(), browser_download_url, {
|
||||||
|
filename: `${asset.name}`,
|
||||||
|
directory: path.join(app.getPath("userData"), "download"),
|
||||||
|
onProgress: progress => {
|
||||||
|
event.reply("Download.frpVersionDownloadOnProgress", {
|
||||||
|
id: args,
|
||||||
|
progress: progress
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onCompleted: () => {
|
||||||
|
const frpcVersionPath = unTarGZ(
|
||||||
|
path.join(
|
||||||
|
path.join(app.getPath("userData"), "download"),
|
||||||
|
`${asset.name}`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
version["frpcVersionPath"] = frpcVersionPath;
|
||||||
|
insertVersion(version, (err, document) => {
|
||||||
|
if (!err) {
|
||||||
|
event.reply("Download.frpVersionDownloadOnCompleted", args);
|
||||||
|
version.download_completed = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
@ -129,6 +129,10 @@ ipcMain.handle("open-win", (_, arg) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ipcMain.on('open-url', (event, url) => {
|
||||||
|
shell.openExternal(url).then(r => {});
|
||||||
|
});
|
||||||
|
|
||||||
initGitHubApi();
|
initGitHubApi();
|
||||||
initConfigApi();
|
initConfigApi();
|
||||||
initProxyApi();
|
initProxyApi();
|
||||||
|
Loading…
Reference in New Issue
Block a user