20 lines
923 B
TypeScript
20 lines
923 B
TypeScript
import { readFileSync, writeFileSync, renameSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { cachedLoras } from '~/shared/studio2/lora-cache.mjs'
|
|
import { listStudioLoras } from '../../utils/loras'
|
|
import { studio2Root } from '../../utils/studio2/store'
|
|
import { studio2LoraCatalog } from '../../utils/studio2/lora-catalog'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const { owner } = assertLibraryOwner(event)
|
|
const path = join(studio2Root(), `loras-${owner}.cache`)
|
|
const fresh = String(getQuery(event).fresh || '') === '1'
|
|
const listed = await cachedLoras({
|
|
read: () => { try { return JSON.parse(readFileSync(path, 'utf8')) } catch { return null } },
|
|
write: value => { writeFileSync(path + '.tmp', JSON.stringify(value)); renameSync(path + '.tmp', path) },
|
|
live: () => listStudioLoras({ fresh: true }),
|
|
fresh
|
|
})
|
|
return { ...listed, ...studio2LoraCatalog(owner) }
|
|
})
|