중복 이미지 해시 재사용 추가

This commit is contained in:
2026-03-31 17:39:54 +09:00
parent d760c7331a
commit 4db1b21ad5
3 changed files with 118 additions and 7 deletions

View File

@@ -64,6 +64,20 @@ function mapGameItemRow(row) {
}
}
function mapImageAssetRow(row) {
if (!row) return null
return {
id: row.id,
contentHash: row.content_hash,
src: row.src || '',
mimeType: row.mime_type || 'image/webp',
byteSize: Number(row.byte_size || 0),
originalByteSize: Number(row.original_byte_size || 0),
width: Number(row.width || 0),
height: Number(row.height || 0),
createdAt: Number(row.created_at || 0),
}
}
function mapTierListRow(row) {
if (!row) return null
return {
@@ -270,6 +284,20 @@ async function ensureSchema() {
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
`)
await query(`
CREATE TABLE IF NOT EXISTS image_assets (
id VARCHAR(64) PRIMARY KEY,
content_hash CHAR(64) NOT NULL UNIQUE,
src VARCHAR(255) NOT NULL UNIQUE,
mime_type VARCHAR(32) NOT NULL DEFAULT 'image/webp',
byte_size INT UNSIGNED NOT NULL,
original_byte_size INT UNSIGNED NOT NULL,
width INT UNSIGNED NOT NULL DEFAULT 0,
height INT UNSIGNED NOT NULL DEFAULT 0,
created_at BIGINT NOT NULL,
INDEX idx_image_assets_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
`)
await query(`
CREATE TABLE IF NOT EXISTS template_requests (
id VARCHAR(64) PRIMARY KEY,
@@ -522,6 +550,22 @@ async function updateGameThumbnail(gameId, thumbnailSrc) {
return findGameById(gameId)
}
async function findImageAssetByHash(contentHash) {
const rows = await query(
'SELECT id, content_hash, src, mime_type, byte_size, original_byte_size, width, height, created_at FROM image_assets WHERE content_hash = ? LIMIT 1',
[contentHash]
)
return mapImageAssetRow(rows[0])
}
async function createImageAsset({ id, contentHash, src, mimeType = "image/webp", byteSize, originalByteSize, width, height }) {
const createdAt = now()
await query(
'INSERT INTO image_assets (id, content_hash, src, mime_type, byte_size, original_byte_size, width, height, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
[id, contentHash, src, mimeType, byteSize, originalByteSize, width, height, createdAt]
)
return findImageAssetByHash(contentHash)
}
async function createGameItem({ id, gameId, src, label }) {
const createdAt = now()
await query('INSERT INTO game_items (id, game_id, src, label, created_at) VALUES (?, ?, ?, ?, ?)', [
@@ -1404,6 +1448,8 @@ module.exports = {
getGameDetail,
createGame,
updateGameThumbnail,
findImageAssetByHash,
createImageAsset,
createGameItem,
updateGameItemLabel,
deleteGameItem,