puteroapix / src /services /mediaStore.js
woiceatus's picture
init
e43a4a9
import { randomUUID } from "node:crypto";
export class InMemoryMediaStore {
constructor({ ttlSeconds = 3600 } = {}) {
this.ttlSeconds = ttlSeconds;
this.items = new Map();
}
save({ buffer, mimeType, extension }) {
this.cleanup();
const id = randomUUID();
const expiresAt = Date.now() + (this.ttlSeconds * 1000);
this.items.set(id, {
buffer,
mimeType,
extension,
expiresAt
});
return { id, expiresAt };
}
get(id) {
this.cleanup();
const item = this.items.get(id);
if (!item) {
return null;
}
return item;
}
cleanup() {
const now = Date.now();
for (const [id, item] of this.items.entries()) {
if (item.expiresAt <= now) {
this.items.delete(id);
}
}
}
}