feat: connect guarded asset import api
This commit is contained in:
1 parent
3497df9dfd
commit
eb759f54bf
26 files changed
+2342
-24
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { withAdmin } from '@/lib/api-handler'
|
||||
import { apiError } from '@/lib/api-response'
|
||||
import { PERMS } from '@/lib/permissions'
|
||||
import { logAudit } from '@/lib/services/audit'
|
||||
import { runSseBatch } from '@/lib/services/import/core/sse-batch'
|
||||
import { ensurePetDirs, importSinglePet } from '@/lib/services/pet-import'
|
||||
|
||||
interface PetBatchItem {
|
||||
lib: string
|
||||
}
|
||||
|
||||
export const POST = withAdmin({ permission: PERMS.ASSETS_IMPORT }, async (request, ctx) => {
|
||||
const body = await request.json()
|
||||
const rawItems: PetBatchItem[] = body.items || []
|
||||
const concurrency = body.concurrency || 3
|
||||
if (rawItems.length === 0) return apiError('No pets to import', 400)
|
||||
|
||||
const seen = new Set<string>()
|
||||
const items = rawItems.filter((it) => {
|
||||
if (seen.has(it.lib)) return false
|
||||
seen.add(it.lib)
|
||||
return true
|
||||
})
|
||||
|
||||
await ensurePetDirs()
|
||||
|
||||
return runSseBatch<PetBatchItem>({
|
||||
items,
|
||||
concurrency,
|
||||
labelOf: (it) => it.lib,
|
||||
worker: async (it, _index, report) => {
|
||||
const r = await importSinglePet({ lib: it.lib, onProgress: report })
|
||||
if (r.ok) {
|
||||
logAudit({
|
||||
userId: ctx.session.user.id,
|
||||
action: 'pet_import',
|
||||
target: 'PetAsset',
|
||||
targetId: 0,
|
||||
after: { lib: it.lib, batch: true },
|
||||
})
|
||||
}
|
||||
return { ok: r.ok, warnings: r.warnings, error: r.error }
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { withAdmin } from '@/lib/api-handler'
|
||||
import { PERMS } from '@/lib/permissions'
|
||||
import { getPetIconPng } from '@/lib/services/pet-icon'
|
||||
|
||||
// Serves a cropped pet preview PNG extracted from the pet's .nitro spritesheet.
|
||||
export const GET = withAdmin({ permission: PERMS.ASSETS_IMPORT }, async (request) => {
|
||||
const lib = request.nextUrl.searchParams.get('lib') || ''
|
||||
const png = getPetIconPng(lib)
|
||||
if (!png) return new Response(null, { status: 404 })
|
||||
return new Response(new Uint8Array(png), {
|
||||
headers: {
|
||||
'Content-Type': 'image/png',
|
||||
// short cache so generator tweaks show up quickly; the in-process icon
|
||||
// cache keeps regeneration cheap. Client bumps a ?v= when output changes.
|
||||
'Cache-Control': 'public, max-age=300',
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { withAdmin } from '@/lib/api-handler'
|
||||
import { apiError, apiOk } from '@/lib/api-response'
|
||||
import { PERMS } from '@/lib/permissions'
|
||||
import { logAudit } from '@/lib/services/audit'
|
||||
import { deleteImportedPet, getPetList, getPetStats } from '@/lib/services/pet-import'
|
||||
|
||||
export const GET = withAdmin({ permission: PERMS.ASSETS_IMPORT }, async (request) => {
|
||||
if (request.nextUrl.searchParams.get('action') === 'stats') return apiOk(getPetStats())
|
||||
const search = request.nextUrl.searchParams.get('search') || ''
|
||||
return apiOk({ pets: getPetList(search) })
|
||||
})
|
||||
|
||||
export const DELETE = withAdmin({ permission: PERMS.ASSETS_IMPORT }, async (request, ctx) => {
|
||||
const lib = request.nextUrl.searchParams.get('lib')
|
||||
if (!lib) return apiError('Missing lib', 400)
|
||||
const { deletedFile } = await deleteImportedPet(lib)
|
||||
if (!deletedFile) return apiError(`Pet "${lib}" not found`, 404)
|
||||
logAudit({
|
||||
userId: ctx.session.user.id,
|
||||
action: 'pet_delete',
|
||||
target: 'PetAsset',
|
||||
targetId: 0,
|
||||
after: { lib },
|
||||
})
|
||||
return apiOk({ lib, deletedFile })
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user