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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
import pathlib, os, uvicorn, base64
|
| 5 |
+
|
| 6 |
+
BASE = pathlib.Path(__file__).parent
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
app.mount("/static", StaticFiles(directory=BASE), name="static")
|
| 9 |
+
|
| 10 |
+
# PDF ๋๋ ํ ๋ฆฌ ์ค์
|
| 11 |
+
PDF_DIR = BASE / "pdf"
|
| 12 |
+
if not PDF_DIR.exists():
|
| 13 |
+
PDF_DIR.mkdir(parents=True)
|
| 14 |
+
|
| 15 |
+
# PDF ํ์ผ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
|
| 16 |
+
def get_pdf_files():
|
| 17 |
+
pdf_files = []
|
| 18 |
+
if PDF_DIR.exists():
|
| 19 |
+
pdf_files = [f for f in PDF_DIR.glob("*.pdf")]
|
| 20 |
+
return pdf_files
|
| 21 |
+
|
| 22 |
+
# PDF ์ธ๋ค์ผ ์์ฑ ๋ฐ ํ๋ก์ ํธ ๋ฐ์ดํฐ ์ค๋น
|
| 23 |
+
def generate_pdf_projects():
|
| 24 |
+
projects_data = []
|
| 25 |
+
pdf_files = get_pdf_files()
|
| 26 |
+
|
| 27 |
+
for pdf_file in pdf_files:
|
| 28 |
+
projects_data.append({
|
| 29 |
+
"path": str(pdf_file),
|
| 30 |
+
"name": pdf_file.stem
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
return projects_data
|
| 34 |
+
|
| 35 |
+
HTML = """
|
| 36 |
+
<!doctype html><html lang="ko"><head>
|
| 37 |
+
<meta charset="utf-8"><title>FlipBook Space</title>
|
| 38 |
+
<link rel="stylesheet" href="/static/flipbook.css">
|
| 39 |
+
<script src="/static/three.js"></script>
|
| 40 |
+
<script src="/static/iscroll.js"></script>
|
| 41 |
+
<script src="/static/mark.js"></script>
|
| 42 |
+
<script src="/static/mod3d.js"></script>
|
| 43 |
+
<script src="/static/pdf.js"></script>
|
| 44 |
+
<script src="/static/flipbook.js"></script>
|
| 45 |
+
<script src="/static/flipbook.book3.js"></script>
|
| 46 |
+
<script src="/static/flipbook.scroll.js"></script>
|
| 47 |
+
<script src="/static/flipbook.swipe.js"></script>
|
| 48 |
+
<script src="/static/flipbook.webgl.js"></script>
|
| 49 |
+
<style>
|
| 50 |
+
body{margin:0;background:#f0f0f0;font-family:sans-serif}
|
| 51 |
+
header{max-width:960px;margin:0 auto;padding:18px 20px;display:flex;align-items:center}
|
| 52 |
+
#homeBtn{display:none;width:38px;height:38px;border:none;border-radius:50%;cursor:pointer;
|
| 53 |
+
background:#0077c2;color:#fff;font-size:20px;margin-right:12px}
|
| 54 |
+
#homeBtn:hover{background:#005999}
|
| 55 |
+
h2{margin:0;font-size:1.5rem;font-weight:600}
|
| 56 |
+
#home,#viewerPage{max-width:960px;margin:0 auto;padding:0 20px 40px}
|
| 57 |
+
.grid{display:grid;grid-template-columns:repeat(auto-fill,180px);gap:16px;margin-top:24px}
|
| 58 |
+
.card{
|
| 59 |
+
background:#fff url('/static/book2.jpg') no-repeat center center;
|
| 60 |
+
background-size: 169%; /* ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ํ์ฌ๋ณด๋ค 30% ๋ ํค์ (130% * 1.3 = 169%) */
|
| 61 |
+
border:1px solid #ccc;
|
| 62 |
+
border-radius:6px;
|
| 63 |
+
cursor:pointer;
|
| 64 |
+
box-shadow:0 2px 4px rgba(0,0,0,.12);
|
| 65 |
+
width: 180px;
|
| 66 |
+
height: 240px;
|
| 67 |
+
position: relative;
|
| 68 |
+
display: flex;
|
| 69 |
+
flex-direction: column;
|
| 70 |
+
align-items: center;
|
| 71 |
+
justify-content: center;
|
| 72 |
+
}
|
| 73 |
+
.card img{
|
| 74 |
+
width:55%; /* ์ธ๋ค์ผ ํฌ๊ธฐ ์ค์ (๋ฐฐ๊ฒฝ์ด ์ปค์ ธ์ ๋น์จ ์ ์ง) */
|
| 75 |
+
height:auto;
|
| 76 |
+
object-fit:contain;
|
| 77 |
+
position:absolute; /* ์ ๋ ์์น๋ก ๋ณ๊ฒฝ */
|
| 78 |
+
top:50%; /* ์๋จ์์ 50% */
|
| 79 |
+
left:50%; /* ์ข์ธก์์ 50% */
|
| 80 |
+
transform: translate(-50%, -70%); /* ์ ์ค์์์ 20% ์๋ก ์ด๋ */
|
| 81 |
+
border: 1px solid #ddd;
|
| 82 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
|
| 83 |
+
}
|
| 84 |
+
.card p{
|
| 85 |
+
text-align:center;
|
| 86 |
+
margin:6px 0;
|
| 87 |
+
position: absolute;
|
| 88 |
+
bottom: 10px;
|
| 89 |
+
left: 50%;
|
| 90 |
+
transform: translateX(-50%);
|
| 91 |
+
background: rgba(255, 255, 255, 0.7);
|
| 92 |
+
padding: 4px 8px;
|
| 93 |
+
border-radius: 4px;
|
| 94 |
+
width: 85%;
|
| 95 |
+
white-space: nowrap;
|
| 96 |
+
overflow: hidden;
|
| 97 |
+
text-overflow: ellipsis;
|
| 98 |
+
max-width: 150px;
|
| 99 |
+
font-size: 11px; /* ๊ธฐ๋ณธ 16px์์ ์ฝ 30% ๊ฐ์ */
|
| 100 |
+
}
|
| 101 |
+
button.upload{all:unset;cursor:pointer;border:1px solid #bbb;padding:8px 14px;border-radius:6px;background:#fff;margin:0 8px}
|
| 102 |
+
#viewer{
|
| 103 |
+
width:90%;
|
| 104 |
+
height:90vh;
|
| 105 |
+
max-width:90%;
|
| 106 |
+
margin:0;
|
| 107 |
+
background:#fff;
|
| 108 |
+
border:1px solid #ddd;
|
| 109 |
+
border-radius:8px;
|
| 110 |
+
position:fixed;
|
| 111 |
+
top:50%;
|
| 112 |
+
left:50%;
|
| 113 |
+
transform:translate(-50%, -50%);
|
| 114 |
+
z-index:1000;
|
| 115 |
+
box-shadow:0 4px 20px rgba(0,0,0,0.15);
|
| 116 |
+
/* ํ๋ฉด ๋น์จ์ ๋ง๊ฒ ์กฐ์ */
|
| 117 |
+
max-height: calc(90vh - 40px); /* ์ฌ๋ฐฑ ๊ณ ๋ ค */
|
| 118 |
+
aspect-ratio: auto; /* ๋น์จ ์๋ ์กฐ์ */
|
| 119 |
+
object-fit: contain; /* ๋ด์ฉ๋ฌผ ๋น์จ ์ ์ง */
|
| 120 |
+
overflow: hidden; /* ๋ด์ฉ ๋์นจ ๋ฐฉ์ง */
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
/* FlipBook ์ปจํธ๋กค๋ฐ ๊ด๋ จ ์คํ์ผ ์ค๋ฒ๋ผ์ด๋ */
|
| 124 |
+
.flipbook-container .fb3d-menu-bar {
|
| 125 |
+
z-index: 2000 !important; /* ์ปจํธ๋กค๋ฐ๊ฐ ๋ค๋ฅธ ์์๋ณด๋ค ์์ ์ค๋๋ก ํจ */
|
| 126 |
+
opacity: 1 !important; /* ํญ์ ํ์๋๋๋ก ํจ */
|
| 127 |
+
bottom: 0 !important; /* ํ๋จ์ ๊ณ ์ */
|
| 128 |
+
background-color: rgba(0,0,0,0.7) !important; /* ๋ฐฐ๊ฒฝ์ ์ค์ */
|
| 129 |
+
border-radius: 0 0 8px 8px !important; /* ํ๋จ ๋ชจ์๋ฆฌ๋ง ๋ฅ๊ธ๊ฒ */
|
| 130 |
+
padding: 8px 0 !important; /* ์ํ ํจ๋ฉ ์ถ๊ฐ */
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
.flipbook-container .fb3d-menu-bar > ul > li > img,
|
| 134 |
+
.flipbook-container .fb3d-menu-bar > ul > li > div {
|
| 135 |
+
opacity: 1 !important; /* ๋ฉ๋ด ์์ด์ฝ ํญ์ ํ์ */
|
| 136 |
+
transform: scale(1.2) !important; /* ์์ด์ฝ ํฌ๊ธฐ ์ฝ๊ฐ ํค์ */
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
.flipbook-container .fb3d-menu-bar > ul > li {
|
| 140 |
+
margin: 0 8px !important; /* ๋ฉ๋ด ์์ดํ
๊ฐ๊ฒฉ ์กฐ์ */
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
/* ๋ฉ๋ด ํดํ ์คํ์ผ ๊ฐ์ */
|
| 144 |
+
.flipbook-container .fb3d-menu-bar > ul > li > span {
|
| 145 |
+
background-color: rgba(0,0,0,0.8) !important;
|
| 146 |
+
color: white !important;
|
| 147 |
+
border-radius: 4px !important;
|
| 148 |
+
padding: 4px 8px !important;
|
| 149 |
+
font-size: 12px !important;
|
| 150 |
+
bottom: 45px !important; /* ํดํ ์์น ์กฐ์ */
|
| 151 |
+
}
|
| 152 |
+
</style></head><body>
|
| 153 |
+
|
| 154 |
+
<header>
|
| 155 |
+
<button id="homeBtn" title="ํ์ผ๋ก">๐ </button>
|
| 156 |
+
<h2>My FlipBook Projects</h2>
|
| 157 |
+
</header>
|
| 158 |
+
|
| 159 |
+
<section id="home">
|
| 160 |
+
<div>
|
| 161 |
+
<label class="upload">๐ท ์ด๋ฏธ์ง <input id="imgInput" type="file" accept="image/*" multiple hidden></label>
|
| 162 |
+
<label class="upload">๐ PDF <input id="pdfInput" type="file" accept="application/pdf" hidden></label>
|
| 163 |
+
</div>
|
| 164 |
+
<div class="grid" id="grid"></div>
|
| 165 |
+
</section>
|
| 166 |
+
|
| 167 |
+
<section id="viewerPage" style="display:none">
|
| 168 |
+
<div id="viewer"></div>
|
| 169 |
+
</section>
|
| 170 |
+
|
| 171 |
+
<script>
|
| 172 |
+
let projects=[], fb=null;
|
| 173 |
+
const grid=$id('grid'), viewer=$id('viewer');
|
| 174 |
+
pdfjsLib.GlobalWorkerOptions.workerSrc='/static/pdf.worker.js';
|
| 175 |
+
|
| 176 |
+
// ์๋ฒ์์ ๋ฏธ๋ฆฌ ๋ก๋๋ PDF ํ๋ก์ ํธ
|
| 177 |
+
let serverProjects = [];
|
| 178 |
+
|
| 179 |
+
/* ๐ ์ค๋์ค unlock โ ๋ด์ฅ Audio ์ ๊ฐ์ MP3 ๊ฒฝ๋ก ์ฌ์ฉ */
|
| 180 |
+
['click','touchstart'].forEach(evt=>{
|
| 181 |
+
document.addEventListener(evt,function u(){new Audio('static/turnPage2.mp3')
|
| 182 |
+
.play().then(a=>a.pause()).catch(()=>{});document.removeEventListener(evt,u,{capture:true});},
|
| 183 |
+
{once:true,capture:true});
|
| 184 |
+
});
|
| 185 |
+
|
| 186 |
+
/* โโ ์ ํธ โโ */
|
| 187 |
+
function $id(id){return document.getElementById(id)}
|
| 188 |
+
function addCard(i,thumb,title){
|
| 189 |
+
const d=document.createElement('div');
|
| 190 |
+
d.className='card';
|
| 191 |
+
d.onclick=()=>open(i);
|
| 192 |
+
|
| 193 |
+
// ์ ๋ชฉ 10๊ธ์ ์ ํ ๋ฐ ๋ง์ค์ํ ์ฒ๋ฆฌ
|
| 194 |
+
const displayTitle = title ?
|
| 195 |
+
(title.length > 10 ? title.substring(0, 10) + '...' : title) :
|
| 196 |
+
'ํ๋ก์ ํธ ' + (i+1);
|
| 197 |
+
|
| 198 |
+
d.innerHTML=`<img src="${thumb}"><p title="${title || 'ํ๋ก์ ํธ ' + (i+1)}">${displayTitle}</p>`;
|
| 199 |
+
grid.appendChild(d);
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
/* โโ ์ด๋ฏธ์ง ์
๋ก๋ โโ */
|
| 203 |
+
$id('imgInput').onchange=e=>{
|
| 204 |
+
const files=[...e.target.files]; if(!files.length) return;
|
| 205 |
+
const pages=[],tot=files.length;let done=0;
|
| 206 |
+
files.forEach((f,i)=>{const r=new FileReader();r.onload=x=>{pages[i]={src:x.target.result,thumb:x.target.result};
|
| 207 |
+
if(++done===tot) save(pages);};r.readAsDataURL(f);});
|
| 208 |
+
};
|
| 209 |
+
|
| 210 |
+
/* โโ PDF ์
๋ก๋ โโ */
|
| 211 |
+
$id('pdfInput').onchange=e=>{
|
| 212 |
+
const file=e.target.files[0]; if(!file) return;
|
| 213 |
+
const fr=new FileReader();
|
| 214 |
+
fr.onload=v=>{
|
| 215 |
+
pdfjsLib.getDocument({data:v.target.result}).promise.then(async pdf=>{
|
| 216 |
+
const pages=[];
|
| 217 |
+
for(let p=1;p<=pdf.numPages;p++){
|
| 218 |
+
const pg=await pdf.getPage(p), vp=pg.getViewport({scale:1});
|
| 219 |
+
const c=document.createElement('canvas');c.width=vp.width;c.height=vp.height;
|
| 220 |
+
await pg.render({canvasContext:c.getContext('2d'),viewport:vp}).promise;
|
| 221 |
+
pages.push({src:c.toDataURL(),thumb:c.toDataURL()});
|
| 222 |
+
}
|
| 223 |
+
save(pages, file.name.replace('.pdf', ''));
|
| 224 |
+
});
|
| 225 |
+
};fr.readAsArrayBuffer(file);
|
| 226 |
+
};
|
| 227 |
+
|
| 228 |
+
/* โโ ํ๋ก์ ํธ ์ ์ฅ โโ */
|
| 229 |
+
function save(pages, title){
|
| 230 |
+
const id=projects.push(pages)-1;
|
| 231 |
+
addCard(id,pages[0].thumb, title);
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
/* โโ ์๋ฒ PDF ๋ก๋ โโ */
|
| 235 |
+
async function loadServerPDFs() {
|
| 236 |
+
try {
|
| 237 |
+
const response = await fetch('/api/pdf-projects');
|
| 238 |
+
serverProjects = await response.json();
|
| 239 |
+
|
| 240 |
+
// ์๋ฒ PDF ๋ก๋ ๋ฐ ์ธ๋ค์ผ ์์ฑ
|
| 241 |
+
for(let i = 0; i < serverProjects.length; i++) {
|
| 242 |
+
const project = serverProjects[i];
|
| 243 |
+
const response = await fetch(`/api/pdf-thumbnail?path=${encodeURIComponent(project.path)}`);
|
| 244 |
+
const data = await response.json();
|
| 245 |
+
|
| 246 |
+
if(data.thumbnail) {
|
| 247 |
+
const pages = [{
|
| 248 |
+
src: data.thumbnail,
|
| 249 |
+
thumb: data.thumbnail,
|
| 250 |
+
path: project.path
|
| 251 |
+
}];
|
| 252 |
+
|
| 253 |
+
save(pages, project.name);
|
| 254 |
+
}
|
| 255 |
+
}
|
| 256 |
+
} catch(error) {
|
| 257 |
+
console.error('์๋ฒ PDF ๋ก๋ ์คํจ:', error);
|
| 258 |
+
}
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
/* โโ ์นด๋ โ FlipBook โโ */
|
| 262 |
+
function open(i){
|
| 263 |
+
toggle(false);
|
| 264 |
+
const pages = projects[i];
|
| 265 |
+
|
| 266 |
+
// ๋ก์ปฌ ํ๋ก์ ํธ ๋๋ ์๋ฒ PDF ๋ก๋
|
| 267 |
+
if(fb){fb.destroy();viewer.innerHTML='';}
|
| 268 |
+
|
| 269 |
+
if(pages[0].path) {
|
| 270 |
+
// ๋ก๋ฉ ํ์
|
| 271 |
+
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>';
|
| 272 |
+
|
| 273 |
+
// ์คํ์ผ ์ถ๊ฐ
|
| 274 |
+
if (!document.getElementById('loadingStyle')) {
|
| 275 |
+
const style = document.createElement('style');
|
| 276 |
+
style.id = 'loadingStyle';
|
| 277 |
+
style.textContent = '@keyframes spin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}';
|
| 278 |
+
document.head.appendChild(style);
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
// ์๋ฒ PDF ํ์ผ ๋ก๋
|
| 282 |
+
fetch(`/api/pdf-content?path=${encodeURIComponent(pages[0].path)}`)
|
| 283 |
+
.then(response => {
|
| 284 |
+
if (!response.ok) {
|
| 285 |
+
throw new Error('PDF ๋ก๋ ์คํจ: ' + response.statusText);
|
| 286 |
+
}
|
| 287 |
+
return response.arrayBuffer();
|
| 288 |
+
})
|
| 289 |
+
.then(pdfData => {
|
| 290 |
+
// PDF ๋ฐ์ดํฐ ๋ก๋ ํ์ธ ๋ก๊น
|
| 291 |
+
console.log('PDF ๋ฐ์ดํฐ ๋ก๋ ์๋ฃ:', pdfData.byteLength + ' ๋ฐ์ดํธ');
|
| 292 |
+
|
| 293 |
+
return pdfjsLib.getDocument({data: pdfData}).promise;
|
| 294 |
+
})
|
| 295 |
+
.then(async pdf => {
|
| 296 |
+
console.log('PDF ๋ฌธ์ ๋ก๋ ์๋ฃ. ํ์ด์ง ์:', pdf.numPages);
|
| 297 |
+
|
| 298 |
+
const pdfPages = [];
|
| 299 |
+
const progressElement = viewer.querySelector('p');
|
| 300 |
+
|
| 301 |
+
for(let p = 1; p <= pdf.numPages; p++) {
|
| 302 |
+
if (progressElement) {
|
| 303 |
+
progressElement.textContent = `PDF ํ์ด์ง ๋ก๋ฉ ์ค... (${p}/${pdf.numPages})`;
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
try {
|
| 307 |
+
const pg = await pdf.getPage(p);
|
| 308 |
+
const vp = pg.getViewport({scale: 1});
|
| 309 |
+
const c = document.createElement('canvas');
|
| 310 |
+
c.width = vp.width;
|
| 311 |
+
c.height = vp.height;
|
| 312 |
+
|
| 313 |
+
await pg.render({canvasContext: c.getContext('2d'), viewport: vp}).promise;
|
| 314 |
+
pdfPages.push({src: c.toDataURL(), thumb: c.toDataURL()});
|
| 315 |
+
} catch (pageError) {
|
| 316 |
+
console.error(`ํ์ด์ง ${p} ๋ ๋๋ง ์ค๋ฅ:`, pageError);
|
| 317 |
+
}
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
console.log('๋ชจ๋ ํ์ด์ง ๋ ๋๋ง ์๋ฃ:', pdfPages.length);
|
| 321 |
+
|
| 322 |
+
if (pdfPages.length > 0) {
|
| 323 |
+
createFlipBook(pdfPages);
|
| 324 |
+
} else {
|
| 325 |
+
throw new Error('PDF์์ ํ์ด์ง๋ฅผ ์ถ์ถํ ์ ์์ต๋๋ค.');
|
| 326 |
+
}
|
| 327 |
+
})
|
| 328 |
+
.catch(error => {
|
| 329 |
+
console.error('PDF ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:', error);
|
| 330 |
+
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>`;
|
| 331 |
+
|
| 332 |
+
document.getElementById('backBtn').addEventListener('click', function() {
|
| 333 |
+
toggle(true);
|
| 334 |
+
});
|
| 335 |
+
});
|
| 336 |
+
} else {
|
| 337 |
+
// ์
๋ก๋๋ ํ๋ก์ ํธ ๋ณด๊ธฐ
|
| 338 |
+
console.log('๋ก์ปฌ ์
๋ก๋๋ ํ๋ก์ ํธ ๋ ๋๋ง:', pages.length + 'ํ์ด์ง');
|
| 339 |
+
createFlipBook(pages);
|
| 340 |
+
}
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
function createFlipBook(pages) {
|
| 344 |
+
console.log('FlipBook ์์ฑ ์์. ํ์ด์ง ์:', pages.length);
|
| 345 |
+
|
| 346 |
+
try {
|
| 347 |
+
// ํ๋ฉด ๋น์จ ๊ณ์ฐ
|
| 348 |
+
const calculateAspectRatio = () => {
|
| 349 |
+
const windowWidth = window.innerWidth;
|
| 350 |
+
const windowHeight = window.innerHeight;
|
| 351 |
+
const aspectRatio = windowWidth / windowHeight;
|
| 352 |
+
|
| 353 |
+
// ๋๋น ๋๋ ๋์ด ๊ธฐ์ค์ผ๋ก ์ต๋ 90% ์ ํ
|
| 354 |
+
let width, height;
|
| 355 |
+
if (aspectRatio > 1) { // ๊ฐ๋ก ํ๋ฉด
|
| 356 |
+
height = Math.min(windowHeight * 0.9, windowHeight - 40);
|
| 357 |
+
width = height * aspectRatio * 0.8; // ๊ฐ๋ก ํ๋ฉด์์๋ ์ฝ๊ฐ ์ค์
|
| 358 |
+
if (width > windowWidth * 0.9) {
|
| 359 |
+
width = windowWidth * 0.9;
|
| 360 |
+
height = width / (aspectRatio * 0.8);
|
| 361 |
+
}
|
| 362 |
+
} else { // ์ธ๋ก ํ๋ฉด
|
| 363 |
+
width = Math.min(windowWidth * 0.9, windowWidth - 40);
|
| 364 |
+
height = width / aspectRatio * 0.9; // ์ธ๋ก ํ๋ฉด์์๋ ์ฝ๊ฐ ๋๋ฆผ
|
| 365 |
+
if (height > windowHeight * 0.9) {
|
| 366 |
+
height = windowHeight * 0.9;
|
| 367 |
+
width = height * aspectRatio * 0.9;
|
| 368 |
+
}
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
// ์ต์ ์ฌ์ด์ฆ ๋ฐํ
|
| 372 |
+
return {
|
| 373 |
+
width: Math.round(width),
|
| 374 |
+
height: Math.round(height)
|
| 375 |
+
};
|
| 376 |
+
};
|
| 377 |
+
|
| 378 |
+
// ์ด๊ธฐ ํ๋ฉด ๋น์จ ๊ณ์ฐ
|
| 379 |
+
const size = calculateAspectRatio();
|
| 380 |
+
viewer.style.width = size.width + 'px';
|
| 381 |
+
viewer.style.height = size.height + 'px';
|
| 382 |
+
|
| 383 |
+
fb = new FlipBook(viewer, {
|
| 384 |
+
pages: pages,
|
| 385 |
+
viewMode: 'webgl',
|
| 386 |
+
autoSize: true,
|
| 387 |
+
flipDuration: 800,
|
| 388 |
+
backgroundColor: '#fff',
|
| 389 |
+
/* ๐ ๋ด์ฅ ์ฌ์ด๋ */
|
| 390 |
+
sound: true,
|
| 391 |
+
assets: {flipMp3: 'static/turnPage2.mp3', hardFlipMp3: 'static/turnPage2.mp3'},
|
| 392 |
+
controlsProps: {
|
| 393 |
+
enableFullscreen: true,
|
| 394 |
+
enableToc: true,
|
| 395 |
+
enableDownload: false,
|
| 396 |
+
enablePrint: false,
|
| 397 |
+
enableZoom: true,
|
| 398 |
+
enableShare: false,
|
| 399 |
+
enableSearch: true,
|
| 400 |
+
enableAutoPlay: true,
|
| 401 |
+
enableAnnotation: false,
|
| 402 |
+
enableSound: true,
|
| 403 |
+
enableLightbox: false,
|
| 404 |
+
layout: 10, // ๋ ์ด์์ ์ต์
|
| 405 |
+
skin: 'light', // ์คํจ ์คํ์ผ
|
| 406 |
+
autoNavigationTime: 3600, // ์๋ ๋๊น ์๊ฐ(์ด)
|
| 407 |
+
hideControls: false, // ์ปจํธ๋กค ์จ๊น ๋นํ์ฑํ
|
| 408 |
+
paddingTop: 10, // ์๋จ ํจ๋ฉ
|
| 409 |
+
paddingLeft: 10, // ์ข์ธก ํจ๋ฉ
|
| 410 |
+
paddingRight: 10, // ์ฐ์ธก ํจ๋ฉ
|
| 411 |
+
paddingBottom: 10, // ํ๋จ ํจ๋ฉ
|
| 412 |
+
pageTextureSize: 1024, // ํ์ด์ง ํ
์ค์ฒ ํฌ๊ธฐ
|
| 413 |
+
thumbnails: true, // ์ฌ๋ค์ผ ํ์ฑํ
|
| 414 |
+
autoHideControls: false, // ์๋ ์จ๊น ๋นํ์ฑํ
|
| 415 |
+
controlsTimeout: 8000 // ์ปจํธ๋กค ํ์ ์๊ฐ ์ฐ์ฅ
|
| 416 |
+
}
|
| 417 |
+
});
|
| 418 |
+
|
| 419 |
+
// ํ๋ฉด ํฌ๊ธฐ ๋ณ๊ฒฝ ์ FlipBook ํฌ๊ธฐ ์กฐ์
|
| 420 |
+
window.addEventListener('resize', () => {
|
| 421 |
+
if (fb) {
|
| 422 |
+
const newSize = calculateAspectRatio();
|
| 423 |
+
viewer.style.width = newSize.width + 'px';
|
| 424 |
+
viewer.style.height = newSize.height + 'px';
|
| 425 |
+
fb.resize();
|
| 426 |
+
}
|
| 427 |
+
});
|
| 428 |
+
|
| 429 |
+
// FlipBook ์์ฑ ํ ์ปจํธ๋กค๋ฐ ๊ฐ์ ํ์
|
| 430 |
+
setTimeout(() => {
|
| 431 |
+
try {
|
| 432 |
+
// ์ปจํธ๋กค๋ฐ ๊ด๋ จ ์์ ์ฐพ๊ธฐ ๋ฐ ์คํ์ผ ์ ์ฉ
|
| 433 |
+
const menuBars = document.querySelectorAll('.flipbook-container .fb3d-menu-bar');
|
| 434 |
+
if (menuBars && menuBars.length > 0) {
|
| 435 |
+
menuBars.forEach(menuBar => {
|
| 436 |
+
menuBar.style.display = 'block';
|
| 437 |
+
menuBar.style.opacity = '1';
|
| 438 |
+
menuBar.style.visibility = 'visible';
|
| 439 |
+
menuBar.style.zIndex = '9999';
|
| 440 |
+
});
|
| 441 |
+
}
|
| 442 |
+
} catch (e) {
|
| 443 |
+
console.warn('์ปจํธ๋กค๋ฐ ์คํ์ผ ์ ์ฉ ์ค ์ค๋ฅ:', e);
|
| 444 |
+
}
|
| 445 |
+
}, 1000);
|
| 446 |
+
|
| 447 |
+
console.log('FlipBook ์์ฑ ์๋ฃ');
|
| 448 |
+
} catch (error) {
|
| 449 |
+
console.error('FlipBook ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:', error);
|
| 450 |
+
alert('FlipBook์ ์์ฑํ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: ' + error.message);
|
| 451 |
+
}
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
/* โโ ๋ค๋น๊ฒ์ด์
โโ */
|
| 455 |
+
$id('homeBtn').onclick=()=>{
|
| 456 |
+
if(fb) {
|
| 457 |
+
fb.destroy();
|
| 458 |
+
viewer.innerHTML = '';
|
| 459 |
+
fb = null;
|
| 460 |
+
}
|
| 461 |
+
toggle(true);
|
| 462 |
+
};
|
| 463 |
+
|
| 464 |
+
function toggle(showHome){
|
| 465 |
+
$id('home').style.display=showHome?'block':'none';
|
| 466 |
+
$id('viewerPage').style.display=showHome?'none':'block';
|
| 467 |
+
$id('homeBtn').style.display=showHome?'none':'inline-block';
|
| 468 |
+
|
| 469 |
+
// ์ถ๊ฐ: ์ ์ฒด ํ๋ฉด ๋ชจ๋์์ homeBtn ์์น ์กฐ์
|
| 470 |
+
if(!showHome) {
|
| 471 |
+
$id('homeBtn').style.position = 'fixed';
|
| 472 |
+
$id('homeBtn').style.top = '20px';
|
| 473 |
+
$id('homeBtn').style.left = '20px';
|
| 474 |
+
$id('homeBtn').style.zIndex = '9999'; // ์ต์์ z-index๋ก ๋ณ๊ฒฝ
|
| 475 |
+
$id('homeBtn').style.fontSize = '24px'; // ํฌ๊ธฐ ์ฆ๊ฐ
|
| 476 |
+
$id('homeBtn').style.width = '48px';
|
| 477 |
+
$id('homeBtn').style.height = '48px';
|
| 478 |
+
$id('homeBtn').style.boxShadow = '0 2px 10px rgba(0,0,0,0.2)'; // ๊ทธ๋ฆผ์ ์ถ๊ฐ
|
| 479 |
+
|
| 480 |
+
// ๋ฐฐ๊ฒฝ ์ค๋ฒ๋ ์ด ์ถ๊ฐ
|
| 481 |
+
document.body.style.backgroundColor = '#3a3a3a';
|
| 482 |
+
} else {
|
| 483 |
+
$id('homeBtn').style.position = '';
|
| 484 |
+
$id('homeBtn').style.top = '';
|
| 485 |
+
$id('homeBtn').style.left = '';
|
| 486 |
+
$id('homeBtn').style.zIndex = '';
|
| 487 |
+
$id('homeBtn').style.fontSize = '';
|
| 488 |
+
$id('homeBtn').style.width = '';
|
| 489 |
+
$id('homeBtn').style.height = '';
|
| 490 |
+
$id('homeBtn').style.boxShadow = '';
|
| 491 |
+
|
| 492 |
+
// ๋ฐฐ๊ฒฝ ์๋๋๋ก
|
| 493 |
+
document.body.style.backgroundColor = '#f0f0f0';
|
| 494 |
+
}
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
// ํ์ด์ง ๋ก๋ ์ ์๋ฒ PDF ๋ก๋
|
| 498 |
+
window.addEventListener('DOMContentLoaded', loadServerPDFs);
|
| 499 |
+
</script>
|
| 500 |
+
</body></html>
|
| 501 |
+
"""
|
| 502 |
+
|
| 503 |
+
# API ์๋ํฌ์ธํธ: PDF ํ๋ก์ ํธ ๋ชฉ๋ก
|
| 504 |
+
@app.get("/api/pdf-projects")
|
| 505 |
+
async def get_pdf_projects():
|
| 506 |
+
return generate_pdf_projects()
|
| 507 |
+
|
| 508 |
+
# API ์๋ํฌ์ธํธ: PDF ์ธ๋ค์ผ ์์ฑ
|
| 509 |
+
@app.get("/api/pdf-thumbnail")
|
| 510 |
+
async def get_pdf_thumbnail(path: str):
|
| 511 |
+
try:
|
| 512 |
+
import fitz # PyMuPDF
|
| 513 |
+
|
| 514 |
+
# PDF ํ์ผ ์ด๊ธฐ
|
| 515 |
+
doc = fitz.open(path)
|
| 516 |
+
|
| 517 |
+
# ์ฒซ ํ์ด์ง ๊ฐ์ ธ์ค๊ธฐ
|
| 518 |
+
if doc.page_count > 0:
|
| 519 |
+
page = doc[0]
|
| 520 |
+
# ์ธ๋ค์ผ์ฉ ์ด๋ฏธ์ง ๋ ๋๋ง (ํด์๋ ์กฐ์ )
|
| 521 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(0.5, 0.5))
|
| 522 |
+
img_data = pix.tobytes("png")
|
| 523 |
+
|
| 524 |
+
# Base64 ์ธ์ฝ๋ฉ
|
| 525 |
+
b64_img = base64.b64encode(img_data).decode('utf-8')
|
| 526 |
+
return {"thumbnail": f"data:image/png;base64,{b64_img}"}
|
| 527 |
+
|
| 528 |
+
return {"thumbnail": None}
|
| 529 |
+
except Exception as e:
|
| 530 |
+
return {"error": str(e), "thumbnail": None}
|
| 531 |
+
|
| 532 |
+
@app.get("/api/pdf-content")
|
| 533 |
+
async def get_pdf_content(path: str):
|
| 534 |
+
try:
|
| 535 |
+
# ํ์ผ ์กด์ฌ ์ฌ๋ถ ํ์ธ
|
| 536 |
+
pdf_path = pathlib.Path(path)
|
| 537 |
+
if not pdf_path.exists():
|
| 538 |
+
return {"error": f"ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค: {path}"}, 404
|
| 539 |
+
|
| 540 |
+
# ํ์ผ ์ฝ๊ธฐ
|
| 541 |
+
with open(path, "rb") as pdf_file:
|
| 542 |
+
content = pdf_file.read()
|
| 543 |
+
|
| 544 |
+
# ํ์ผ๋ช
์ฒ๋ฆฌ - URL ์ธ์ฝ๋ฉ์ผ๋ก ํ๊ธ ๋ฑ ํน์ ๋ฌธ์ ์ฒ๋ฆฌ
|
| 545 |
+
import urllib.parse
|
| 546 |
+
filename = pdf_path.name
|
| 547 |
+
encoded_filename = urllib.parse.quote(filename)
|
| 548 |
+
|
| 549 |
+
# ์๋ต ํค๋ ์ค์ - RFC 6266 ํ์ค ์ฌ์ฉ
|
| 550 |
+
headers = {
|
| 551 |
+
"Content-Type": "application/pdf",
|
| 552 |
+
"Content-Disposition": f"inline; filename=\"{encoded_filename}\"; filename*=UTF-8''{encoded_filename}"
|
| 553 |
+
}
|
| 554 |
+
|
| 555 |
+
# ํ์ผ ์ฝํ
์ธ ์ง์ ๋ฐํ (dict๊ฐ ์๋ Response ๊ฐ์ฒด)
|
| 556 |
+
from fastapi.responses import Response
|
| 557 |
+
return Response(content=content, media_type="application/pdf")
|
| 558 |
+
except Exception as e:
|
| 559 |
+
import traceback
|
| 560 |
+
error_details = traceback.format_exc()
|
| 561 |
+
print(f"PDF ์ฝํ
์ธ ๋ก๋ ์ค๋ฅ: {str(e)}\n{error_details}")
|
| 562 |
+
|
| 563 |
+
# ์ค๋ฅ ์๋ต ๋ฐํ (JSON ํ์)
|
| 564 |
+
from fastapi.responses import JSONResponse
|
| 565 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 566 |
+
|
| 567 |
+
@app.get("/", response_class=HTMLResponse)
|
| 568 |
+
async def root():
|
| 569 |
+
return HTML
|
| 570 |
+
|
| 571 |
+
if __name__ == "__main__":
|
| 572 |
+
uvicorn.run("app:app", host="0.0.0.0", port=int(os.getenv("PORT", 7860)))
|