59 lines
1.1 KiB
JavaScript
59 lines
1.1 KiB
JavaScript
const { v4: uuidv4 } = require('uuid');
|
|
|
|
const shareTokens = new Map();
|
|
const SHARE_CLEANUP_INTERVAL_MS = 60 * 1000;
|
|
|
|
function createShareRecord(
|
|
entryId,
|
|
filePath,
|
|
ttlSeconds = 300,
|
|
oneTime = false,
|
|
owner = null,
|
|
options = {},
|
|
) {
|
|
const token = uuidv4();
|
|
const expiresAt = Date.now() + ttlSeconds * 1000;
|
|
|
|
const record = {
|
|
entryId,
|
|
filePath,
|
|
expiresAt,
|
|
oneTime,
|
|
owner,
|
|
variant: options.variant || "original",
|
|
originalPath: options.originalPath || filePath,
|
|
castPath: options.castPath || null,
|
|
};
|
|
|
|
shareTokens.set(token, record);
|
|
|
|
return {
|
|
token,
|
|
expiresAt,
|
|
};
|
|
}
|
|
|
|
function getShareRecord(token) {
|
|
const rec = shareTokens.get(token);
|
|
if (!rec) return null;
|
|
if (rec.expiresAt <= Date.now()) {
|
|
shareTokens.delete(token);
|
|
return null;
|
|
}
|
|
return rec;
|
|
}
|
|
|
|
function cleanupShareTokens() {
|
|
const now = Date.now();
|
|
for (const [t, rec] of shareTokens.entries()) {
|
|
if (rec.expiresAt <= now) shareTokens.delete(t);
|
|
}
|
|
}
|
|
|
|
setInterval(cleanupShareTokens, SHARE_CLEANUP_INTERVAL_MS);
|
|
|
|
module.exports = {
|
|
createShareRecord,
|
|
getShareRecord,
|
|
shareTokens,
|
|
};
|