update
This commit is contained in:
155
src/api.js
155
src/api.js
@@ -1,52 +1,3 @@
|
|||||||
import {
|
|
||||||
DeleteObjectCommand,
|
|
||||||
GetObjectCommand,
|
|
||||||
ListObjectsCommand,
|
|
||||||
S3Client,
|
|
||||||
} from "@aws-sdk/client-s3";
|
|
||||||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
||||||
|
|
||||||
const BUCKET = "repertory";
|
|
||||||
|
|
||||||
const oldItems = [];
|
|
||||||
|
|
||||||
const s3 = new S3Client({
|
|
||||||
region: "any",
|
|
||||||
endpoint: "https://gateway.storjshare.io",
|
|
||||||
forcePathStyle: true,
|
|
||||||
credentials: {
|
|
||||||
accessKeyId: process.env.R_AWS_KEY,
|
|
||||||
secretAccessKey: process.env.R_AWS_SECRET,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const cleanOldItems = async () => {
|
|
||||||
console.log(`cleaning|count|${oldItems.length}`);
|
|
||||||
while (oldItems.length > 0) {
|
|
||||||
try {
|
|
||||||
const key = oldItems.pop();
|
|
||||||
console.log(`cleaning|key|${key}`);
|
|
||||||
await s3.send(new DeleteObjectCommand({ Bucket: BUCKET, Key: key }));
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const createDownloadLink = async (key) => {
|
|
||||||
let filename = key.split("/");
|
|
||||||
filename = filename[filename.length - 1];
|
|
||||||
return await getSignedUrl(
|
|
||||||
s3,
|
|
||||||
new GetObjectCommand({
|
|
||||||
Bucket: BUCKET,
|
|
||||||
Key: key,
|
|
||||||
ResponseContentDisposition: `attachment; filename="${filename}"`,
|
|
||||||
}),
|
|
||||||
{ expiresIn: 3600 },
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const getBucketFiles = async (folderName) => {
|
const getBucketFiles = async (folderName) => {
|
||||||
try {
|
try {
|
||||||
folderName = (folderName || "").toLowerCase();
|
folderName = (folderName || "").toLowerCase();
|
||||||
@@ -89,13 +40,13 @@ const getBucketFiles = async (folderName) => {
|
|||||||
const itemCount = {};
|
const itemCount = {};
|
||||||
const ext = ".tar.gz";
|
const ext = ".tar.gz";
|
||||||
|
|
||||||
// choose base tars
|
// 1) choose which .tar.gz to keep (nightly retention unchanged)
|
||||||
const tars = ret.filter((it) => it.name.endsWith(ext));
|
const tars = ret.filter((it) => it.name.endsWith(ext));
|
||||||
const keepTarKeys = new Set();
|
const keepTarKeys = new Set();
|
||||||
|
|
||||||
for (const t of tars) {
|
for (const t of tars) {
|
||||||
if (folderName === "nightly") {
|
if (folderName === "nightly") {
|
||||||
const parts = t.name.split("_"); // 0=product,1=ver,2=build,3=platform,4=arch
|
const parts = t.name.split("_"); // 0=product,1=version,2=build,3=platform,4=arch
|
||||||
const groupId = `${parts[0]}_${parts[3]}_${parts[4]}`;
|
const groupId = `${parts[0]}_${parts[3]}_${parts[4]}`;
|
||||||
itemCount[groupId] = itemCount[groupId] || 0;
|
itemCount[groupId] = itemCount[groupId] || 0;
|
||||||
|
|
||||||
@@ -110,10 +61,8 @@ const getBucketFiles = async (folderName) => {
|
|||||||
if (parts[3] === "windows") {
|
if (parts[3] === "windows") {
|
||||||
const setupKey =
|
const setupKey =
|
||||||
t.key.substring(0, t.key.length - ext.length) + "_setup.exe";
|
t.key.substring(0, t.key.length - ext.length) + "_setup.exe";
|
||||||
if (
|
const hasSetup = ret.find((x) => x.key === setupKey);
|
||||||
ret.find((x) => x.key === setupKey) &&
|
if (hasSetup && !oldItems.includes(setupKey)) {
|
||||||
!oldItems.includes(setupKey)
|
|
||||||
) {
|
|
||||||
oldItems.push(setupKey, setupKey + ".sha256", setupKey + ".sig");
|
oldItems.push(setupKey, setupKey + ".sha256", setupKey + ".sig");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -121,10 +70,8 @@ const getBucketFiles = async (folderName) => {
|
|||||||
if (parts[3] === "darwin") {
|
if (parts[3] === "darwin") {
|
||||||
const dmgKey =
|
const dmgKey =
|
||||||
t.key.substring(0, t.key.length - ext.length) + ".dmg";
|
t.key.substring(0, t.key.length - ext.length) + ".dmg";
|
||||||
if (
|
const hasDmg = ret.find((x) => x.key === dmgKey);
|
||||||
ret.find((x) => x.key === dmgKey) &&
|
if (hasDmg && !oldItems.includes(dmgKey)) {
|
||||||
!oldItems.includes(dmgKey)
|
|
||||||
) {
|
|
||||||
oldItems.push(dmgKey, dmgKey + ".sha256", dmgKey + ".sig");
|
oldItems.push(dmgKey, dmgKey + ".sha256", dmgKey + ".sig");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -134,75 +81,20 @@ const getBucketFiles = async (folderName) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2) fast lookup by key
|
||||||
const byKey = Object.fromEntries(ret.map((r) => [r.key, r]));
|
const byKey = Object.fromEntries(ret.map((r) => [r.key, r]));
|
||||||
|
|
||||||
// ---- NEW: group kept tars by build, sort builds by newest, and sort within each build by platform/arch
|
// 3) build final list strictly in the order you want for each group:
|
||||||
const platformRank = (p) =>
|
// DMG(+sidecars) -> SETUP(+sidecars) -> TAR(+sidecars)
|
||||||
p === "darwin" ? 0 : p === "windows" ? 1 : p === "linux" ? 2 : 3;
|
const out = [];
|
||||||
const archRank = (a) =>
|
|
||||||
a === "aarch64" || a === "arm64"
|
|
||||||
? 0
|
|
||||||
: a === "x86-64" || a === "x86_64"
|
|
||||||
? 1
|
|
||||||
: 2;
|
|
||||||
|
|
||||||
// map buildId -> {maxSort, items:[tar]}
|
|
||||||
const builds = new Map();
|
|
||||||
for (const t of tars) {
|
for (const t of tars) {
|
||||||
if (!keepTarKeys.has(t.key)) continue;
|
if (!keepTarKeys.has(t.key)) continue;
|
||||||
const parts = t.name.split("_");
|
|
||||||
const buildId = parts[2] || "";
|
|
||||||
const platform = (parts[3] || "").toLowerCase();
|
|
||||||
const arch = parts[4] || "";
|
|
||||||
|
|
||||||
if (!builds.has(buildId))
|
const base = t.key.substring(0, t.key.length - ext.length);
|
||||||
builds.set(buildId, { maxSort: t.sort, items: [] });
|
const dmgKey = base + ".dmg";
|
||||||
const b = builds.get(buildId);
|
const setupKey = base + "_setup.exe";
|
||||||
b.maxSort = Math.max(b.maxSort, t.sort);
|
|
||||||
b.items.push({ t, platform, arch });
|
|
||||||
}
|
|
||||||
|
|
||||||
const buildList = Array.from(builds.entries()).sort(
|
// group order: 1) DMG group (if present)
|
||||||
(a, b) => b[1].maxSort - a[1].maxSort,
|
|
||||||
);
|
|
||||||
|
|
||||||
const out = [];
|
|
||||||
|
|
||||||
for (const [, bucket] of buildList) {
|
|
||||||
// stable within-build order: platform → arch, then by tar time desc
|
|
||||||
bucket.items.sort((a, b) => {
|
|
||||||
const pr = platformRank(a.platform) - platformRank(b.platform);
|
|
||||||
if (pr !== 0) return pr;
|
|
||||||
const ar = archRank(a.arch) - archRank(b.arch);
|
|
||||||
if (ar !== 0) return ar;
|
|
||||||
return b.t.sort - a.t.sort;
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const { t, platform } of bucket.items) {
|
|
||||||
// 1) tar + sidecars
|
|
||||||
out.push(t);
|
|
||||||
const tarSha = byKey[t.key + ".sha256"];
|
|
||||||
if (tarSha) out.push(tarSha);
|
|
||||||
const tarSig = byKey[t.key + ".sig"];
|
|
||||||
if (tarSig) out.push(tarSig);
|
|
||||||
|
|
||||||
// 2) windows setup + sidecars
|
|
||||||
if (platform === "windows") {
|
|
||||||
const setupKey =
|
|
||||||
t.key.substring(0, t.key.length - ext.length) + "_setup.exe";
|
|
||||||
const setup = byKey[setupKey];
|
|
||||||
if (setup) {
|
|
||||||
out.push(setup);
|
|
||||||
const setupSha = byKey[setupKey + ".sha256"];
|
|
||||||
if (setupSha) out.push(setupSha);
|
|
||||||
const setupSig = byKey[setupKey + ".sig"];
|
|
||||||
if (setupSig) out.push(setupSig);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3) darwin dmg + sidecars
|
|
||||||
if (platform === "darwin") {
|
|
||||||
const dmgKey = t.key.substring(0, t.key.length - ext.length) + ".dmg";
|
|
||||||
const dmg = byKey[dmgKey];
|
const dmg = byKey[dmgKey];
|
||||||
if (dmg) {
|
if (dmg) {
|
||||||
out.push(dmg);
|
out.push(dmg);
|
||||||
@@ -211,8 +103,23 @@ const getBucketFiles = async (folderName) => {
|
|||||||
const dmgSig = byKey[dmgKey + ".sig"];
|
const dmgSig = byKey[dmgKey + ".sig"];
|
||||||
if (dmgSig) out.push(dmgSig);
|
if (dmgSig) out.push(dmgSig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2) SETUP group (if present)
|
||||||
|
const setup = byKey[setupKey];
|
||||||
|
if (setup) {
|
||||||
|
out.push(setup);
|
||||||
|
const setupSha = byKey[setupKey + ".sha256"];
|
||||||
|
if (setupSha) out.push(setupSha);
|
||||||
|
const setupSig = byKey[setupKey + ".sig"];
|
||||||
|
if (setupSig) out.push(setupSig);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// 3) TAR group (always present for this loop)
|
||||||
|
out.push(t);
|
||||||
|
const tarSha = byKey[t.key + ".sha256"];
|
||||||
|
if (tarSha) out.push(tarSha);
|
||||||
|
const tarSig = byKey[t.key + ".sig"];
|
||||||
|
if (tarSig) out.push(tarSig);
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
@@ -221,5 +128,3 @@ const getBucketFiles = async (folderName) => {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { cleanOldItems, createDownloadLink, getBucketFiles };
|
|
||||||
|
Reference in New Issue
Block a user