This commit is contained in:
2025-09-13 22:26:23 -05:00
parent 1fb68ba318
commit 659b3d5e8e

View File

@@ -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) => {
try {
folderName = (folderName || "").toLowerCase();
@@ -89,13 +40,13 @@ const getBucketFiles = async (folderName) => {
const itemCount = {};
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 keepTarKeys = new Set();
for (const t of tars) {
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]}`;
itemCount[groupId] = itemCount[groupId] || 0;
@@ -110,10 +61,8 @@ const getBucketFiles = async (folderName) => {
if (parts[3] === "windows") {
const setupKey =
t.key.substring(0, t.key.length - ext.length) + "_setup.exe";
if (
ret.find((x) => x.key === setupKey) &&
!oldItems.includes(setupKey)
) {
const hasSetup = ret.find((x) => x.key === setupKey);
if (hasSetup && !oldItems.includes(setupKey)) {
oldItems.push(setupKey, setupKey + ".sha256", setupKey + ".sig");
}
}
@@ -121,10 +70,8 @@ const getBucketFiles = async (folderName) => {
if (parts[3] === "darwin") {
const dmgKey =
t.key.substring(0, t.key.length - ext.length) + ".dmg";
if (
ret.find((x) => x.key === dmgKey) &&
!oldItems.includes(dmgKey)
) {
const hasDmg = ret.find((x) => x.key === dmgKey);
if (hasDmg && !oldItems.includes(dmgKey)) {
oldItems.push(dmgKey, dmgKey + ".sha256", dmgKey + ".sig");
}
}
@@ -134,85 +81,45 @@ const getBucketFiles = async (folderName) => {
}
}
// 2) fast lookup by key
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
const platformRank = (p) =>
p === "darwin" ? 0 : p === "windows" ? 1 : p === "linux" ? 2 : 3;
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();
// 3) build final list strictly in the order you want for each group:
// DMG(+sidecars) -> SETUP(+sidecars) -> TAR(+sidecars)
const out = [];
for (const t of tars) {
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))
builds.set(buildId, { maxSort: t.sort, items: [] });
const b = builds.get(buildId);
b.maxSort = Math.max(b.maxSort, t.sort);
b.items.push({ t, platform, arch });
}
const base = t.key.substring(0, t.key.length - ext.length);
const dmgKey = base + ".dmg";
const setupKey = base + "_setup.exe";
const buildList = Array.from(builds.entries()).sort(
(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];
if (dmg) {
out.push(dmg);
const dmgSha = byKey[dmgKey + ".sha256"];
if (dmgSha) out.push(dmgSha);
const dmgSig = byKey[dmgKey + ".sig"];
if (dmgSig) out.push(dmgSig);
}
}
// group order: 1) DMG group (if present)
const dmg = byKey[dmgKey];
if (dmg) {
out.push(dmg);
const dmgSha = byKey[dmgKey + ".sha256"];
if (dmgSha) out.push(dmgSha);
const dmgSig = byKey[dmgKey + ".sig"];
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;
@@ -221,5 +128,3 @@ const getBucketFiles = async (folderName) => {
return [];
}
};
export { cleanOldItems, createDownloadLink, getBucketFiles };