File size: 814 Bytes
121e67d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const crypto = require('crypto')

const imageCache = new Map()

function computeHash(base64Data) {
  const base64Content = base64Data.includes('base64,') 
    ? base64Data.split('base64,')[1] 
    : base64Data
  
  return crypto.createHash('sha256')
    .update(base64Content)
    .digest('hex')
}

function hasImage(base64Data) {
  const hash = computeHash(base64Data)
  return imageCache.has(hash)
}

function getImageUrl(base64Data) {
  const hash = computeHash(base64Data)
  if (imageCache.has(hash)) {
    return imageCache.get(hash)
  }
  return null
}

function addImage(base64Data, imageUrl) {
  const hash = computeHash(base64Data)
  if (!imageCache.has(hash)) {
    imageCache.set(hash, imageUrl)
  }
}



module.exports = {
  hasImage,
  getImageUrl,
  addImage
}