Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

6.4 KB
Raw Blame
import {
  existsSync,
  createWriteStream,
  mkdirSync,
  statSync,
  unlinkSync,
  rmSync
} from 'graceful-fs'
import { createHash } from 'crypto'
import { readFile } from 'fs/promises'
import { extname, join } from 'path'
import { fileURLToPath } from 'url'
import axios from 'axios'
import { protocol } from 'electron'
import { appFolder } from './constants/paths'

const imagesCachePath = join(appFolder, 'images-cache')

const localImageMimeTypes: Record<string, string> = {
  '.avif': 'image/avif',
  '.gif': 'image/gif',
  '.jpeg': 'image/jpeg',
  '.jpg': 'image/jpeg',
  '.png': 'image/png',
  '.svg': 'image/svg+xml',
  '.webp': 'image/webp'
}

function detectImageMimeType(data: Buffer): string {
  if (data.length >= 3 && data[0] === 0xff && data[1] === 0xd8) {
    return 'image/jpeg'
  }
  if (
    data.length >= 8 &&
    data[0] === 0x89 &&
    data.subarray(1, 4).toString('ascii') === 'PNG'
  ) {
    return 'image/png'
  }
  if (data.length >= 6 && data.subarray(0, 4).toString('ascii') === 'GIF8') {
    return 'image/gif'
  }
  if (
    data.length >= 12 &&
    data.subarray(0, 4).toString('ascii') === 'RIFF' &&
    data.subarray(8, 12).toString('ascii') === 'WEBP'
  ) {
    return 'image/webp'
  }
  if (
    data.length >= 12 &&
    data.subarray(4, 8).toString('ascii') === 'ftyp' &&
    data.subarray(8, 12).toString('ascii').startsWith('avif')
  ) {
    return 'image/avif'
  }
  const start = data.subarray(0, 64).toString('utf8').trim()
  if (start.startsWith('<svg') || start.startsWith('<?xml')) {
    return 'image/svg+xml'
  }
  return 'application/octet-stream'
}

async function responseFromFile(filePath: string): Promise<Response> {
  try {
    const data = await readFile(filePath)
    return new Response(data, {
      status: 200,
      headers: {
        'Content-Type':
          localImageMimeTypes[extname(filePath).toLowerCase()] ??
          detectImageMimeType(data)
      }
    })
  } catch {
    return new Response(null, { status: 404 })
  }
}

const FAILED_MARKER_TTL_MS = 24 * 60 * 60 * 1000
const MAX_CONCURRENT_DOWNLOADS = 6
const DOWNLOAD_SPACING_MS = 150

export const initImagesCache = () => {
  // make sure we have a folder to store the cache
  if (!existsSync(imagesCachePath)) {
    mkdirSync(imagesCachePath)
  }

  // use a fake protocol for images we want to cache
  protocol.handle('imagecache', (request) => {
    return getImageFromCache(request.url)
  })
}

const pending = new Map<string, Promise<boolean>>()
let activeDownloads = 0
const waitQueue: Array<() => void> = []

async function acquireSlot(): Promise<void> {
  if (activeDownloads < MAX_CONCURRENT_DOWNLOADS) {
    activeDownloads++
    return
  }
  await new Promise<void>((res) => waitQueue.push(res))
  activeDownloads++
}

function releaseSlot(): void {
  activeDownloads--
  const next = waitQueue.shift()
  if (next) next()
}

function failedMarkerPath(digest: string): string {
  return `${join(imagesCachePath, digest)}.err`
}

function isRecentlyFailed(digest: string): boolean {
  const marker = failedMarkerPath(digest)
  if (!existsSync(marker)) return false
  try {
    const { mtimeMs } = statSync(marker)
    return Date.now() - mtimeMs < FAILED_MARKER_TTL_MS
  } catch {
    return false
  }
}

function markFailed(digest: string): void {
  try {
    createWriteStream(failedMarkerPath(digest)).end()
  } catch {
    // ignore — best-effort marker
  }
}

// Rate-limited, negatively-cached download
async function downloadImage(
  realUrl: string,
  digest: string
): Promise<boolean> {
  const marker = failedMarkerPath(digest)
  // Clean up a stale marker before a fresh attempt.
  if (existsSync(marker)) {
    try {
      unlinkSync(marker)
    } catch {
      // ignore
    }
  }
  const cachePath = join(imagesCachePath, digest)

  await acquireSlot()
  try {
    // small spacing to avoid bursts on the CDN
    await new Promise((r) => setTimeout(r, DOWNLOAD_SPACING_MS))

    const response = await axios({
      method: 'get',
      url: realUrl,
      responseType: 'stream',
      timeout: 15000
    })
    await new Promise<void>((resolve, reject) => {
      const stream = createWriteStream(cachePath)
      response.data.pipe(stream)
      stream.on('finish', () => resolve())
      stream.on('error', reject)
    })
    return true
  } catch {
    // Negative-cache this failure
    markFailed(digest)
    return false
  } finally {
    releaseSlot()
  }
}

const getImageFromCache = async (url: string): Promise<Response> => {
  const realUrl = decodeURIComponent(url.replace('imagecache://', ''))

  // Local images can't be loaded directly by the renderer while it's served
  // from the dev server (http origin), so proxy them through this protocol too.
  if (realUrl.startsWith('file://')) {
    try {
      return await responseFromFile(fileURLToPath(realUrl))
    } catch {
      return new Response(null, { status: 404 })
    }
  }

  // digest of the image url for the file name
  const digest = createHash('sha256').update(realUrl).digest('hex')
  const cachePath = join(imagesCachePath, digest)

  // Serve from cache
  if (existsSync(cachePath)) {
    return responseFromFile(cachePath)
  }

  // Not a downloadable URL
  if (!realUrl.startsWith('http')) {
    return new Response(null, { status: 404 })
  }

  // Skip recently failed downloads
  if (isRecentlyFailed(digest)) {
    return new Response(null, { status: 404 })
  }

  // Reuse in-flight download
  let pendingPromise = pending.get(digest)
  if (!pendingPromise) {
    pendingPromise = downloadImage(realUrl, digest).finally(() => {
      pending.delete(digest)
    })
    pending.set(digest, pendingPromise)
  }

  const ok = await pendingPromise
  if (ok && existsSync(cachePath)) {
    return responseFromFile(cachePath)
  }
  // 404 triggers frontend fallback
  return new Response(null, { status: 404 })
}

// Wipe all cached images
export function clearImagesCache(): void {
  if (!existsSync(imagesCachePath)) return
  try {
    rmSync(imagesCachePath, { recursive: true, force: true })
    mkdirSync(imagesCachePath)
  } catch {
    // ignore
  }
  pending.clear()
  waitQueue.length = 0
}

// Purge one cached image
export function removeImageFromCache(url: string): void {
  const digest = createHash('sha256').update(url).digest('hex')
  const cachePath = join(imagesCachePath, digest)
  if (existsSync(cachePath)) {
    try {
      unlinkSync(cachePath)
    } catch {
      // ignore
    }
  }
  const marker = `${cachePath}.err`
  if (existsSync(marker)) {
    try {
      unlinkSync(marker)
    } catch {
      // ignore
    }
  }
}