mirror of
https://github.com/daydreamer-json/ak-endfield-api-archive.git
synced 2026-03-21 23:02:20 +01:00
feat: Add origStatus to MirrorFileEntry and implement status check for original files
- Added origStatus property to MirrorFileEntry interface to track the availability of original files. - Updated generateDownloadLinks function to conditionally display the original link based on origStatus. - Implemented checkMirrorFileDbStatus function to verify the accessibility of original files and update their status in the mirrorFileDb. - Called checkMirrorFileDbStatus in main command handler to ensure the status is checked during execution.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -22,6 +22,7 @@ const FILE_SIZE_OPTS = {
|
||||
interface MirrorFileEntry {
|
||||
orig: string;
|
||||
mirror: string;
|
||||
origStatus: boolean;
|
||||
}
|
||||
|
||||
interface StoredData<T> {
|
||||
@@ -681,7 +682,9 @@ function generateDownloadLinks(url: string) {
|
||||
const mirrorEntry = mirrorFileDb.find((g) => g.orig.includes(cleanUrl.toString()));
|
||||
|
||||
const links: string[] = [];
|
||||
links.push(`<a href="${url}" target="_blank">Orig</a>`);
|
||||
if (!mirrorEntry || mirrorEntry.origStatus === true) {
|
||||
links.push(`<a href="${url}" target="_blank">Orig</a>`);
|
||||
}
|
||||
if (mirrorEntry) {
|
||||
links.push(`<a href="${mirrorEntry.mirror}" target="_blank">Mirror</a>`);
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@ interface LauncherTarget {
|
||||
interface MirrorFileEntry {
|
||||
orig: string;
|
||||
mirror: string;
|
||||
origStatus: boolean;
|
||||
}
|
||||
|
||||
function getObjectDiff(
|
||||
@@ -171,12 +172,40 @@ async function saveToGHMirror(url: string, name: string | null): Promise<void> {
|
||||
mirrorFileDb.push({
|
||||
orig: stringUtils.removeQueryStr(url),
|
||||
mirror: `https://github.com/${githubAuthCfg.github.relArchive.owner}/${githubAuthCfg.github.relArchive.repo}/releases/download/${githubAuthCfg.github.relArchive.tag}/${name ?? new URL(url).pathname.split('/').pop() ?? ''}`,
|
||||
origStatus: true,
|
||||
});
|
||||
await Bun.write(localJsonPath, JSON.stringify(mirrorFileDb));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function checkMirrorFileDbStatus(): Promise<void> {
|
||||
logger.info('Checking the availability of orig for mirrored files ...');
|
||||
const localJsonPath = path.join(argvUtils.getArgv()['outputDir'], 'mirror_file_list.json');
|
||||
const mirrorFileDb = (await Bun.file(localJsonPath).json()) as MirrorFileEntry[];
|
||||
for (const fileEntry of mirrorFileDb) {
|
||||
const rsp = await (async () => {
|
||||
try {
|
||||
await ky.head(fileEntry.orig, {
|
||||
headers: { 'User-Agent': appConfig.network.userAgent.minimum },
|
||||
timeout: appConfig.network.timeout,
|
||||
retry: { limit: appConfig.network.retryCount },
|
||||
});
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
if (rsp === false && fileEntry.origStatus) {
|
||||
const url = new URL(fileEntry.orig);
|
||||
url.search = '';
|
||||
logger.trace('Orig is inaccessible: ' + url.pathname.split('/').pop());
|
||||
}
|
||||
fileEntry.origStatus = rsp;
|
||||
}
|
||||
await Bun.write(localJsonPath, JSON.stringify(mirrorFileDb));
|
||||
}
|
||||
|
||||
async function fetchAndSaveLatestGames(gameTargets: GameTarget[]) {
|
||||
logger.debug(`Fetching latestGame ...`);
|
||||
for (const target of gameTargets) {
|
||||
@@ -590,6 +619,8 @@ async function mainCmdHandler() {
|
||||
await fetchAndSaveAllGameResRawData(gameTargets);
|
||||
await fetchAndSaveLatestLauncher(launcherTargets);
|
||||
|
||||
await checkMirrorFileDbStatus();
|
||||
|
||||
// todo: Implement multithreading or separate the steps in GH Actions
|
||||
for (const e of saveToGHMirrorNeedUrls) {
|
||||
await saveToGHMirror(e.url, e.name);
|
||||
|
||||
Reference in New Issue
Block a user