feat(member): 썸네일 교체/삭제 시 자산 자동 정리 추가
회원 아바타를 교체·삭제·탈퇴하는 흐름에서 이전 썸네일 파일과 메타데이터가 남지 않도록 공통 정리 로직을 연결했다. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
66
server/utils/member-avatar.js
Normal file
66
server/utils/member-avatar.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import { rm } from 'node:fs/promises'
|
||||
import { join, relative } from 'node:path'
|
||||
import { createError } from 'h3'
|
||||
import { getPostgresClient } from '../repositories/postgres-client'
|
||||
|
||||
const managedAvatarPrefix = '/uploads/members/avatars/'
|
||||
|
||||
/**
|
||||
* 회원 전용 썸네일 URL인지 확인한다.
|
||||
* @param {string} url - 대상 URL
|
||||
* @returns {boolean} 회원 썸네일 여부
|
||||
*/
|
||||
export const isManagedAvatarUrl = (url) => String(url || '').startsWith(managedAvatarPrefix)
|
||||
|
||||
/**
|
||||
* 회원 썸네일 URL을 실제 파일 경로로 변환한다.
|
||||
* @param {string} url - 썸네일 URL
|
||||
* @returns {string} 파일 시스템 경로
|
||||
*/
|
||||
export const resolveMemberAvatarPath = (url) => {
|
||||
if (!isManagedAvatarUrl(url)) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: '회원 썸네일 경로가 아닙니다.'
|
||||
})
|
||||
}
|
||||
|
||||
const decodedUrl = decodeURIComponent(url)
|
||||
const filePath = join(process.cwd(), 'public', decodedUrl)
|
||||
const uploadRoot = join(process.cwd(), 'public', 'uploads')
|
||||
const relativePath = relative(uploadRoot, filePath)
|
||||
|
||||
if (relativePath.startsWith('..') || relativePath === '') {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: '회원 썸네일 경로가 올바르지 않습니다.'
|
||||
})
|
||||
}
|
||||
|
||||
return filePath
|
||||
}
|
||||
|
||||
/**
|
||||
* 회원 썸네일 파일과 메타데이터를 정리한다.
|
||||
* @param {string} url - 정리 대상 URL
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export const removeManagedAvatarAsset = async (url) => {
|
||||
if (!isManagedAvatarUrl(url)) {
|
||||
return
|
||||
}
|
||||
|
||||
const filePath = resolveMemberAvatarPath(url)
|
||||
await rm(filePath, { force: true })
|
||||
|
||||
const sql = getPostgresClient()
|
||||
if (!sql) {
|
||||
return
|
||||
}
|
||||
|
||||
await sql`
|
||||
DELETE FROM media_metadata
|
||||
WHERE url = ${url}
|
||||
`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user