|
"use server" |
|
|
|
import { RenderRequest, RenderedScene, RenderingEngine } from "@/types" |
|
|
|
const renderingEngine = `${process.env.RENDERING_ENGINE || ""}` as RenderingEngine |
|
|
|
|
|
|
|
const apiUrl = process.env.VIDEOCHAIN_API_URL |
|
|
|
export async function newRender({ |
|
prompt, |
|
// negativePrompt, |
|
width, |
|
height |
|
}: { |
|
prompt: string |
|
// negativePrompt: string[] |
|
width: number |
|
height: number |
|
}) { |
|
|
|
if (!prompt) { |
|
console.error(`cannot call the rendering API without a prompt, aborting..`) |
|
throw new Error(`cannot call the rendering API without a prompt, aborting..`) |
|
} |
|
|
|
let defaulResult: RenderedScene = { |
|
renderId: "", |
|
status: "error", |
|
assetUrl: "", |
|
alt: prompt || "", |
|
maskUrl: "", |
|
error: "failed to fetch the data", |
|
segments: [] |
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
const res = await fetch(`${apiUrl}/render`, { |
|
method: "POST", |
|
headers: { |
|
Accept: "application/json", |
|
"Content-Type": "application/json", |
|
Authorization: `Bearer ${process.env.VIDEOCHAIN_API_TOKEN}`, |
|
}, |
|
body: JSON.stringify({ |
|
prompt, |
|
|
|
nbFrames: 1, |
|
nbSteps: 25, |
|
actionnables: [], |
|
segmentation: "disabled", |
|
width, |
|
height, |
|
|
|
|
|
|
|
|
|
|
|
upscalingFactor: 1, |
|
|
|
|
|
analyze: false, |
|
|
|
cache: "ignore" |
|
} as Partial<RenderRequest>), |
|
cache: 'no-store', |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
if (res.status !== 200) { |
|
|
|
throw new Error('Failed to fetch data') |
|
} |
|
|
|
const response = (await res.json()) as RenderedScene |
|
|
|
return response |
|
} catch (err) { |
|
console.error(err) |
|
return defaulResult |
|
} |
|
} |
|
|
|
export async function getRender(renderId: string) { |
|
if (!renderId) { |
|
console.error(`cannot call the rendering API without a renderId, aborting..`) |
|
throw new Error(`cannot call the rendering API without a renderId, aborting..`) |
|
} |
|
|
|
let defaulResult: RenderedScene = { |
|
renderId: "", |
|
status: "pending", |
|
assetUrl: "", |
|
alt: "", |
|
maskUrl: "", |
|
error: "failed to fetch the data", |
|
segments: [] |
|
} |
|
|
|
try { |
|
|
|
const res = await fetch(`${apiUrl}/render/${renderId}`, { |
|
method: "GET", |
|
headers: { |
|
Accept: "application/json", |
|
"Content-Type": "application/json", |
|
Authorization: `Bearer ${process.env.VIDEOCHAIN_API_TOKEN}`, |
|
}, |
|
cache: 'no-store', |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
if (res.status !== 200) { |
|
|
|
throw new Error('Failed to fetch data') |
|
} |
|
|
|
const response = (await res.json()) as RenderedScene |
|
|
|
return response |
|
} catch (err) { |
|
console.error(err) |
|
defaulResult.status = "error" |
|
defaulResult.error = `${err}` |
|
|
|
return defaulResult |
|
} |
|
|
|
|
|
} |
|
|
|
export async function upscaleImage(image: string): Promise<{ |
|
assetUrl: string |
|
error: string |
|
}> { |
|
if (!image) { |
|
console.error(`cannot call the rendering API without an image, aborting..`) |
|
throw new Error(`cannot call the rendering API without an image, aborting..`) |
|
} |
|
|
|
let defaulResult = { |
|
assetUrl: "", |
|
error: "failed to fetch the data", |
|
} |
|
|
|
try { |
|
|
|
const res = await fetch(`${apiUrl}/upscale`, { |
|
method: "POST", |
|
headers: { |
|
Accept: "application/json", |
|
"Content-Type": "application/json", |
|
Authorization: `Bearer ${process.env.VIDEOCHAIN_API_TOKEN}`, |
|
}, |
|
cache: 'no-store', |
|
body: JSON.stringify({ image, factor: 3 }) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
if (res.status !== 200) { |
|
|
|
throw new Error('Failed to fetch data') |
|
} |
|
|
|
const response = (await res.json()) as { |
|
assetUrl: string |
|
error: string |
|
} |
|
|
|
return response |
|
} catch (err) { |
|
console.error(err) |
|
|
|
return defaulResult |
|
} |
|
|
|
|
|
} |
|
|