feat:文件列表与依赖列表
This commit is contained in:
110
src/controller/versionlist.controller.ts
Normal file
110
src/controller/versionlist.controller.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { Pool } from "pg";
|
||||
|
||||
export class VersionListController {
|
||||
versionCache: Version[];
|
||||
private pool: Pool;
|
||||
hashList: string[];
|
||||
constructor(pool: Pool) {
|
||||
this.pool = pool;
|
||||
this.versionCache = [];
|
||||
this.hashList = ["md5", "sha1", "sha256"];
|
||||
this._refreshCache(); // 初始化缓存
|
||||
setInterval(this._refreshCache, 1000 * 60 * 60 * 2); // 2小时刷新缓存
|
||||
}
|
||||
|
||||
async getVersionList() {
|
||||
if (this.versionCache.length === 0) {
|
||||
await this._refreshCache();
|
||||
}
|
||||
console.log("use cache");
|
||||
return this.versionCache;
|
||||
}
|
||||
|
||||
private async _refreshCache() {
|
||||
const arr: any[] = [];
|
||||
const result = (
|
||||
await this.pool.query(`
|
||||
SELECT mcversion."version",
|
||||
mcversion."Type",
|
||||
mcversion."Date",
|
||||
array_agg(DISTINCT variation.arch) as arch,
|
||||
variation."OSbuild"
|
||||
FROM variation
|
||||
LEFT JOIN mcversion ON mcversion.id = variation."MCVId"
|
||||
GROUP BY mcversion."version", mcversion."Type", mcversion."Date", variation."OSbuild"
|
||||
ORDER BY "Date" DESC
|
||||
`)
|
||||
).rows;
|
||||
result.forEach((row: VersionPool) => {
|
||||
const url: string[] = [];
|
||||
for (const arch of row.arch) {
|
||||
url.push(`./mc/version/${row.version}/${arch}`);
|
||||
arr.push({
|
||||
id: row.version,
|
||||
type: row.Type,
|
||||
BuildType: "UWP",
|
||||
Arch: ["x64", "x86"],
|
||||
url,
|
||||
time: row.Date,
|
||||
});
|
||||
}
|
||||
});
|
||||
this.versionCache = arr;
|
||||
}
|
||||
|
||||
async getLibraries(id: string, arch: string) {
|
||||
const archs = ["x64", "x86", "arm"];
|
||||
if (
|
||||
!archs.includes(arch) ||
|
||||
this.versionCache.filter((v) => v.id === id).length === 0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const r = (
|
||||
await this.pool.query(
|
||||
`
|
||||
SELECT variation.id
|
||||
FROM variation
|
||||
LEFT JOIN mcversion ON mcversion.id = variation."MCVId"
|
||||
WHERE mcversion."versionHash" = $1
|
||||
AND variation.arch = $2
|
||||
LIMIT 1
|
||||
`,
|
||||
[createHash("sha256").update(id).digest(), arch]
|
||||
)
|
||||
).rows; // 获取版本id
|
||||
|
||||
const data = (
|
||||
await this.pool.query(
|
||||
`
|
||||
SELECT "filePathName"."pathName",
|
||||
"filesHash"."size",
|
||||
${this.hashList.map(hash => `encode("filesHash"."${hash}", 'hex') as "${hash}"`).join(', ')}
|
||||
FROM variation_files_data
|
||||
LEFT JOIN files ON files.id = variation_files_data."filesId"
|
||||
LEFT JOIN "filePathName" ON "filePathName".id = files."pathNameId"
|
||||
LEFT JOIN "filesHash" ON "filesHash".id = files."hashId"
|
||||
WHERE variation_files_data."variationId" = $1
|
||||
`,
|
||||
[r[0].id]
|
||||
)
|
||||
).rows;
|
||||
console.log(data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
interface VersionPool {
|
||||
id: number;
|
||||
version: string;
|
||||
Type: string;
|
||||
Date: string;
|
||||
arch: string;
|
||||
}
|
||||
|
||||
interface Version {
|
||||
id: string;
|
||||
type: string;
|
||||
url: string;
|
||||
time: string;
|
||||
}
|
||||
Reference in New Issue
Block a user