Add secure Node image webhook service
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { basename, dirname, extname } from 'node:path/posix';
|
||||
import { rename, writeFile } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
|
||||
export function buildLegacyInventory(paths) {
|
||||
const inventory = {};
|
||||
for (const fullPath of paths) {
|
||||
if (!fullPath.startsWith('icons/')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const relative = fullPath.slice('icons/'.length);
|
||||
const directory = dirname(relative);
|
||||
const file = basename(relative);
|
||||
const extension = extname(file);
|
||||
const name = extension ? file.slice(0, -extension.length) : file;
|
||||
inventory[directory] ??= {};
|
||||
inventory[directory][name] = file;
|
||||
}
|
||||
return inventory;
|
||||
}
|
||||
|
||||
async function atomicJson(path, value) {
|
||||
const temporary = `${path}.tmp-${process.pid}`;
|
||||
await writeFile(temporary, JSON.stringify(value), { encoding: 'utf8', mode: 0o644 });
|
||||
await rename(temporary, path);
|
||||
}
|
||||
|
||||
export async function writeInventories({ repositoryPath, paths, branch, commit, publicBases, generatedAt = new Date() }) {
|
||||
const legacy = buildLegacyInventory(paths);
|
||||
const versioned = {
|
||||
version: 2,
|
||||
branch,
|
||||
commit,
|
||||
generatedAt: generatedAt.toISOString(),
|
||||
publicBases,
|
||||
assets: paths.filter((path) => path.startsWith('game/') || path.startsWith('icons/')),
|
||||
directories: legacy,
|
||||
};
|
||||
|
||||
await atomicJson(join(repositoryPath, 'hook', 'list.json'), legacy);
|
||||
await atomicJson(join(repositoryPath, 'hook', 'inventory.v2.json'), versioned);
|
||||
return versioned;
|
||||
}
|
||||
Reference in New Issue
Block a user