File size: 791 Bytes
e43a4a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
      }
    }
  }
}