Files
uniare.sori.studio/api/src/services/previewImage.js
zenn dbf02241e2 v4.0.0: 회원·덱 API, 인증 UI, 프로필 및 레이아웃 통일
Express/SQLite API와 Docker 구성을 추가하고 로그인·덱 저장·공개 덱·관리자·프로필 화면을 구현했다. 결과 화면 저장 UX, 헤더 프로필, 애드센스용 app-shell 레이아웃(1280px) 및 문서를 반영했다.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-22 16:59:36 +09:00

56 lines
1.5 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* 미리보기 이미지 저장 디렉터리
* @returns {string}
*/
export function getPreviewsDir() {
const dir = process.env.PREVIEWS_PATH || path.join(__dirname, '..', '..', 'data', 'previews');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
return dir;
}
/**
* base64 data URL을 PNG 파일로 저장
* @param {string} shareCode
* @param {string} dataUrl
* @returns {string} 파일명
*/
export function savePreviewImage(shareCode, dataUrl) {
if (!dataUrl || !dataUrl.startsWith('data:image')) {
return null;
}
const base64 = dataUrl.replace(/^data:image\/\w+;base64,/, '');
const fileName = `${shareCode}.png`;
fs.writeFileSync(path.join(getPreviewsDir(), fileName), Buffer.from(base64, 'base64'));
return fileName;
}
/**
* @param {string} shareCode
* @returns {string|null}
*/
export function getPreviewFilePath(shareCode) {
const filePath = path.join(getPreviewsDir(), `${shareCode}.png`);
return fs.existsSync(filePath) ? filePath : null;
}
/**
* @param {string} shareCode
* @param {string|null} [previewImageRecord] DB preview_image 컬럼
* @returns {string|null}
*/
export function getPreviewUrl(shareCode, previewImageRecord = null) {
const hasFile = Boolean(getPreviewFilePath(shareCode));
if (hasFile || previewImageRecord) {
return `/api/v1/decks/${shareCode}/preview`;
}
return null;
}