feat: add image validation and SteamGridDB fallback to game stub creation
This commit is contained in:
+14
-1
@@ -2,6 +2,7 @@ import { Elysia, t } from "elysia"
|
||||
import { db } from "@/lib/db/index"
|
||||
import { games } from "@/lib/db/schema"
|
||||
import { eq } from "drizzle-orm"
|
||||
import { validateImageUrl, fetchSteamGridDBCover } from "@/lib/steam/sync"
|
||||
|
||||
interface SteamAppDetails {
|
||||
type?: string
|
||||
@@ -77,6 +78,18 @@ export const gameStubRoutes = new Elysia({ prefix: "/games" }).post(
|
||||
const capsuleImage =
|
||||
`https://cdn.akamai.steamstatic.com/steam/apps/${body.steamAppId}/library_600x900.jpg`
|
||||
|
||||
let finalCapsuleImage: string | null = capsuleImage
|
||||
const imageValid = await validateImageUrl(capsuleImage)
|
||||
|
||||
if (!imageValid) {
|
||||
const fallbackImage = await fetchSteamGridDBCover(title)
|
||||
if (fallbackImage) {
|
||||
finalCapsuleImage = fallbackImage
|
||||
} else {
|
||||
finalCapsuleImage = null
|
||||
}
|
||||
}
|
||||
|
||||
const [game] = await db
|
||||
.insert(games)
|
||||
.values({
|
||||
@@ -87,7 +100,7 @@ export const gameStubRoutes = new Elysia({ prefix: "/games" }).post(
|
||||
publisher,
|
||||
genres,
|
||||
headerImage,
|
||||
capsuleImage,
|
||||
capsuleImage: finalCapsuleImage,
|
||||
storeUrl: `https://store.steampowered.com/app/${body.steamAppId}`,
|
||||
systemRequirements: details?.pc_requirements
|
||||
? { minimum: details.pc_requirements.minimum || null, recommended: details.pc_requirements.recommended || null }
|
||||
|
||||
+2
-2
@@ -30,7 +30,7 @@ export function isSyncStale(lastSync: Date | null): boolean {
|
||||
return Date.now() - new Date(lastSync).getTime() > SEVEN_DAYS_MS
|
||||
}
|
||||
|
||||
async function validateImageUrl(url: string): Promise<boolean> {
|
||||
export async function validateImageUrl(url: string): Promise<boolean> {
|
||||
try {
|
||||
const res = await fetch(url, { method: "HEAD", signal: AbortSignal.timeout(5000) });
|
||||
return res.ok;
|
||||
@@ -39,7 +39,7 @@ async function validateImageUrl(url: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchSteamGridDBCover(gameTitle: string): Promise<string | null> {
|
||||
export async function fetchSteamGridDBCover(gameTitle: string): Promise<string | null> {
|
||||
try {
|
||||
const apiKey = process.env.STEAMGRIDDB_API_KEY;
|
||||
if (!apiKey) return null;
|
||||
|
||||
Reference in New Issue
Block a user