AI-BOOK / app-backup3.py
ginipick's picture
Rename app.py to app-backup3.py
d5ebba7 verified
raw
history blame
20 kB
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
import pathlib, os, uvicorn, base64
BASE = pathlib.Path(__file__).parent
app = FastAPI()
app.mount("/static", StaticFiles(directory=BASE), name="static")
# PDF ๋””๋ ‰ํ† ๋ฆฌ ์„ค์ •
PDF_DIR = BASE / "pdf"
if not PDF_DIR.exists():
PDF_DIR.mkdir(parents=True)
# PDF ํŒŒ์ผ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
def get_pdf_files():
pdf_files = []
if PDF_DIR.exists():
pdf_files = [f for f in PDF_DIR.glob("*.pdf")]
return pdf_files
# PDF ์ธ๋„ค์ผ ์ƒ์„ฑ ๋ฐ ํ”„๋กœ์ ํŠธ ๋ฐ์ดํ„ฐ ์ค€๋น„
def generate_pdf_projects():
projects_data = []
pdf_files = get_pdf_files()
for pdf_file in pdf_files:
projects_data.append({
"path": str(pdf_file),
"name": pdf_file.stem
})
return projects_data
HTML = """
<!doctype html><html lang="ko"><head>
<meta charset="utf-8"><title>FlipBook Space</title>
<link rel="stylesheet" href="/static/flipbook.css">
<script src="/static/three.js"></script>
<script src="/static/iscroll.js"></script>
<script src="/static/mark.js"></script>
<script src="/static/mod3d.js"></script>
<script src="/static/pdf.js"></script>
<script src="/static/flipbook.js"></script>
<script src="/static/flipbook.book3.js"></script>
<script src="/static/flipbook.scroll.js"></script>
<script src="/static/flipbook.swipe.js"></script>
<script src="/static/flipbook.webgl.js"></script>
<style>
body{margin:0;background:#f0f0f0;font-family:sans-serif}
header{max-width:960px;margin:0 auto;padding:18px 20px;display:flex;align-items:center}
#homeBtn{display:none;width:38px;height:38px;border:none;border-radius:50%;cursor:pointer;
background:#0077c2;color:#fff;font-size:20px;margin-right:12px}
#homeBtn:hover{background:#005999}
h2{margin:0;font-size:1.5rem;font-weight:600}
#home,#viewerPage{max-width:960px;margin:0 auto;padding:0 20px 40px}
.grid{display:grid;grid-template-columns:repeat(auto-fill,180px);gap:16px;margin-top:24px}
.card{
background:#fff url('/static/book2.jpg') no-repeat center center;
background-size: 169%; /* ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ํ˜„์žฌ๋ณด๋‹ค 30% ๋” ํ‚ค์›€ (130% * 1.3 = 169%) */
border:1px solid #ccc;
border-radius:6px;
cursor:pointer;
box-shadow:0 2px 4px rgba(0,0,0,.12);
width: 180px;
height: 240px;
position: relative;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.card img{
width:55%; /* ์ธ๋„ค์ผ ํฌ๊ธฐ ์ค„์ž„ (๋ฐฐ๊ฒฝ์ด ์ปค์ ธ์„œ ๋น„์œจ ์œ ์ง€) */
height:auto;
object-fit:contain;
position:absolute; /* ์ ˆ๋Œ€ ์œ„์น˜๋กœ ๋ณ€๊ฒฝ */
top:50%; /* ์ƒ๋‹จ์—์„œ 50% */
left:50%; /* ์ขŒ์ธก์—์„œ 50% */
transform: translate(-50%, -70%); /* ์ •์ค‘์•™์—์„œ 20% ์œ„๋กœ ์ด๋™ */
border: 1px solid #ddd;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.card p{
text-align:center;
margin:6px 0;
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
background: rgba(255, 255, 255, 0.7);
padding: 4px 8px;
border-radius: 4px;
width: 85%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 150px;
font-size: 11px; /* ๊ธฐ๋ณธ 16px์—์„œ ์•ฝ 30% ๊ฐ์†Œ */
}
button.upload{all:unset;cursor:pointer;border:1px solid #bbb;padding:8px 14px;border-radius:6px;background:#fff;margin:0 8px}
#viewer{
width:90%;
height:90vh;
max-width:90%;
margin:0;
background:#fff;
border:1px solid #ddd;
border-radius:8px;
position:fixed;
top:50%;
left:50%;
transform:translate(-50%, -50%);
z-index:1000;
box-shadow:0 4px 20px rgba(0,0,0,0.15);
/* ํ™”๋ฉด ๋น„์œจ์— ๋งž๊ฒŒ ์กฐ์ • */
max-height: calc(90vh - 40px); /* ์—ฌ๋ฐฑ ๊ณ ๋ ค */
aspect-ratio: auto; /* ๋น„์œจ ์ž๋™ ์กฐ์ • */
object-fit: contain; /* ๋‚ด์šฉ๋ฌผ ๋น„์œจ ์œ ์ง€ */
overflow: hidden; /* ๋‚ด์šฉ ๋„˜์นจ ๋ฐฉ์ง€ */
}
/* FlipBook ์ปจํŠธ๋กค๋ฐ” ๊ด€๋ จ ์Šคํƒ€์ผ ์˜ค๋ฒ„๋ผ์ด๋“œ */
.flipbook-container .fb3d-menu-bar {
z-index: 2000 !important; /* ์ปจํŠธ๋กค๋ฐ”๊ฐ€ ๋‹ค๋ฅธ ์š”์†Œ๋ณด๋‹ค ์œ„์— ์˜ค๋„๋ก ํ•จ */
opacity: 1 !important; /* ํ•ญ์ƒ ํ‘œ์‹œ๋˜๋„๋ก ํ•จ */
bottom: 0 !important; /* ํ•˜๋‹จ์— ๊ณ ์ • */
background-color: rgba(0,0,0,0.7) !important; /* ๋ฐฐ๊ฒฝ์ƒ‰ ์„ค์ • */
border-radius: 0 0 8px 8px !important; /* ํ•˜๋‹จ ๋ชจ์„œ๋ฆฌ๋งŒ ๋‘ฅ๊ธ€๊ฒŒ */
padding: 8px 0 !important; /* ์ƒํ•˜ ํŒจ๋”ฉ ์ถ”๊ฐ€ */
}
.flipbook-container .fb3d-menu-bar > ul > li > img,
.flipbook-container .fb3d-menu-bar > ul > li > div {
opacity: 1 !important; /* ๋ฉ”๋‰ด ์•„์ด์ฝ˜ ํ•ญ์ƒ ํ‘œ์‹œ */
transform: scale(1.2) !important; /* ์•„์ด์ฝ˜ ํฌ๊ธฐ ์•ฝ๊ฐ„ ํ‚ค์›€ */
}
.flipbook-container .fb3d-menu-bar > ul > li {
margin: 0 8px !important; /* ๋ฉ”๋‰ด ์•„์ดํ…œ ๊ฐ„๊ฒฉ ์กฐ์ • */
}
/* ๋ฉ”๋‰ด ํˆดํŒ ์Šคํƒ€์ผ ๊ฐœ์„  */
.flipbook-container .fb3d-menu-bar > ul > li > span {
background-color: rgba(0,0,0,0.8) !important;
color: white !important;
border-radius: 4px !important;
padding: 4px 8px !important;
font-size: 12px !important;
bottom: 45px !important; /* ํˆดํŒ ์œ„์น˜ ์กฐ์ • */
}
</style></head><body>
<header>
<button id="homeBtn" title="ํ™ˆ์œผ๋กœ">๐Ÿ </button>
<h2>My FlipBook Projects</h2>
</header>
<section id="home">
<div>
<label class="upload">๐Ÿ“ท ์ด๋ฏธ์ง€ <input id="imgInput" type="file" accept="image/*" multiple hidden></label>
<label class="upload">๐Ÿ“„ PDF <input id="pdfInput" type="file" accept="application/pdf" hidden></label>
</div>
<div class="grid" id="grid"></div>
</section>
<section id="viewerPage" style="display:none">
<div id="viewer"></div>
</section>
<script>
let projects=[], fb=null;
const grid=$id('grid'), viewer=$id('viewer');
pdfjsLib.GlobalWorkerOptions.workerSrc='/static/pdf.worker.js';
// ์„œ๋ฒ„์—์„œ ๋ฏธ๋ฆฌ ๋กœ๋“œ๋œ PDF ํ”„๋กœ์ ํŠธ
let serverProjects = [];
/* ๐Ÿ”Š ์˜ค๋””์˜ค unlock โ€“ ๋‚ด์žฅ Audio ์™€ ๊ฐ™์€ MP3 ๊ฒฝ๋กœ ์‚ฌ์šฉ */
['click','touchstart'].forEach(evt=>{
document.addEventListener(evt,function u(){new Audio('static/turnPage2.mp3')
.play().then(a=>a.pause()).catch(()=>{});document.removeEventListener(evt,u,{capture:true});},
{once:true,capture:true});
});
/* โ”€โ”€ ์œ ํ‹ธ โ”€โ”€ */
function $id(id){return document.getElementById(id)}
function addCard(i,thumb,title){
const d=document.createElement('div');
d.className='card';
d.onclick=()=>open(i);
// ์ œ๋ชฉ 10๊ธ€์ž ์ œํ•œ ๋ฐ ๋ง์ค„์ž„ํ‘œ ์ฒ˜๋ฆฌ
const displayTitle = title ?
(title.length > 10 ? title.substring(0, 10) + '...' : title) :
'ํ”„๋กœ์ ํŠธ ' + (i+1);
d.innerHTML=`<img src="${thumb}"><p title="${title || 'ํ”„๋กœ์ ํŠธ ' + (i+1)}">${displayTitle}</p>`;
grid.appendChild(d);
}
/* โ”€โ”€ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ โ”€โ”€ */
$id('imgInput').onchange=e=>{
const files=[...e.target.files]; if(!files.length) return;
const pages=[],tot=files.length;let done=0;
files.forEach((f,i)=>{const r=new FileReader();r.onload=x=>{pages[i]={src:x.target.result,thumb:x.target.result};
if(++done===tot) save(pages);};r.readAsDataURL(f);});
};
/* โ”€โ”€ PDF ์—…๋กœ๋“œ โ”€โ”€ */
$id('pdfInput').onchange=e=>{
const file=e.target.files[0]; if(!file) return;
const fr=new FileReader();
fr.onload=v=>{
pdfjsLib.getDocument({data:v.target.result}).promise.then(async pdf=>{
const pages=[];
for(let p=1;p<=pdf.numPages;p++){
const pg=await pdf.getPage(p), vp=pg.getViewport({scale:1});
const c=document.createElement('canvas');c.width=vp.width;c.height=vp.height;
await pg.render({canvasContext:c.getContext('2d'),viewport:vp}).promise;
pages.push({src:c.toDataURL(),thumb:c.toDataURL()});
}
save(pages, file.name.replace('.pdf', ''));
});
};fr.readAsArrayBuffer(file);
};
/* โ”€โ”€ ํ”„๋กœ์ ํŠธ ์ €์žฅ โ”€โ”€ */
function save(pages, title){
const id=projects.push(pages)-1;
addCard(id,pages[0].thumb, title);
}
/* โ”€โ”€ ์„œ๋ฒ„ PDF ๋กœ๋“œ โ”€โ”€ */
async function loadServerPDFs() {
try {
const response = await fetch('/api/pdf-projects');
serverProjects = await response.json();
// ์„œ๋ฒ„ PDF ๋กœ๋“œ ๋ฐ ์ธ๋„ค์ผ ์ƒ์„ฑ
for(let i = 0; i < serverProjects.length; i++) {
const project = serverProjects[i];
const response = await fetch(`/api/pdf-thumbnail?path=${encodeURIComponent(project.path)}`);
const data = await response.json();
if(data.thumbnail) {
const pages = [{
src: data.thumbnail,
thumb: data.thumbnail,
path: project.path
}];
save(pages, project.name);
}
}
} catch(error) {
console.error('์„œ๋ฒ„ PDF ๋กœ๋“œ ์‹คํŒจ:', error);
}
}
/* โ”€โ”€ ์นด๋“œ โ†’ FlipBook โ”€โ”€ */
function open(i){
toggle(false);
const pages = projects[i];
// ๋กœ์ปฌ ํ”„๋กœ์ ํŠธ ๋˜๋Š” ์„œ๋ฒ„ PDF ๋กœ๋“œ
if(fb){fb.destroy();viewer.innerHTML='';}
if(pages[0].path) {
// ๋กœ๋”ฉ ํ‘œ์‹œ
viewer.innerHTML = '<div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);text-align:center;"><div style="border:4px solid #f3f3f3;border-top:4px solid #3498db;border-radius:50%;width:50px;height:50px;margin:0 auto;animation:spin 2s linear infinite;"></div><p style="margin-top:20px;font-size:16px;">PDF ๋กœ๋”ฉ ์ค‘...</p></div>';
// ์Šคํƒ€์ผ ์ถ”๊ฐ€
if (!document.getElementById('loadingStyle')) {
const style = document.createElement('style');
style.id = 'loadingStyle';
style.textContent = '@keyframes spin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}';
document.head.appendChild(style);
}
// ์„œ๋ฒ„ PDF ํŒŒ์ผ ๋กœ๋“œ
fetch(`/api/pdf-content?path=${encodeURIComponent(pages[0].path)}`)
.then(response => {
if (!response.ok) {
throw new Error('PDF ๋กœ๋“œ ์‹คํŒจ: ' + response.statusText);
}
return response.arrayBuffer();
})
.then(pdfData => {
// PDF ๋ฐ์ดํ„ฐ ๋กœ๋“œ ํ™•์ธ ๋กœ๊น…
console.log('PDF ๋ฐ์ดํ„ฐ ๋กœ๋“œ ์™„๋ฃŒ:', pdfData.byteLength + ' ๋ฐ”์ดํŠธ');
return pdfjsLib.getDocument({data: pdfData}).promise;
})
.then(async pdf => {
console.log('PDF ๋ฌธ์„œ ๋กœ๋“œ ์™„๋ฃŒ. ํŽ˜์ด์ง€ ์ˆ˜:', pdf.numPages);
const pdfPages = [];
const progressElement = viewer.querySelector('p');
for(let p = 1; p <= pdf.numPages; p++) {
if (progressElement) {
progressElement.textContent = `PDF ํŽ˜์ด์ง€ ๋กœ๋”ฉ ์ค‘... (${p}/${pdf.numPages})`;
}
try {
const pg = await pdf.getPage(p);
const vp = pg.getViewport({scale: 1});
const c = document.createElement('canvas');
c.width = vp.width;
c.height = vp.height;
await pg.render({canvasContext: c.getContext('2d'), viewport: vp}).promise;
pdfPages.push({src: c.toDataURL(), thumb: c.toDataURL()});
} catch (pageError) {
console.error(`ํŽ˜์ด์ง€ ${p} ๋ Œ๋”๋ง ์˜ค๋ฅ˜:`, pageError);
}
}
console.log('๋ชจ๋“  ํŽ˜์ด์ง€ ๋ Œ๋”๋ง ์™„๋ฃŒ:', pdfPages.length);
if (pdfPages.length > 0) {
createFlipBook(pdfPages);
} else {
throw new Error('PDF์—์„œ ํŽ˜์ด์ง€๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.');
}
})
.catch(error => {
console.error('PDF ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:', error);
viewer.innerHTML = `<div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);text-align:center;"><p style="color:red;font-size:16px;">PDF๋ฅผ ๋กœ๋“œํ•˜๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค:<br>${error.message}</p><button id="backBtn" style="margin-top:20px;padding:10px 20px;background:#0077c2;color:white;border:none;border-radius:4px;cursor:pointer;">ํ™ˆ์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ</button></div>`;
document.getElementById('backBtn').addEventListener('click', function() {
toggle(true);
});
});
} else {
// ์—…๋กœ๋“œ๋œ ํ”„๋กœ์ ํŠธ ๋ณด๊ธฐ
console.log('๋กœ์ปฌ ์—…๋กœ๋“œ๋œ ํ”„๋กœ์ ํŠธ ๋ Œ๋”๋ง:', pages.length + 'ํŽ˜์ด์ง€');
createFlipBook(pages);
}
}
function createFlipBook(pages) {
console.log('FlipBook ์ƒ์„ฑ ์‹œ์ž‘. ํŽ˜์ด์ง€ ์ˆ˜:', pages.length);
try {
// ํ™”๋ฉด ๋น„์œจ ๊ณ„์‚ฐ
const calculateAspectRatio = () => {
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
const aspectRatio = windowWidth / windowHeight;
// ๋„ˆ๋น„ ๋˜๋Š” ๋†’์ด ๊ธฐ์ค€์œผ๋กœ ์ตœ๋Œ€ 90% ์ œํ•œ
let width, height;
if (aspectRatio > 1) { // ๊ฐ€๋กœ ํ™”๋ฉด
height = Math.min(windowHeight * 0.9, windowHeight - 40);
width = height * aspectRatio * 0.8; // ๊ฐ€๋กœ ํ™”๋ฉด์—์„œ๋Š” ์•ฝ๊ฐ„ ์ค„์ž„
if (width > windowWidth * 0.9) {
width = windowWidth * 0.9;
height = width / (aspectRatio * 0.8);
}
} else { // ์„ธ๋กœ ํ™”๋ฉด
width = Math.min(windowWidth * 0.9, windowWidth - 40);
height = width / aspectRatio * 0.9; // ์„ธ๋กœ ํ™”๋ฉด์—์„œ๋Š” ์•ฝ๊ฐ„ ๋Š˜๋ฆผ
if (height > windowHeight * 0.9) {
height = windowHeight * 0.9;
width = height * aspectRatio * 0.9;
}
}
// ์ตœ์  ์‚ฌ์ด์ฆˆ ๋ฐ˜ํ™˜
return {
width: Math.round(width),
height: Math.round(height)
};
};
// ์ดˆ๊ธฐ ํ™”๋ฉด ๋น„์œจ ๊ณ„์‚ฐ
const size = calculateAspectRatio();
viewer.style.width = size.width + 'px';
viewer.style.height = size.height + 'px';
fb = new FlipBook(viewer, {
pages: pages,
viewMode: 'webgl',
autoSize: true,
flipDuration: 800,
backgroundColor: '#fff',
/* ๐Ÿ”Š ๋‚ด์žฅ ์‚ฌ์šด๋“œ */
sound: true,
assets: {flipMp3: 'static/turnPage2.mp3', hardFlipMp3: 'static/turnPage2.mp3'},
controlsProps: {
enableFullscreen: true,
enableToc: true,
enableDownload: false,
enablePrint: false,
enableZoom: true,
enableShare: false,
enableSearch: true,
enableAutoPlay: true,
enableAnnotation: false,
enableSound: true,
enableLightbox: false,
layout: 10, // ๋ ˆ์ด์•„์›ƒ ์˜ต์…˜
skin: 'light', // ์Šคํ‚จ ์Šคํƒ€์ผ
autoNavigationTime: 3600, // ์ž๋™ ๋„˜๊น€ ์‹œ๊ฐ„(์ดˆ)
hideControls: false, // ์ปจํŠธ๋กค ์ˆจ๊น€ ๋น„ํ™œ์„ฑํ™”
paddingTop: 10, // ์ƒ๋‹จ ํŒจ๋”ฉ
paddingLeft: 10, // ์ขŒ์ธก ํŒจ๋”ฉ
paddingRight: 10, // ์šฐ์ธก ํŒจ๋”ฉ
paddingBottom: 10, // ํ•˜๋‹จ ํŒจ๋”ฉ
pageTextureSize: 1024, // ํŽ˜์ด์ง€ ํ…์Šค์ฒ˜ ํฌ๊ธฐ
thumbnails: true, // ์„ฌ๋„ค์ผ ํ™œ์„ฑํ™”
autoHideControls: false, // ์ž๋™ ์ˆจ๊น€ ๋น„ํ™œ์„ฑํ™”
controlsTimeout: 8000 // ์ปจํŠธ๋กค ํ‘œ์‹œ ์‹œ๊ฐ„ ์—ฐ์žฅ
}
});
// ํ™”๋ฉด ํฌ๊ธฐ ๋ณ€๊ฒฝ ์‹œ FlipBook ํฌ๊ธฐ ์กฐ์ •
window.addEventListener('resize', () => {
if (fb) {
const newSize = calculateAspectRatio();
viewer.style.width = newSize.width + 'px';
viewer.style.height = newSize.height + 'px';
fb.resize();
}
});
// FlipBook ์ƒ์„ฑ ํ›„ ์ปจํŠธ๋กค๋ฐ” ๊ฐ•์ œ ํ‘œ์‹œ
setTimeout(() => {
try {
// ์ปจํŠธ๋กค๋ฐ” ๊ด€๋ จ ์š”์†Œ ์ฐพ๊ธฐ ๋ฐ ์Šคํƒ€์ผ ์ ์šฉ
const menuBars = document.querySelectorAll('.flipbook-container .fb3d-menu-bar');
if (menuBars && menuBars.length > 0) {
menuBars.forEach(menuBar => {
menuBar.style.display = 'block';
menuBar.style.opacity = '1';
menuBar.style.visibility = 'visible';
menuBar.style.zIndex = '9999';
});
}
} catch (e) {
console.warn('์ปจํŠธ๋กค๋ฐ” ์Šคํƒ€์ผ ์ ์šฉ ์ค‘ ์˜ค๋ฅ˜:', e);
}
}, 1000);
console.log('FlipBook ์ƒ์„ฑ ์™„๋ฃŒ');
} catch (error) {
console.error('FlipBook ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:', error);
alert('FlipBook์„ ์ƒ์„ฑํ•˜๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: ' + error.message);
}
}
/* โ”€โ”€ ๋„ค๋น„๊ฒŒ์ด์…˜ โ”€โ”€ */
$id('homeBtn').onclick=()=>{
if(fb) {
fb.destroy();
viewer.innerHTML = '';
fb = null;
}
toggle(true);
};
function toggle(showHome){
$id('home').style.display=showHome?'block':'none';
$id('viewerPage').style.display=showHome?'none':'block';
$id('homeBtn').style.display=showHome?'none':'inline-block';
// ์ถ”๊ฐ€: ์ „์ฒด ํ™”๋ฉด ๋ชจ๋“œ์—์„œ homeBtn ์œ„์น˜ ์กฐ์ •
if(!showHome) {
$id('homeBtn').style.position = 'fixed';
$id('homeBtn').style.top = '20px';
$id('homeBtn').style.left = '20px';
$id('homeBtn').style.zIndex = '9999'; // ์ตœ์ƒ์œ„ z-index๋กœ ๋ณ€๊ฒฝ
$id('homeBtn').style.fontSize = '24px'; // ํฌ๊ธฐ ์ฆ๊ฐ€
$id('homeBtn').style.width = '48px';
$id('homeBtn').style.height = '48px';
$id('homeBtn').style.boxShadow = '0 2px 10px rgba(0,0,0,0.2)'; // ๊ทธ๋ฆผ์ž ์ถ”๊ฐ€
// ๋ฐฐ๊ฒฝ ์˜ค๋ฒ„๋ ˆ์ด ์ถ”๊ฐ€
document.body.style.backgroundColor = '#3a3a3a';
} else {
$id('homeBtn').style.position = '';
$id('homeBtn').style.top = '';
$id('homeBtn').style.left = '';
$id('homeBtn').style.zIndex = '';
$id('homeBtn').style.fontSize = '';
$id('homeBtn').style.width = '';
$id('homeBtn').style.height = '';
$id('homeBtn').style.boxShadow = '';
// ๋ฐฐ๊ฒฝ ์›๋ž˜๋Œ€๋กœ
document.body.style.backgroundColor = '#f0f0f0';
}
}
// ํŽ˜์ด์ง€ ๋กœ๋“œ ์‹œ ์„œ๋ฒ„ PDF ๋กœ๋“œ
window.addEventListener('DOMContentLoaded', loadServerPDFs);
</script>
</body></html>
"""
# API ์—”๋“œํฌ์ธํŠธ: PDF ํ”„๋กœ์ ํŠธ ๋ชฉ๋ก
@app.get("/api/pdf-projects")
async def get_pdf_projects():
return generate_pdf_projects()
# API ์—”๋“œํฌ์ธํŠธ: PDF ์ธ๋„ค์ผ ์ƒ์„ฑ
@app.get("/api/pdf-thumbnail")
async def get_pdf_thumbnail(path: str):
try:
import fitz # PyMuPDF
# PDF ํŒŒ์ผ ์—ด๊ธฐ
doc = fitz.open(path)
# ์ฒซ ํŽ˜์ด์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ
if doc.page_count > 0:
page = doc[0]
# ์ธ๋„ค์ผ์šฉ ์ด๋ฏธ์ง€ ๋ Œ๋”๋ง (ํ•ด์ƒ๋„ ์กฐ์ •)
pix = page.get_pixmap(matrix=fitz.Matrix(0.5, 0.5))
img_data = pix.tobytes("png")
# Base64 ์ธ์ฝ”๋”ฉ
b64_img = base64.b64encode(img_data).decode('utf-8')
return {"thumbnail": f"data:image/png;base64,{b64_img}"}
return {"thumbnail": None}
except Exception as e:
return {"error": str(e), "thumbnail": None}
@app.get("/api/pdf-content")
async def get_pdf_content(path: str):
try:
# ํŒŒ์ผ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ
pdf_path = pathlib.Path(path)
if not pdf_path.exists():
return {"error": f"ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: {path}"}, 404
# ํŒŒ์ผ ์ฝ๊ธฐ
with open(path, "rb") as pdf_file:
content = pdf_file.read()
# ํŒŒ์ผ๋ช… ์ฒ˜๋ฆฌ - URL ์ธ์ฝ”๋”ฉ์œผ๋กœ ํ•œ๊ธ€ ๋“ฑ ํŠน์ˆ˜ ๋ฌธ์ž ์ฒ˜๋ฆฌ
import urllib.parse
filename = pdf_path.name
encoded_filename = urllib.parse.quote(filename)
# ์‘๋‹ต ํ—ค๋” ์„ค์ • - RFC 6266 ํ‘œ์ค€ ์‚ฌ์šฉ
headers = {
"Content-Type": "application/pdf",
"Content-Disposition": f"inline; filename=\"{encoded_filename}\"; filename*=UTF-8''{encoded_filename}"
}
# ํŒŒ์ผ ์ฝ˜ํ…์ธ  ์ง์ ‘ ๋ฐ˜ํ™˜ (dict๊ฐ€ ์•„๋‹Œ Response ๊ฐ์ฒด)
from fastapi.responses import Response
return Response(content=content, media_type="application/pdf")
except Exception as e:
import traceback
error_details = traceback.format_exc()
print(f"PDF ์ฝ˜ํ…์ธ  ๋กœ๋“œ ์˜ค๋ฅ˜: {str(e)}\n{error_details}")
# ์˜ค๋ฅ˜ ์‘๋‹ต ๋ฐ˜ํ™˜ (JSON ํ˜•์‹)
from fastapi.responses import JSONResponse
return JSONResponse(content={"error": str(e)}, status_code=500)
@app.get("/", response_class=HTMLResponse)
async def root():
return HTML
if __name__ == "__main__":
uvicorn.run("app:app", host="0.0.0.0", port=int(os.getenv("PORT", 7860)))