File size: 1,124 Bytes
44fe180
 
 
955ce73
 
44fe180
955ce73
44fe180
 
 
 
 
c4b02b2
44fe180
 
 
 
c4b02b2
 
44fe180
 
 
 
 
 
 
c4b02b2
 
 
 
 
 
 
 
 
 
 
44fe180
c4b02b2
44fe180
 
 
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
import { promises as fs } from "node:fs"
import path from "node:path"

import { RenderRequest, RenderedScene } from "../../types.mts"
import { renderedDirFilePath } from "../../config.mts"

import { hashRequest } from "../requests/hashRequest.mts"

export async function saveRenderedSceneToCache(
  request: RenderRequest,
  scene: RenderedScene
): Promise<RenderedScene> {
  // console.log("saveRenderedSceneToCache")
  if (scene.status !== "completed") {
    throw new Error("sorry, it only makes sense to cache a *completed* scene, not a pending or failed one.")
  }

  //note: this hashing function ignores the commands associated to cache and stuff
  const hash = hashRequest(request)
  const id = scene.renderId

  const cacheFileName = `hash_${hash}_id_${id}.json`
  const cacheFilePath = path.join(renderedDirFilePath, cacheFileName)

  const renderedSceneJson = JSON.stringify(scene)

  /*
  console.log({
    request,
    hash,
    id,
    cacheFileName,
    cacheFilePath,
    scene
  })
  */

  await fs.writeFile(cacheFilePath, renderedSceneJson, "utf8")
  console.log(`saved result to cache`)

  return scene
}