미사용 이미지 자산 정리 배치 추가
This commit is contained in:
@@ -30,6 +30,8 @@ const {
|
||||
adminUpdateUser,
|
||||
adminUpdateUserPassword,
|
||||
adminDeleteUser,
|
||||
listUnusedImageAssets,
|
||||
deleteImageAssets,
|
||||
} = require('../db')
|
||||
const { requireAdmin } = require('../middleware/auth')
|
||||
const { createMemoryUpload, writeOptimizedImage } = require('../lib/image-storage')
|
||||
@@ -206,6 +208,46 @@ router.get('/template-requests', requireAdmin, async (req, res) => {
|
||||
res.json({ requests })
|
||||
})
|
||||
|
||||
router.get('/image-assets/orphans', requireAdmin, async (req, res) => {
|
||||
const schema = z.object({
|
||||
limit: z.coerce.number().int().min(1).max(500).optional().default(100),
|
||||
minAgeHours: z.coerce.number().min(0).max(24 * 365).optional().default(24),
|
||||
})
|
||||
const parsed = schema.safeParse(req.query)
|
||||
if (!parsed.success) return res.status(400).json({ error: 'bad_request' })
|
||||
|
||||
const assets = await listUnusedImageAssets(parsed.data)
|
||||
res.json({ assets })
|
||||
})
|
||||
|
||||
async function removeImageAssetFiles(assets) {
|
||||
await Promise.all(
|
||||
(assets || []).map(async (asset) => {
|
||||
if (!asset?.src || !asset.src.startsWith('/uploads/')) return
|
||||
const absolutePath = path.join(__dirname, '..', '..', asset.src.replace(/^\//, ''))
|
||||
try {
|
||||
await fs.unlink(absolutePath)
|
||||
} catch (error) {
|
||||
if (error?.code !== 'ENOENT') throw error
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
router.post('/image-assets/cleanup', requireAdmin, async (req, res) => {
|
||||
const schema = z.object({
|
||||
limit: z.coerce.number().int().min(1).max(500).optional().default(100),
|
||||
minAgeHours: z.coerce.number().min(0).max(24 * 365).optional().default(24),
|
||||
})
|
||||
const parsed = schema.safeParse(req.body)
|
||||
if (!parsed.success) return res.status(400).json({ error: 'bad_request' })
|
||||
|
||||
const assets = await listUnusedImageAssets(parsed.data)
|
||||
const deleted = await deleteImageAssets(assets.map((asset) => asset.id))
|
||||
await removeImageAssetFiles(deleted)
|
||||
res.json({ deletedCount: deleted.length, assets: deleted })
|
||||
})
|
||||
|
||||
async function removeCustomItemFiles(items) {
|
||||
await Promise.all(
|
||||
items.map(async (item) => {
|
||||
@@ -221,33 +263,18 @@ async function removeCustomItemFiles(items) {
|
||||
}
|
||||
|
||||
async function promoteCustomItemToGameItem({ customItem, gameId }) {
|
||||
const originalName = path.basename(customItem.src || '')
|
||||
const nextFilename = buildUploadFilename({ originalname: originalName })
|
||||
const sourcePath = path.join(__dirname, '..', '..', customItem.src.replace(/^\//, ''))
|
||||
const targetRelativePath = path.join('uploads', 'games', nextFilename)
|
||||
const targetPath = path.join(__dirname, '..', '..', targetRelativePath)
|
||||
|
||||
await fs.copyFile(sourcePath, targetPath)
|
||||
|
||||
return createGameItem({
|
||||
id: nanoid(),
|
||||
gameId,
|
||||
src: `/${targetRelativePath.replace(/\\/g, '/')}`,
|
||||
src: customItem.src || '',
|
||||
label: customItem.label,
|
||||
})
|
||||
}
|
||||
|
||||
async function copyUploadIntoGameAsset(src) {
|
||||
if (typeof src !== 'string' || !src.startsWith('/uploads/')) return src || ''
|
||||
|
||||
const originalName = path.basename(src)
|
||||
const nextFilename = buildUploadFilename({ originalname: originalName })
|
||||
const sourcePath = path.join(__dirname, '..', '..', src.replace(/^\//, ''))
|
||||
const targetRelativePath = path.join('uploads', 'games', nextFilename)
|
||||
const targetPath = path.join(__dirname, '..', '..', targetRelativePath)
|
||||
|
||||
await fs.copyFile(sourcePath, targetPath)
|
||||
return `/${targetRelativePath.replace(/\\/g, '/')}`
|
||||
if (src.startsWith('/uploads/assets/')) return src
|
||||
return src
|
||||
}
|
||||
|
||||
function uniqueTierListPoolItems(tierList) {
|
||||
|
||||
Reference in New Issue
Block a user