본문 이미지 최적화 및 백필 보정

This commit is contained in:
2026-07-01 17:35:19 +09:00
parent 943794ae34
commit 78ab394f7a
18 changed files with 437 additions and 20 deletions

View File

@@ -1,11 +1,87 @@
import { listAdminPosts } from '../server/repositories/content-repository.js'
import { existsSync, readFileSync } from 'node:fs'
import postgres from 'postgres'
import {
createPostThumbnailForImageUrl,
getExistingPostThumbnailUrl,
getPostThumbnailUrl
} from '../server/utils/post-thumbnail-image.js'
const posts = await listAdminPosts()
/**
* 로컬 실행용 환경 파일을 process.env에 반영한다.
* @returns {void}
*/
const loadEnvironmentFile = () => {
const candidates = [
process.env.ENV_FILE,
'.env.development',
'.env'
].filter(Boolean)
const envFile = candidates.find((filePath) => existsSync(filePath))
if (!envFile) {
return
}
const lines = readFileSync(envFile, 'utf8').split(/\r?\n/)
for (const line of lines) {
const trimmedLine = line.trim()
if (!trimmedLine || trimmedLine.startsWith('#')) {
continue
}
const separatorIndex = trimmedLine.indexOf('=')
if (separatorIndex === -1) {
continue
}
const key = trimmedLine.slice(0, separatorIndex).trim()
const rawValue = trimmedLine.slice(separatorIndex + 1).trim()
if (!key || process.env[key] !== undefined) {
continue
}
process.env[key] = rawValue.replace(/^['"]|['"]$/g, '')
}
}
/**
* 게시물 대표 이미지 목록을 조회한다.
* @returns {Promise<Array<{ featuredImage: string | null }>>} 대표 이미지 목록
*/
const listPostFeaturedImages = async () => {
loadEnvironmentFile()
if (!process.env.DATABASE_URL) {
throw new Error('DATABASE_URL 환경 변수가 필요합니다.')
}
const sql = postgres(process.env.DATABASE_URL, {
max: 1,
idle_timeout: 5,
connect_timeout: 10
})
try {
const rows = await sql`
SELECT featured_image
FROM posts
WHERE featured_image IS NOT NULL
ORDER BY COALESCE(published_at, updated_at) DESC
`
return rows.map((row) => ({
featuredImage: row.featured_image
}))
} finally {
await sql.end({ timeout: 5 })
}
}
const posts = await listPostFeaturedImages()
const featuredImageUrls = [...new Set(posts
.map((post) => post.featuredImage)
.filter((url) => getPostThumbnailUrl(url)))]