Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,14 @@
|
|
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()
|
@@ -12,6 +19,14 @@ 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 = []
|
@@ -27,546 +42,1320 @@ def generate_pdf_projects():
|
|
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 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
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 |
-
|
|
|
321 |
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
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 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
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 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
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 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
$id('homeBtn').style.boxShadow = '';
|
491 |
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
|
|
|
|
|
|
|
|
496 |
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
|
503 |
# API ์๋ํฌ์ธํธ: PDF ํ๋ก์ ํธ ๋ชฉ๋ก
|
504 |
@app.get("/api/pdf-projects")
|
505 |
-
async def
|
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 |
-
|
|
|
513 |
|
514 |
-
#
|
515 |
-
|
|
|
|
|
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 |
-
|
533 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
534 |
try:
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
539 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
540 |
# ํ์ผ ์ฝ๊ธฐ
|
541 |
with open(path, "rb") as pdf_file:
|
542 |
content = pdf_file.read()
|
543 |
|
544 |
-
# ํ์ผ๋ช
์ฒ๋ฆฌ
|
545 |
import urllib.parse
|
546 |
-
filename =
|
547 |
encoded_filename = urllib.parse.quote(filename)
|
548 |
|
549 |
-
#
|
|
|
|
|
|
|
550 |
headers = {
|
551 |
"Content-Type": "application/pdf",
|
552 |
"Content-Disposition": f"inline; filename=\"{encoded_filename}\"; filename*=UTF-8''{encoded_filename}"
|
553 |
}
|
554 |
|
555 |
-
|
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 |
-
|
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
|
570 |
|
571 |
if __name__ == "__main__":
|
572 |
uvicorn.run("app:app", host="0.0.0.0", port=int(os.getenv("PORT", 7860)))
|
|
|
1 |
+
from fastapi import FastAPI, BackgroundTasks
|
2 |
+
from fastapi.responses import HTMLResponse, JSONResponse, Response
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
+
import pathlib, os, uvicorn, base64, json
|
5 |
+
from typing import Dict, List, Any
|
6 |
+
import asyncio
|
7 |
+
import logging
|
8 |
+
|
9 |
+
# ๋ก๊น
์ค์
|
10 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
|
13 |
BASE = pathlib.Path(__file__).parent
|
14 |
app = FastAPI()
|
|
|
19 |
if not PDF_DIR.exists():
|
20 |
PDF_DIR.mkdir(parents=True)
|
21 |
|
22 |
+
# ์บ์ ๋๋ ํ ๋ฆฌ ์ค์
|
23 |
+
CACHE_DIR = BASE / "cache"
|
24 |
+
if not CACHE_DIR.exists():
|
25 |
+
CACHE_DIR.mkdir(parents=True)
|
26 |
+
|
27 |
+
# ์ ์ญ ์บ์ ๊ฐ์ฒด
|
28 |
+
pdf_cache: Dict[str, Dict[str, Any]] = {}
|
29 |
+
|
30 |
# PDF ํ์ผ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
|
31 |
def get_pdf_files():
|
32 |
pdf_files = []
|
|
|
42 |
for pdf_file in pdf_files:
|
43 |
projects_data.append({
|
44 |
"path": str(pdf_file),
|
45 |
+
"name": pdf_file.stem,
|
46 |
+
"cached": pdf_file.stem in pdf_cache
|
47 |
})
|
48 |
|
49 |
return projects_data
|
50 |
|
51 |
+
# ์บ์ ํ์ผ ๊ฒฝ๋ก ์์ฑ
|
52 |
+
def get_cache_path(pdf_name: str):
|
53 |
+
return CACHE_DIR / f"{pdf_name}_cache.json"
|
54 |
+
|
55 |
+
# PDF ํ์ด์ง ์บ์ฑ ํจ์
|
56 |
+
async def cache_pdf(pdf_path: str):
|
57 |
+
try:
|
58 |
+
import fitz # PyMuPDF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
pdf_file = pathlib.Path(pdf_path)
|
61 |
+
pdf_name = pdf_file.stem
|
62 |
|
63 |
+
# ์ด๋ฏธ ์บ์ฑ ์ค์ด๊ฑฐ๋ ์บ์ฑ ์๋ฃ๋ PDF๋ ๊ฑด๋๋ฐ๊ธฐ
|
64 |
+
if pdf_name in pdf_cache and pdf_cache[pdf_name].get("status") in ["processing", "completed"]:
|
65 |
+
logger.info(f"PDF {pdf_name} ์ด๋ฏธ ์บ์ฑ ์๋ฃ ๋๋ ์งํ ์ค")
|
66 |
+
return
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# ์บ์ ์ํ ์
๋ฐ์ดํธ
|
69 |
+
pdf_cache[pdf_name] = {"status": "processing", "progress": 0, "pages": []}
|
70 |
+
|
71 |
+
# ์บ์ ํ์ผ์ด ์ด๋ฏธ ์กด์ฌํ๋์ง ํ์ธ
|
72 |
+
cache_path = get_cache_path(pdf_name)
|
73 |
+
if cache_path.exists():
|
74 |
+
try:
|
75 |
+
with open(cache_path, "r") as cache_file:
|
76 |
+
cached_data = json.load(cache_file)
|
77 |
+
pdf_cache[pdf_name] = cached_data
|
78 |
+
pdf_cache[pdf_name]["status"] = "completed"
|
79 |
+
logger.info(f"์บ์ ํ์ผ์์ {pdf_name} ๋ก๋ ์๋ฃ")
|
80 |
+
return
|
81 |
+
except Exception as e:
|
82 |
+
logger.error(f"์บ์ ํ์ผ ๋ก๋ ์คํจ: {e}")
|
83 |
+
# ๊ณ์ ์งํํ์ฌ ์๋ก ์บ์ฑ
|
84 |
+
|
85 |
+
# PDF ํ์ผ ์ด๊ธฐ
|
86 |
+
doc = fitz.open(pdf_path)
|
87 |
+
total_pages = doc.page_count
|
88 |
+
|
89 |
+
# ๊ฐ ํ์ด์ง๋ฅผ ์ด๋ฏธ์ง๋ก ๋ณํํ๊ณ ์บ์ฑ
|
90 |
+
pages = []
|
91 |
+
for page_num in range(total_pages):
|
92 |
+
# ์งํ ์ํ ์
๋ฐ์ดํธ
|
93 |
+
progress = round((page_num + 1) / total_pages * 100)
|
94 |
+
pdf_cache[pdf_name]["progress"] = progress
|
95 |
+
|
96 |
+
# ํ์ด์ง ๋ ๋๋ง
|
97 |
+
page = doc[page_num]
|
98 |
+
pix = page.get_pixmap() # ๊ธฐ๋ณธ ํด์๋๋ก ๋ ๋๋ง
|
99 |
+
img_data = pix.tobytes("png")
|
100 |
+
|
101 |
+
# Base64 ์ธ์ฝ๋ฉ
|
102 |
+
b64_img = base64.b64encode(img_data).decode('utf-8')
|
103 |
+
img_src = f"data:image/png;base64,{b64_img}"
|
104 |
+
|
105 |
+
# ์ธ๋ค์ผ์ฉ ์์ ์ด๋ฏธ์ง (์ฒซ ํ์ด์ง๋ง)
|
106 |
+
if page_num == 0:
|
107 |
+
pix_thumb = page.get_pixmap(matrix=fitz.Matrix(0.3, 0.3)) # ๋ ์์ ์ธ๋ค์ผ
|
108 |
+
thumb_data = pix_thumb.tobytes("png")
|
109 |
+
b64_thumb = base64.b64encode(thumb_data).decode('utf-8')
|
110 |
+
thumb_src = f"data:image/png;base64,{b64_thumb}"
|
111 |
+
else:
|
112 |
+
thumb_src = "" # ์ฒซ ํ์ด์ง ์ธ์๋ ์ธ๋ค์ผ ์๋ต
|
113 |
+
|
114 |
+
# ํ์ด์ง ์ ๋ณด ์ ์ฅ
|
115 |
+
pages.append({
|
116 |
+
"src": img_src,
|
117 |
+
"thumb": thumb_src if page_num == 0 else ""
|
118 |
+
})
|
119 |
+
|
120 |
+
# ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ๋ฅผ ์ํด ์ค๊ฐ ์ ์ฅ (20ํ์ด์ง๋ง๋ค)
|
121 |
+
if (page_num + 1) % 20 == 0 or page_num == total_pages - 1:
|
122 |
+
pdf_cache[pdf_name]["pages"] = pages
|
123 |
+
# ์บ์ ํ์ผ ์ ์ฅ (์งํ ์ค)
|
124 |
+
try:
|
125 |
+
with open(cache_path, "w") as cache_file:
|
126 |
+
json.dump({
|
127 |
+
"status": "processing",
|
128 |
+
"progress": progress,
|
129 |
+
"pages": pages,
|
130 |
+
"total_pages": total_pages
|
131 |
+
}, cache_file)
|
132 |
+
except Exception as e:
|
133 |
+
logger.error(f"์ค๊ฐ ์บ์ ์ ์ฅ ์คํจ: {e}")
|
134 |
+
|
135 |
+
# ์บ์ฑ ์๋ฃ
|
136 |
+
pdf_cache[pdf_name] = {
|
137 |
+
"status": "completed",
|
138 |
+
"progress": 100,
|
139 |
+
"pages": pages,
|
140 |
+
"total_pages": total_pages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
}
|
142 |
+
|
143 |
+
# ์ต์ข
์บ์ ํ์ผ ์ ์ฅ
|
144 |
+
try:
|
145 |
+
with open(cache_path, "w") as cache_file:
|
146 |
+
json.dump(pdf_cache[pdf_name], cache_file)
|
147 |
+
logger.info(f"PDF {pdf_name} ์บ์ฑ ์๋ฃ, {total_pages}ํ์ด์ง")
|
148 |
+
except Exception as e:
|
149 |
+
logger.error(f"์ต์ข
์บ์ ์ ์ฅ ์คํจ: {e}")
|
150 |
+
|
151 |
+
except Exception as e:
|
152 |
+
import traceback
|
153 |
+
logger.error(f"PDF ์บ์ฑ ์ค๋ฅ: {str(e)}\n{traceback.format_exc()}")
|
154 |
+
if pdf_name in pdf_cache:
|
155 |
+
pdf_cache[pdf_name]["status"] = "error"
|
156 |
+
pdf_cache[pdf_name]["error"] = str(e)
|
157 |
+
|
158 |
+
# ์์ ์ ๋ชจ๋ PDF ํ์ผ ์บ์ฑ
|
159 |
+
async def init_cache_all_pdfs():
|
160 |
+
logger.info("PDF ์บ์ฑ ์์
์์")
|
161 |
+
pdf_files = get_pdf_files()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
+
# ์ด๋ฏธ ์บ์๋ PDF ํ์ผ ๋ก๋
|
164 |
+
for cache_file in CACHE_DIR.glob("*_cache.json"):
|
165 |
+
try:
|
166 |
+
pdf_name = cache_file.stem.replace("_cache", "")
|
167 |
+
with open(cache_file, "r") as f:
|
168 |
+
pdf_cache[pdf_name] = json.load(f)
|
169 |
+
pdf_cache[pdf_name]["status"] = "completed"
|
170 |
+
logger.info(f"๊ธฐ์กด ์บ์ ๋ก๋: {pdf_name}")
|
171 |
+
except Exception as e:
|
172 |
+
logger.error(f"์บ์ ํ์ผ ๋ก๋ ์ค๋ฅ: {str(e)}")
|
|
|
173 |
|
174 |
+
# ์บ์ฑ๋์ง ์์ PDF ํ์ผ ์ฒ๋ฆฌ
|
175 |
+
for pdf_file in pdf_files:
|
176 |
+
pdf_name = pdf_file.stem
|
177 |
+
if pdf_name not in pdf_cache or pdf_cache[pdf_name].get("status") != "completed":
|
178 |
+
try:
|
179 |
+
await cache_pdf(str(pdf_file))
|
180 |
+
except Exception as e:
|
181 |
+
logger.error(f"PDF {pdf_name} ์บ์ฑ ์ค๋ฅ: {str(e)}")
|
182 |
|
183 |
+
# ๋ฐฑ๊ทธ๋ผ์ด๋ ์์
์์ ํจ์
|
184 |
+
@app.on_event("startup")
|
185 |
+
async def startup_event():
|
186 |
+
# ๋ฐฑ๊ทธ๋ผ์ด๋ ํ์คํฌ๋ก ์บ์ฑ ์คํ
|
187 |
+
asyncio.create_task(init_cache_all_pdfs())
|
188 |
|
189 |
# API ์๋ํฌ์ธํธ: PDF ํ๋ก์ ํธ ๋ชฉ๋ก
|
190 |
@app.get("/api/pdf-projects")
|
191 |
+
async def get_pdf_projects_api():
|
192 |
return generate_pdf_projects()
|
193 |
|
194 |
+
# API ์๋ํฌ์ธํธ: PDF ์ธ๋ค์ผ ์ ๊ณต
|
195 |
@app.get("/api/pdf-thumbnail")
|
196 |
async def get_pdf_thumbnail(path: str):
|
197 |
try:
|
198 |
+
pdf_file = pathlib.Path(path)
|
199 |
+
pdf_name = pdf_file.stem
|
200 |
|
201 |
+
# ์บ์์์ ์ธ๋ค์ผ ๊ฐ์ ธ์ค๊ธฐ
|
202 |
+
if pdf_name in pdf_cache and pdf_cache[pdf_name].get("status") == "completed":
|
203 |
+
if pdf_cache[pdf_name]["pages"] and pdf_cache[pdf_name]["pages"][0].get("thumb"):
|
204 |
+
return {"thumbnail": pdf_cache[pdf_name]["pages"][0]["thumb"]}
|
205 |
|
206 |
+
# ์บ์์ ์์ผ๋ฉด ์์ฑ
|
207 |
+
import fitz
|
208 |
+
doc = fitz.open(path)
|
209 |
if doc.page_count > 0:
|
210 |
page = doc[0]
|
|
|
211 |
pix = page.get_pixmap(matrix=fitz.Matrix(0.5, 0.5))
|
212 |
img_data = pix.tobytes("png")
|
|
|
|
|
213 |
b64_img = base64.b64encode(img_data).decode('utf-8')
|
214 |
+
|
215 |
+
# ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์บ์ฑ ์์
|
216 |
+
asyncio.create_task(cache_pdf(path))
|
217 |
+
|
218 |
return {"thumbnail": f"data:image/png;base64,{b64_img}"}
|
219 |
|
220 |
return {"thumbnail": None}
|
221 |
except Exception as e:
|
222 |
+
logger.error(f"์ธ๋ค์ผ ์์ฑ ์ค๋ฅ: {str(e)}")
|
223 |
return {"error": str(e), "thumbnail": None}
|
224 |
|
225 |
+
# API ์๋ํฌ์ธํธ: ์บ์ ์ํ ํ์ธ
|
226 |
+
@app.get("/api/cache-status")
|
227 |
+
async def get_cache_status(path: str = None):
|
228 |
+
if path:
|
229 |
+
pdf_file = pathlib.Path(path)
|
230 |
+
pdf_name = pdf_file.stem
|
231 |
+
if pdf_name in pdf_cache:
|
232 |
+
return pdf_cache[pdf_name]
|
233 |
+
return {"status": "not_cached"}
|
234 |
+
else:
|
235 |
+
return {name: {"status": info["status"], "progress": info.get("progress", 0)}
|
236 |
+
for name, info in pdf_cache.items()}
|
237 |
+
|
238 |
+
# API ์๋ํฌ์ธํธ: ์บ์๋ PDF ์ฝํ
์ธ ์ ๊ณต
|
239 |
+
@app.get("/api/cached-pdf")
|
240 |
+
async def get_cached_pdf(path: str, background_tasks: BackgroundTasks):
|
241 |
try:
|
242 |
+
pdf_file = pathlib.Path(path)
|
243 |
+
pdf_name = pdf_file.stem
|
244 |
+
|
245 |
+
# ์บ์ ํ์ธ
|
246 |
+
if pdf_name in pdf_cache and pdf_cache[pdf_name].get("status") == "completed":
|
247 |
+
return pdf_cache[pdf_name]
|
248 |
+
|
249 |
+
# ์บ์๊ฐ ์งํ์ค์ธ ๊ฒฝ์ฐ
|
250 |
+
if pdf_name in pdf_cache and pdf_cache[pdf_name].get("status") == "processing":
|
251 |
+
return {"status": "processing", "progress": pdf_cache[pdf_name].get("progress", 0)}
|
252 |
+
|
253 |
+
# ์บ์๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์บ์ฑ ์์
|
254 |
+
background_tasks.add_task(cache_pdf, path)
|
255 |
+
return {"status": "started", "progress": 0}
|
256 |
+
|
257 |
+
except Exception as e:
|
258 |
+
logger.error(f"์บ์๋ PDF ์ ๊ณต ์ค๋ฅ: {str(e)}")
|
259 |
+
return {"error": str(e), "status": "error"}
|
260 |
|
261 |
+
# API ์๋ํฌ์ธํธ: PDF ์๋ณธ ์ฝํ
์ธ ์ ๊ณต(์บ์๊ฐ ์๋ ๊ฒฝ์ฐ)
|
262 |
+
@app.get("/api/pdf-content")
|
263 |
+
async def get_pdf_content(path: str, background_tasks: BackgroundTasks):
|
264 |
+
try:
|
265 |
+
# ์บ์ฑ ์ํ ํ์ธ
|
266 |
+
pdf_file = pathlib.Path(path)
|
267 |
+
if not pdf_file.exists():
|
268 |
+
return JSONResponse(content={"error": f"ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค: {path}"}, status_code=404)
|
269 |
+
|
270 |
+
pdf_name = pdf_file.stem
|
271 |
+
|
272 |
+
# ์บ์๋ ๊ฒฝ์ฐ ๋ฆฌ๋ค์ด๋ ํธ
|
273 |
+
if pdf_name in pdf_cache and pdf_cache[pdf_name].get("status") == "completed":
|
274 |
+
return JSONResponse(content={"redirect": f"/api/cached-pdf?path={path}"})
|
275 |
+
|
276 |
# ํ์ผ ์ฝ๊ธฐ
|
277 |
with open(path, "rb") as pdf_file:
|
278 |
content = pdf_file.read()
|
279 |
|
280 |
+
# ํ์ผ๋ช
์ฒ๋ฆฌ
|
281 |
import urllib.parse
|
282 |
+
filename = pdf_file.name
|
283 |
encoded_filename = urllib.parse.quote(filename)
|
284 |
|
285 |
+
# ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์บ์ฑ ์์
|
286 |
+
background_tasks.add_task(cache_pdf, path)
|
287 |
+
|
288 |
+
# ์๋ต ํค๋ ์ค์
|
289 |
headers = {
|
290 |
"Content-Type": "application/pdf",
|
291 |
"Content-Disposition": f"inline; filename=\"{encoded_filename}\"; filename*=UTF-8''{encoded_filename}"
|
292 |
}
|
293 |
|
294 |
+
return Response(content=content, media_type="application/pdf", headers=headers)
|
|
|
|
|
295 |
except Exception as e:
|
296 |
import traceback
|
297 |
error_details = traceback.format_exc()
|
298 |
+
logger.error(f"PDF ์ฝํ
์ธ ๋ก๋ ์ค๋ฅ: {str(e)}\n{error_details}")
|
|
|
|
|
|
|
299 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
300 |
|
301 |
+
# HTML ํ์ผ ์ฝ๊ธฐ ํจ์
|
302 |
+
def get_html_content():
|
303 |
+
html_path = BASE / "flipbook_template.html"
|
304 |
+
if html_path.exists():
|
305 |
+
with open(html_path, "r", encoding="utf-8") as f:
|
306 |
+
return f.read()
|
307 |
+
return HTML # ๊ธฐ๋ณธ HTML ์ฌ์ฉ
|
308 |
+
|
309 |
+
# HTML ๋ฌธ์์ด (์ด์ ์ ์
๋ฐ์ดํธํ HTML ๋ด์ฉ)
|
310 |
+
HTML = """
|
311 |
+
<!doctype html>
|
312 |
+
<html lang="ko">
|
313 |
+
<head>
|
314 |
+
<meta charset="utf-8">
|
315 |
+
<title>FlipBook Space</title>
|
316 |
+
<link rel="stylesheet" href="/static/flipbook.css">
|
317 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
318 |
+
<script src="/static/three.js"></script>
|
319 |
+
<script src="/static/iscroll.js"></script>
|
320 |
+
<script src="/static/mark.js"></script>
|
321 |
+
<script src="/static/mod3d.js"></script>
|
322 |
+
<script src="/static/pdf.js"></script>
|
323 |
+
<script src="/static/flipbook.js"></script>
|
324 |
+
<script src="/static/flipbook.book3.js"></script>
|
325 |
+
<script src="/static/flipbook.scroll.js"></script>
|
326 |
+
<script src="/static/flipbook.swipe.js"></script>
|
327 |
+
<script src="/static/flipbook.webgl.js"></script>
|
328 |
+
<style>
|
329 |
+
/* ์ ์ฒด ์ฌ์ดํธ ํ์คํ
ํค ํ
๋ง */
|
330 |
+
:root {
|
331 |
+
--primary-color: #a5d8ff; /* ํ์คํ
๋ธ๋ฃจ */
|
332 |
+
--secondary-color: #ffd6e0; /* ํ์คํ
ํํฌ */
|
333 |
+
--tertiary-color: #c3fae8; /* ํ์คํ
๋ฏผํธ */
|
334 |
+
--accent-color: #d0bfff; /* ํ์คํ
ํผํ */
|
335 |
+
--bg-color: #f8f9fa; /* ๋ฐ์ ๋ฐฐ๊ฒฝ */
|
336 |
+
--text-color: #495057; /* ๋ถ๋๋ฌ์ด ์ด๋์ด ์ */
|
337 |
+
--card-bg: #ffffff; /* ์นด๋ ๋ฐฐ๊ฒฝ์ */
|
338 |
+
--shadow-sm: 0 2px 8px rgba(0,0,0,0.05);
|
339 |
+
--shadow-md: 0 4px 12px rgba(0,0,0,0.08);
|
340 |
+
--shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
|
341 |
+
--radius-sm: 8px;
|
342 |
+
--radius-md: 12px;
|
343 |
+
--radius-lg: 16px;
|
344 |
+
--transition: all 0.3s ease;
|
345 |
+
}
|
346 |
+
|
347 |
+
body {
|
348 |
+
margin: 0;
|
349 |
+
background: var(--bg-color);
|
350 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
351 |
+
color: var(--text-color);
|
352 |
+
background-image: linear-gradient(120deg, var(--tertiary-color) 0%, var(--bg-color) 100%);
|
353 |
+
background-attachment: fixed;
|
354 |
+
}
|
355 |
+
|
356 |
+
header {
|
357 |
+
max-width: 1200px;
|
358 |
+
margin: 0 auto;
|
359 |
+
padding: 24px 30px;
|
360 |
+
display: flex;
|
361 |
+
align-items: center;
|
362 |
+
background: rgba(255, 255, 255, 0.8);
|
363 |
+
backdrop-filter: blur(8px);
|
364 |
+
border-radius: 0 0 var(--radius-md) var(--radius-md);
|
365 |
+
box-shadow: var(--shadow-sm);
|
366 |
+
position: relative;
|
367 |
+
z-index: 10;
|
368 |
+
}
|
369 |
+
|
370 |
+
#homeBtn {
|
371 |
+
display: none;
|
372 |
+
width: 48px;
|
373 |
+
height: 48px;
|
374 |
+
border: none;
|
375 |
+
border-radius: 50%;
|
376 |
+
cursor: pointer;
|
377 |
+
background: var(--primary-color);
|
378 |
+
color: white;
|
379 |
+
font-size: 22px;
|
380 |
+
margin-right: 16px;
|
381 |
+
box-shadow: var(--shadow-sm);
|
382 |
+
transition: var(--transition);
|
383 |
+
}
|
384 |
+
|
385 |
+
#homeBtn:hover {
|
386 |
+
background: #8bc5f8;
|
387 |
+
transform: scale(1.05);
|
388 |
+
box-shadow: var(--shadow-md);
|
389 |
+
}
|
390 |
+
|
391 |
+
h2 {
|
392 |
+
margin: 0;
|
393 |
+
font-size: 1.75rem;
|
394 |
+
font-weight: 600;
|
395 |
+
background: linear-gradient(120deg, #667eea 0%, #764ba2 100%);
|
396 |
+
-webkit-background-clip: text;
|
397 |
+
background-clip: text;
|
398 |
+
color: transparent;
|
399 |
+
letter-spacing: 0.5px;
|
400 |
+
}
|
401 |
+
|
402 |
+
#home, #viewerPage {
|
403 |
+
max-width: 1200px;
|
404 |
+
margin: 0 auto;
|
405 |
+
padding: 24px 30px 60px;
|
406 |
+
position: relative;
|
407 |
+
}
|
408 |
+
|
409 |
+
/* ์
๋ก๋ ๋ฒํผ ์คํ์ผ */
|
410 |
+
.upload-container {
|
411 |
+
display: flex;
|
412 |
+
margin-bottom: 30px;
|
413 |
+
justify-content: center;
|
414 |
+
}
|
415 |
+
|
416 |
+
button.upload {
|
417 |
+
all: unset;
|
418 |
+
cursor: pointer;
|
419 |
+
padding: 12px 20px;
|
420 |
+
border-radius: var(--radius-md);
|
421 |
+
background: white;
|
422 |
+
margin: 0 10px;
|
423 |
+
font-weight: 500;
|
424 |
+
display: flex;
|
425 |
+
align-items: center;
|
426 |
+
box-shadow: var(--shadow-sm);
|
427 |
+
transition: var(--transition);
|
428 |
+
position: relative;
|
429 |
+
overflow: hidden;
|
430 |
+
}
|
431 |
+
|
432 |
+
button.upload::before {
|
433 |
+
content: '';
|
434 |
+
position: absolute;
|
435 |
+
top: 0;
|
436 |
+
left: 0;
|
437 |
+
width: 100%;
|
438 |
+
height: 100%;
|
439 |
+
background: linear-gradient(120deg, var(--primary-color) 0%, var(--secondary-color) 100%);
|
440 |
+
opacity: 0.08;
|
441 |
+
z-index: -1;
|
442 |
+
}
|
443 |
+
|
444 |
+
button.upload:hover {
|
445 |
+
transform: translateY(-3px);
|
446 |
+
box-shadow: var(--shadow-md);
|
447 |
+
}
|
448 |
+
|
449 |
+
button.upload:hover::before {
|
450 |
+
opacity: 0.15;
|
451 |
+
}
|
452 |
+
|
453 |
+
button.upload i {
|
454 |
+
margin-right: 8px;
|
455 |
+
font-size: 20px;
|
456 |
+
}
|
457 |
+
|
458 |
+
/* ๊ทธ๋ฆฌ๋ ๋ฐ ์นด๋ ์คํ์ผ */
|
459 |
+
.grid {
|
460 |
+
display: grid;
|
461 |
+
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
462 |
+
gap: 24px;
|
463 |
+
margin-top: 36px;
|
464 |
+
}
|
465 |
+
|
466 |
+
.card {
|
467 |
+
background: var(--card-bg);
|
468 |
+
border-radius: var(--radius-md);
|
469 |
+
cursor: pointer;
|
470 |
+
box-shadow: var(--shadow-sm);
|
471 |
+
width: 100%;
|
472 |
+
height: 280px;
|
473 |
+
position: relative;
|
474 |
+
display: flex;
|
475 |
+
flex-direction: column;
|
476 |
+
align-items: center;
|
477 |
+
justify-content: center;
|
478 |
+
transition: var(--transition);
|
479 |
+
overflow: hidden;
|
480 |
+
}
|
481 |
+
|
482 |
+
.card::before {
|
483 |
+
content: '';
|
484 |
+
position: absolute;
|
485 |
+
top: 0;
|
486 |
+
left: 0;
|
487 |
+
width: 100%;
|
488 |
+
height: 100%;
|
489 |
+
background: linear-gradient(135deg, var(--secondary-color) 0%, var(--primary-color) 100%);
|
490 |
+
opacity: 0.06;
|
491 |
+
z-index: 1;
|
492 |
+
}
|
493 |
+
|
494 |
+
.card::after {
|
495 |
+
content: '';
|
496 |
+
position: absolute;
|
497 |
+
top: 0;
|
498 |
+
left: 0;
|
499 |
+
width: 100%;
|
500 |
+
height: 30%;
|
501 |
+
background: linear-gradient(to bottom, rgba(255,255,255,0.8) 0%, rgba(255,255,255,0) 100%);
|
502 |
+
z-index: 2;
|
503 |
+
}
|
504 |
+
|
505 |
+
.card img {
|
506 |
+
width: 65%;
|
507 |
+
height: auto;
|
508 |
+
object-fit: contain;
|
509 |
+
position: absolute;
|
510 |
+
top: 50%;
|
511 |
+
left: 50%;
|
512 |
+
transform: translate(-50%, -65%);
|
513 |
+
border: 1px solid rgba(0,0,0,0.05);
|
514 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.08);
|
515 |
+
z-index: 3;
|
516 |
+
transition: var(--transition);
|
517 |
+
}
|
518 |
+
|
519 |
+
.card:hover {
|
520 |
+
transform: translateY(-5px);
|
521 |
+
box-shadow: var(--shadow-md);
|
522 |
+
}
|
523 |
+
|
524 |
+
.card:hover img {
|
525 |
+
transform: translate(-50%, -65%) scale(1.03);
|
526 |
+
box-shadow: 0 8px 20px rgba(0,0,0,0.12);
|
527 |
+
}
|
528 |
+
|
529 |
+
.card p {
|
530 |
+
position: absolute;
|
531 |
+
bottom: 20px;
|
532 |
+
left: 50%;
|
533 |
+
transform: translateX(-50%);
|
534 |
+
background: rgba(255, 255, 255, 0.9);
|
535 |
+
padding: 8px 16px;
|
536 |
+
border-radius: 30px;
|
537 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
|
538 |
+
width: 80%;
|
539 |
+
text-align: center;
|
540 |
+
white-space: nowrap;
|
541 |
+
overflow: hidden;
|
542 |
+
text-overflow: ellipsis;
|
543 |
+
font-size: 14px;
|
544 |
+
font-weight: 500;
|
545 |
+
color: var(--text-color);
|
546 |
+
z-index: 4;
|
547 |
+
transition: var(--transition);
|
548 |
+
}
|
549 |
+
|
550 |
+
.card:hover p {
|
551 |
+
background: rgba(255, 255, 255, 0.95);
|
552 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.08);
|
553 |
+
}
|
554 |
+
|
555 |
+
/* ์บ์ ์ํ ๋ฑ์ง */
|
556 |
+
.cached-status {
|
557 |
+
position: absolute;
|
558 |
+
top: 10px;
|
559 |
+
right: 10px;
|
560 |
+
background: var(--accent-color);
|
561 |
+
color: white;
|
562 |
+
font-size: 11px;
|
563 |
+
padding: 3px 8px;
|
564 |
+
border-radius: 12px;
|
565 |
+
z-index: 5;
|
566 |
+
box-shadow: var(--shadow-sm);
|
567 |
+
}
|
568 |
+
|
569 |
+
/* ๋ทฐ์ด ์คํ์ผ */
|
570 |
+
#viewer {
|
571 |
+
width: 90%;
|
572 |
+
height: 90vh;
|
573 |
+
max-width: 90%;
|
574 |
+
margin: 0;
|
575 |
+
background: var(--card-bg);
|
576 |
+
border: none;
|
577 |
+
border-radius: var(--radius-lg);
|
578 |
+
position: fixed;
|
579 |
+
top: 50%;
|
580 |
+
left: 50%;
|
581 |
+
transform: translate(-50%, -50%);
|
582 |
+
z-index: 1000;
|
583 |
+
box-shadow: var(--shadow-lg);
|
584 |
+
max-height: calc(90vh - 40px);
|
585 |
+
aspect-ratio: auto;
|
586 |
+
object-fit: contain;
|
587 |
+
overflow: hidden;
|
588 |
+
}
|
589 |
+
|
590 |
+
/* FlipBook ์ปจํธ๋กค๋ฐ ์คํ์ผ */
|
591 |
+
.flipbook-container .fb3d-menu-bar {
|
592 |
+
z-index: 2000 !important;
|
593 |
+
opacity: 1 !important;
|
594 |
+
bottom: 0 !important;
|
595 |
+
background-color: rgba(255,255,255,0.9) !important;
|
596 |
+
backdrop-filter: blur(10px) !important;
|
597 |
+
border-radius: 0 0 var(--radius-lg) var(--radius-lg) !important;
|
598 |
+
padding: 12px 0 !important;
|
599 |
+
box-shadow: 0 -4px 20px rgba(0,0,0,0.1) !important;
|
600 |
+
}
|
601 |
+
|
602 |
+
.flipbook-container .fb3d-menu-bar > ul > li > img,
|
603 |
+
.flipbook-container .fb3d-menu-bar > ul > li > div {
|
604 |
+
opacity: 1 !important;
|
605 |
+
transform: scale(1.2) !important;
|
606 |
+
filter: drop-shadow(0 2px 3px rgba(0,0,0,0.1)) !important;
|
607 |
+
}
|
608 |
+
|
609 |
+
.flipbook-container .fb3d-menu-bar > ul > li {
|
610 |
+
margin: 0 12px !important;
|
611 |
+
}
|
612 |
+
|
613 |
+
/* ๋ฉ๋ด ํดํ ์คํ์ผ */
|
614 |
+
.flipbook-container .fb3d-menu-bar > ul > li > span {
|
615 |
+
background-color: rgba(0,0,0,0.7) !important;
|
616 |
+
color: white !important;
|
617 |
+
border-radius: var(--radius-sm) !important;
|
618 |
+
padding: 8px 12px !important;
|
619 |
+
font-size: 13px !important;
|
620 |
+
bottom: 55px !important;
|
621 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
|
622 |
+
letter-spacing: 0.3px !important;
|
623 |
+
}
|
624 |
+
|
625 |
+
/* ๋ทฐ์ด ๋ชจ๋์ผ ๋ ๋ฐฐ๊ฒฝ ์ค๋ฒ๋ ์ด */
|
626 |
+
.viewer-mode {
|
627 |
+
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%) !important;
|
628 |
+
}
|
629 |
+
|
630 |
+
/* ๋ทฐ์ด ํ์ด์ง ๋ฐฐ๊ฒฝ */
|
631 |
+
#viewerPage {
|
632 |
+
background: transparent;
|
633 |
+
}
|
634 |
+
|
635 |
+
/* ๋ก๋ฉ ์ ๋๋ฉ์ด์
*/
|
636 |
+
@keyframes spin {
|
637 |
+
0% { transform: rotate(0deg); }
|
638 |
+
100% { transform: rotate(360deg); }
|
639 |
+
}
|
640 |
+
|
641 |
+
.loading-spinner {
|
642 |
+
border: 4px solid rgba(255,255,255,0.3);
|
643 |
+
border-top: 4px solid var(--primary-color);
|
644 |
+
border-radius: 50%;
|
645 |
+
width: 50px;
|
646 |
+
height: 50px;
|
647 |
+
margin: 0 auto;
|
648 |
+
animation: spin 1.5s ease-in-out infinite;
|
649 |
+
}
|
650 |
+
|
651 |
+
.loading-container {
|
652 |
+
position: absolute;
|
653 |
+
top: 50%;
|
654 |
+
left: 50%;
|
655 |
+
transform: translate(-50%, -50%);
|
656 |
+
text-align: center;
|
657 |
+
background: rgba(255, 255, 255, 0.85);
|
658 |
+
backdrop-filter: blur(10px);
|
659 |
+
padding: 30px;
|
660 |
+
border-radius: var(--radius-md);
|
661 |
+
box-shadow: var(--shadow-md);
|
662 |
+
z-index: 9999;
|
663 |
+
}
|
664 |
+
|
665 |
+
.loading-text {
|
666 |
+
margin-top: 20px;
|
667 |
+
font-size: 16px;
|
668 |
+
color: var(--text-color);
|
669 |
+
font-weight: 500;
|
670 |
+
}
|
671 |
+
|
672 |
+
/* ํ์ด์ง ์ ํ ์ ๋๋ฉ์ด์
*/
|
673 |
+
@keyframes fadeIn {
|
674 |
+
from { opacity: 0; }
|
675 |
+
to { opacity: 1; }
|
676 |
+
}
|
677 |
+
|
678 |
+
.fade-in {
|
679 |
+
animation: fadeIn 0.5s ease-out;
|
680 |
+
}
|
681 |
+
|
682 |
+
/* ์ถ๊ฐ ์คํ์ผ */
|
683 |
+
.section-title {
|
684 |
+
font-size: 1.3rem;
|
685 |
+
font-weight: 600;
|
686 |
+
margin: 30px 0 15px;
|
687 |
+
color: var(--text-color);
|
688 |
+
}
|
689 |
+
|
690 |
+
.no-projects {
|
691 |
+
text-align: center;
|
692 |
+
margin: 40px 0;
|
693 |
+
color: var(--text-color);
|
694 |
+
font-size: 16px;
|
695 |
+
}
|
696 |
+
|
697 |
+
/* ํ๋ก๊ทธ๋ ์ค ๋ฐ */
|
698 |
+
.progress-bar-container {
|
699 |
+
width: 100%;
|
700 |
+
height: 6px;
|
701 |
+
background-color: rgba(0,0,0,0.1);
|
702 |
+
border-radius: 3px;
|
703 |
+
margin-top: 15px;
|
704 |
+
overflow: hidden;
|
705 |
+
}
|
706 |
+
|
707 |
+
.progress-bar {
|
708 |
+
height: 100%;
|
709 |
+
background: linear-gradient(to right, var(--primary-color), var(--accent-color));
|
710 |
+
border-radius: 3px;
|
711 |
+
transition: width 0.3s ease;
|
712 |
+
}
|
713 |
+
|
714 |
+
/* ๋ฐ์ํ ๋์์ธ */
|
715 |
+
@media (max-width: 768px) {
|
716 |
+
.grid {
|
717 |
+
grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
|
718 |
+
gap: 16px;
|
719 |
+
}
|
720 |
+
|
721 |
+
.card {
|
722 |
+
height: 240px;
|
723 |
+
}
|
724 |
+
|
725 |
+
header {
|
726 |
+
padding: 20px;
|
727 |
+
}
|
728 |
+
|
729 |
+
h2 {
|
730 |
+
font-size: 1.5rem;
|
731 |
+
}
|
732 |
+
}
|
733 |
+
</style>
|
734 |
+
</head>
|
735 |
+
<body>
|
736 |
+
<header>
|
737 |
+
<button id="homeBtn" title="ํ์ผ๋ก"><i class="fas fa-home"></i></button>
|
738 |
+
<h2>My FlipBook Library</h2>
|
739 |
+
</header>
|
740 |
+
|
741 |
+
<section id="home" class="fade-in">
|
742 |
+
<div class="upload-container">
|
743 |
+
<button class="upload">
|
744 |
+
<i class="fas fa-images"></i> ์ด๋ฏธ์ง ์ถ๊ฐ
|
745 |
+
<input id="imgInput" type="file" accept="image/*" multiple hidden>
|
746 |
+
</button>
|
747 |
+
<button class="upload">
|
748 |
+
<i class="fas fa-file-pdf"></i> PDF ์ถ๊ฐ
|
749 |
+
<input id="pdfInput" type="file" accept="application/pdf" hidden>
|
750 |
+
</button>
|
751 |
+
</div>
|
752 |
+
|
753 |
+
<div class="section-title">๋ด ํ๋ก์ ํธ</div>
|
754 |
+
<div class="grid" id="grid">
|
755 |
+
<!-- ์นด๋๊ฐ ์ฌ๊ธฐ์ ๋์ ์ผ๋ก ์ถ๊ฐ๋ฉ๋๋ค -->
|
756 |
+
</div>
|
757 |
+
<div id="noProjects" class="no-projects" style="display: none;">
|
758 |
+
ํ๋ก์ ํธ๊ฐ ์์ต๋๋ค. ์ด๋ฏธ์ง๋ PDF๋ฅผ ์ถ๊ฐํ์ฌ ์์ํ์ธ์.
|
759 |
+
</div>
|
760 |
+
</section>
|
761 |
+
|
762 |
+
<section id="viewerPage" style="display:none">
|
763 |
+
<div id="viewer"></div>
|
764 |
+
</section>
|
765 |
+
|
766 |
+
<script>
|
767 |
+
let projects=[], fb=null;
|
768 |
+
const grid=$id('grid'), viewer=$id('viewer');
|
769 |
+
pdfjsLib.GlobalWorkerOptions.workerSrc='/static/pdf.worker.js';
|
770 |
+
|
771 |
+
// ์๋ฒ์์ ๋ฏธ๋ฆฌ ๋ก๋๋ PDF ํ๋ก์ ํธ
|
772 |
+
let serverProjects = [];
|
773 |
+
|
774 |
+
/* ๐ ์ค๋์ค unlock โ ๋ด์ฅ Audio ์ ๊ฐ์ MP3 ๊ฒฝ๋ก ์ฌ์ฉ */
|
775 |
+
['click','touchstart'].forEach(evt=>{
|
776 |
+
document.addEventListener(evt,function u(){new Audio('static/turnPage2.mp3')
|
777 |
+
.play().then(a=>a.pause()).catch(()=>{});document.removeEventListener(evt,u,{capture:true});},
|
778 |
+
{once:true,capture:true});
|
779 |
+
});
|
780 |
+
|
781 |
+
/* โโ ์ ํธ โโ */
|
782 |
+
function $id(id){return document.getElementById(id)}
|
783 |
+
|
784 |
+
function addCard(i, thumb, title, isCached = false) {
|
785 |
+
const d = document.createElement('div');
|
786 |
+
d.className = 'card fade-in';
|
787 |
+
d.onclick = () => open(i);
|
788 |
+
|
789 |
+
// ์ ๋ชฉ ์ฒ๋ฆฌ
|
790 |
+
const displayTitle = title ?
|
791 |
+
(title.length > 15 ? title.substring(0, 15) + '...' : title) :
|
792 |
+
'ํ๋ก์ ํธ ' + (i+1);
|
793 |
+
|
794 |
+
// ์บ์ ์ํ ๋ฑ์ง ์ถ๊ฐ
|
795 |
+
const cachedBadge = isCached ?
|
796 |
+
'<div class="cached-status">์บ์๋จ</div>' : '';
|
797 |
+
|
798 |
+
d.innerHTML = `
|
799 |
+
<div class="card-inner">
|
800 |
+
${cachedBadge}
|
801 |
+
<img src="${thumb}" alt="${displayTitle}">
|
802 |
+
<p title="${title || 'ํ๋ก์ ํธ ' + (i+1)}">${displayTitle}</p>
|
803 |
+
</div>
|
804 |
+
`;
|
805 |
+
grid.appendChild(d);
|
806 |
+
|
807 |
+
// ํ๋ก์ ํธ๊ฐ ์์ผ๋ฉด 'ํ๋ก์ ํธ ์์' ๋ฉ์์ง ์จ๊ธฐ๊ธฐ
|
808 |
+
$id('noProjects').style.display = 'none';
|
809 |
+
}
|
810 |
+
|
811 |
+
/* โโ ์ด๋ฏธ์ง ์
๋ก๋ โโ */
|
812 |
+
$id('imgInput').onchange=e=>{
|
813 |
+
const files=[...e.target.files]; if(!files.length) return;
|
814 |
+
|
815 |
+
// ๋ก๋ฉ ํ์ ์ถ๊ฐ
|
816 |
+
showLoading("์ด๋ฏธ์ง ๋ก๋ฉ ์ค...");
|
817 |
+
|
818 |
+
const pages=[],tot=files.length;let done=0;
|
819 |
+
files.forEach((f,i)=>{const r=new FileReader();r.onload=x=>{pages[i]={src:x.target.result,thumb:x.target.result};
|
820 |
+
if(++done===tot) {
|
821 |
+
save(pages, '์ด๋ฏธ์ง ์ปฌ๋ ์
');
|
822 |
+
hideLoading();
|
823 |
+
}
|
824 |
+
};r.readAsDataURL(f);});
|
825 |
+
};
|
826 |
+
|
827 |
+
/* โโ PDF ์
๋ก๋ โโ */
|
828 |
+
$id('pdfInput').onchange=e=>{
|
829 |
+
const file=e.target.files[0]; if(!file) return;
|
830 |
+
|
831 |
+
// ๋ก๋ฉ ํ์ ์ถ๊ฐ
|
832 |
+
showLoading("PDF ๋ก๋ฉ ์ค...");
|
833 |
+
|
834 |
+
const fr=new FileReader();
|
835 |
+
fr.onload=v=>{
|
836 |
+
pdfjsLib.getDocument({data:v.target.result}).promise.then(async pdf=>{
|
837 |
+
const pages=[];
|
838 |
+
|
839 |
+
for(let p=1;p<=pdf.numPages;p++){
|
840 |
+
// ๋ก๋ฉ ์ํ ์
๋ฐ์ดํธ
|
841 |
+
updateLoading(`PDF ํ์ด์ง ๋ก๋ฉ ์ค... (${p}/${pdf.numPages})`);
|
842 |
+
|
843 |
+
const pg=await pdf.getPage(p), vp=pg.getViewport({scale:1});
|
844 |
+
const c=document.createElement('canvas');c.width=vp.width;c.height=vp.height;
|
845 |
+
await pg.render({canvasContext:c.getContext('2d'),viewport:vp}).promise;
|
846 |
+
pages.push({src:c.toDataURL(),thumb:c.toDataURL()});
|
847 |
+
}
|
848 |
+
|
849 |
+
hideLoading();
|
850 |
+
save(pages, file.name.replace('.pdf', ''));
|
851 |
+
}).catch(error => {
|
852 |
+
console.error("PDF ๋ก๋ฉ ์ค๋ฅ:", error);
|
853 |
+
hideLoading();
|
854 |
+
showError("PDF ๋ก๋ฉ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
|
855 |
+
});
|
856 |
+
};fr.readAsArrayBuffer(file);
|
857 |
+
};
|
858 |
+
|
859 |
+
/* โโ ํ๋ก์ ํธ ์ ์ฅ โโ */
|
860 |
+
function save(pages, title, isCached = false){
|
861 |
+
const id=projects.push(pages)-1;
|
862 |
+
addCard(id, pages[0].thumb, title, isCached);
|
863 |
+
}
|
864 |
+
|
865 |
+
/* โโ ์๋ฒ PDF ๋ก๋ ๋ฐ ์บ์ ์ํ ํ์ธ โโ */
|
866 |
+
async function loadServerPDFs() {
|
867 |
+
try {
|
868 |
+
// ๋ก๋ฉ ํ์ ์ถ๊ฐ
|
869 |
+
if (document.querySelectorAll('.card').length === 0) {
|
870 |
+
showLoading("๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ก๋ฉ ์ค...");
|
871 |
+
}
|
872 |
+
|
873 |
+
// ๋จผ์ ์บ์ ์ํ ํ์ธ
|
874 |
+
const cacheStatusRes = await fetch('/api/cache-status');
|
875 |
+
const cacheStatus = await cacheStatusRes.json();
|
876 |
+
|
877 |
+
// PDF ํ๋ก์ ํธ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
|
878 |
+
const response = await fetch('/api/pdf-projects');
|
879 |
+
serverProjects = await response.json();
|
880 |
+
|
881 |
+
if (serverProjects.length === 0) {
|
882 |
+
hideLoading();
|
883 |
+
$id('noProjects').style.display = 'block';
|
884 |
+
return;
|
885 |
+
}
|
886 |
+
|
887 |
+
// ์๋ฒ PDF ๋ก๋ ๋ฐ ์ธ๋ค์ผ ์์ฑ
|
888 |
+
for(let i = 0; i < serverProjects.length; i++) {
|
889 |
+
updateLoading(`PDF ํ๋ก์ ํธ ๋ก๋ฉ ์ค... (${i+1}/${serverProjects.length})`);
|
890 |
+
|
891 |
+
const project = serverProjects[i];
|
892 |
+
const pdfName = project.name;
|
893 |
+
const isCached = cacheStatus[pdfName] && cacheStatus[pdfName].status === "completed";
|
894 |
+
|
895 |
+
// ์ธ๋ค์ผ ๊ฐ์ ธ์ค๊ธฐ
|
896 |
+
const response = await fetch(`/api/pdf-thumbnail?path=${encodeURIComponent(project.path)}`);
|
897 |
+
const data = await response.json();
|
898 |
+
|
899 |
+
if(data.thumbnail) {
|
900 |
+
const pages = [{
|
901 |
+
src: data.thumbnail,
|
902 |
+
thumb: data.thumbnail,
|
903 |
+
path: project.path,
|
904 |
+
cached: isCached
|
905 |
+
}];
|
906 |
+
|
907 |
+
save(pages, project.name, isCached);
|
908 |
+
}
|
909 |
+
}
|
910 |
+
|
911 |
+
hideLoading();
|
912 |
+
|
913 |
+
// ํ๋ก์ ํธ๊ฐ ์์ ๊ฒฝ์ฐ ๋ฉ์์ง ํ์
|
914 |
+
if (document.querySelectorAll('.card').length === 0) {
|
915 |
+
$id('noProjects').style.display = 'block';
|
916 |
+
}
|
917 |
+
} catch(error) {
|
918 |
+
console.error('์๋ฒ PDF ๋ก๋ ์คํจ:', error);
|
919 |
+
hideLoading();
|
920 |
+
showError("๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ก๋ฉ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
|
921 |
+
}
|
922 |
+
}
|
923 |
+
|
924 |
+
/* โโ ์บ์ ์ํ ์ ๊ธฐ์ ์ผ๋ก ํ์ธ โโ */
|
925 |
+
async function checkCacheStatus() {
|
926 |
+
try {
|
927 |
+
const response = await fetch('/api/cache-status');
|
928 |
+
const cacheStatus = await response.json();
|
929 |
+
|
930 |
+
// ํ์ฌ ์นด๋ ์ํ ์
๋ฐ์ดํธ
|
931 |
+
const cards = document.querySelectorAll('.card');
|
932 |
+
|
933 |
+
for(let i = 0; i < cards.length; i++) {
|
934 |
+
if(projects[i] && projects[i][0] && projects[i][0].path) {
|
935 |
+
const pdfPath = projects[i][0].path;
|
936 |
+
const pdfName = pdfPath.split('/').pop().replace('.pdf', '');
|
937 |
+
|
938 |
+
// ์บ์ ์ํ ๋ฑ์ง ์
๋ฐ์ดํธ
|
939 |
+
let badgeEl = cards[i].querySelector('.cached-status');
|
940 |
+
|
941 |
+
if(cacheStatus[pdfName] && cacheStatus[pdfName].status === "completed") {
|
942 |
+
if(!badgeEl) {
|
943 |
+
badgeEl = document.createElement('div');
|
944 |
+
badgeEl.className = 'cached-status';
|
945 |
+
badgeEl.textContent = '์บ์๋จ';
|
946 |
+
cards[i].querySelector('.card-inner')?.appendChild(badgeEl);
|
947 |
+
}
|
948 |
+
projects[i][0].cached = true;
|
949 |
+
} else if(cacheStatus[pdfName] && cacheStatus[pdfName].status === "processing") {
|
950 |
+
if(!badgeEl) {
|
951 |
+
badgeEl = document.createElement('div');
|
952 |
+
badgeEl.className = 'cached-status';
|
953 |
+
cards[i].querySelector('.card-inner')?.appendChild(badgeEl);
|
954 |
+
}
|
955 |
+
badgeEl.textContent = `${cacheStatus[pdfName].progress}%`;
|
956 |
+
badgeEl.style.background = 'var(--secondary-color)';
|
957 |
+
}
|
958 |
+
}
|
959 |
+
}
|
960 |
+
|
961 |
+
} catch(error) {
|
962 |
+
console.error('์บ์ ์ํ ํ์ธ ์ค๋ฅ:', error);
|
963 |
+
}
|
964 |
+
}
|
965 |
+
|
966 |
+
/* โโ ์นด๋ โ FlipBook โโ */
|
967 |
+
async function open(i) {
|
968 |
+
toggle(false);
|
969 |
+
const pages = projects[i];
|
970 |
+
|
971 |
+
// ๊ธฐ์กด FlipBook ์ ๋ฆฌ
|
972 |
+
if(fb) {
|
973 |
+
fb.destroy();
|
974 |
+
viewer.innerHTML = '';
|
975 |
+
}
|
976 |
+
|
977 |
+
// ์๋ฒ PDF ๋๋ ๋ก์ปฌ ํ๋ก์ ํธ ์ฒ๋ฆฌ
|
978 |
+
if(pages[0].path) {
|
979 |
+
const pdfPath = pages[0].path;
|
980 |
+
|
981 |
+
// ์บ์ ์ฌ๋ถ ํ์ธ
|
982 |
+
if(pages[0].cached) {
|
983 |
+
// ์บ์๋ PDF ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
|
984 |
+
showLoading("์บ์๋ PDF ๋ก๋ฉ ์ค...");
|
985 |
+
|
986 |
+
try {
|
987 |
+
const response = await fetch(`/api/cached-pdf?path=${encodeURIComponent(pdfPath)}`);
|
988 |
+
const cachedData = await response.json();
|
989 |
+
|
990 |
+
if(cachedData.status === "completed" && cachedData.pages) {
|
991 |
+
hideLoading();
|
992 |
+
createFlipBook(cachedData.pages);
|
993 |
+
return;
|
994 |
+
}
|
995 |
+
} catch(error) {
|
996 |
+
console.error("์บ์ ๋ฐ์ดํฐ ๋ก๋ ์ค๋ฅ:", error);
|
997 |
+
// ์บ์ ๋ก๋ฉ ์คํจ ์ ์๋ณธ PDF๋ก ๋์ฒด
|
998 |
+
}
|
999 |
+
}
|
1000 |
+
|
1001 |
+
// ์บ์๊ฐ ์๊ฑฐ๋ ๋ก๋ฉ ์คํจ ์ ์๋ฒ PDF ๋ก๋
|
1002 |
+
showLoading("PDF ์ค๋น ์ค...");
|
1003 |
+
|
1004 |
+
try {
|
1005 |
+
const response = await fetch(`/api/pdf-content?path=${encodeURIComponent(pdfPath)}`);
|
1006 |
+
const data = await response.json();
|
1007 |
+
|
1008 |
+
// ์บ์๋ก ๋ฆฌ๋ค์ด๋ ํธ๋ ๊ฒฝ์ฐ
|
1009 |
+
if(data.redirect) {
|
1010 |
+
const redirectRes = await fetch(data.redirect);
|
1011 |
+
const cachedData = await redirectRes.json();
|
1012 |
+
|
1013 |
+
if(cachedData.status === "completed" && cachedData.pages) {
|
1014 |
+
hideLoading();
|
1015 |
+
createFlipBook(cachedData.pages);
|
1016 |
+
return;
|
1017 |
+
} else if(cachedData.status === "processing") {
|
1018 |
+
// ์บ์ฑ ์งํ ์ค์ธ ๊ฒฝ์ฐ ์งํ๋ฅ ํ์
|
1019 |
+
updateLoading(`PDF ์บ์ฑ ์ค: ${cachedData.progress}%`, cachedData.progress);
|
1020 |
+
|
1021 |
+
// ์บ์ฑ ์๋ฃ ํ์ธ์ ๏ฟฝ๏ฟฝ๏ฟฝํ ํด๋ง
|
1022 |
+
await waitForCaching(pdfPath);
|
1023 |
+
return;
|
1024 |
+
}
|
1025 |
+
}
|
1026 |
+
|
1027 |
+
// ์๋ณธ PDF ๋ก๋ (ArrayBuffer ํํ)
|
1028 |
+
const pdfResponse = await fetch(`/api/pdf-content?path=${encodeURIComponent(pdfPath)}`);
|
1029 |
+
const pdfData = await pdfResponse.arrayBuffer();
|
1030 |
+
|
1031 |
+
// PDF ๋ก๋ ๋ฐ ํ์ด์ง ๋ ๋๋ง
|
1032 |
+
const pdf = await pdfjsLib.getDocument({data: pdfData}).promise;
|
1033 |
+
const pdfPages = [];
|
1034 |
+
|
1035 |
+
for(let p = 1; p <= pdf.numPages; p++) {
|
1036 |
+
updateLoading(`ํ์ด์ง ์ค๋น ์ค... (${p}/${pdf.numPages})`);
|
1037 |
+
|
1038 |
+
const pg = await pdf.getPage(p);
|
1039 |
+
const vp = pg.getViewport({scale: 1});
|
1040 |
+
const c = document.createElement('canvas');
|
1041 |
+
c.width = vp.width;
|
1042 |
+
c.height = vp.height;
|
1043 |
+
|
1044 |
+
await pg.render({canvasContext: c.getContext('2d'), viewport: vp}).promise;
|
1045 |
+
pdfPages.push({src: c.toDataURL(), thumb: c.toDataURL()});
|
1046 |
+
}
|
1047 |
+
|
1048 |
+
hideLoading();
|
1049 |
+
createFlipBook(pdfPages);
|
1050 |
+
|
1051 |
+
} catch(error) {
|
1052 |
+
console.error('PDF ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:', error);
|
1053 |
+
hideLoading();
|
1054 |
+
showError("PDF๋ฅผ ๋ก๋ํ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
|
1055 |
+
}
|
1056 |
+
} else {
|
1057 |
+
// ๋ก์ปฌ ์
๋ก๋๋ ํ๋ก์ ํธ ์คํ
|
1058 |
+
createFlipBook(pages);
|
1059 |
+
}
|
1060 |
+
}
|
1061 |
+
|
1062 |
+
/* โโ ์บ์ฑ ์๋ฃ ๋๊ธฐ ํจ์ โโ */
|
1063 |
+
async function waitForCaching(pdfPath) {
|
1064 |
+
let retries = 0;
|
1065 |
+
const maxRetries = 100; // ์ต๋ ํ์ธ ํ์
|
1066 |
+
|
1067 |
+
const checkInterval = setInterval(async () => {
|
1068 |
+
try {
|
1069 |
+
const response = await fetch(`/api/cache-status?path=${encodeURIComponent(pdfPath)}`);
|
1070 |
+
const status = await response.json();
|
1071 |
+
|
1072 |
+
if(status.status === "completed") {
|
1073 |
+
// ์บ์ฑ ์๋ฃ
|
1074 |
+
clearInterval(checkInterval);
|
1075 |
+
hideLoading();
|
1076 |
+
|
1077 |
+
// ์บ์๋ ๋ฐ์ดํฐ๋ก FlipBook ์์ฑ
|
1078 |
+
const cachedRes = await fetch(`/api/cached-pdf?path=${encodeURIComponent(pdfPath)}`);
|
1079 |
+
const cachedData = await cachedRes.json();
|
1080 |
+
createFlipBook(cachedData.pages);
|
1081 |
+
|
1082 |
+
} else if(status.status === "processing") {
|
1083 |
+
// ์งํ ์ํ ์
๋ฐ์ดํธ
|
1084 |
+
updateLoading(`PDF ์บ์ฑ ์ค: ${status.progress}%`, status.progress);
|
1085 |
+
} else if(status.status === "error" || retries >= maxRetries) {
|
1086 |
+
// ์ค๋ฅ ๋๋ ์๊ฐ ์ด๊ณผ
|
1087 |
+
clearInterval(checkInterval);
|
1088 |
+
hideLoading();
|
1089 |
+
showError("PDF ์บ์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๋ค์ ์๋ํด์ฃผ์ธ์.");
|
1090 |
+
}
|
1091 |
+
|
1092 |
+
retries++;
|
1093 |
+
} catch(e) {
|
1094 |
+
console.error("์บ์ ์ํ ํ์ธ ์ค๋ฅ:", e);
|
1095 |
+
retries++;
|
1096 |
+
|
1097 |
+
if(retries >= maxRetries) {
|
1098 |
+
clearInterval(checkInterval);
|
1099 |
+
hideLoading();
|
1100 |
+
showError("PDF ์บ์ฑ ์ํ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.");
|
1101 |
+
}
|
1102 |
+
}
|
1103 |
+
}, 1000); // 1์ด๋ง๋ค ํ์ธ
|
1104 |
+
}
|
1105 |
+
|
1106 |
+
function createFlipBook(pages) {
|
1107 |
+
console.log('FlipBook ์์ฑ ์์. ํ์ด์ง ์:', pages.length);
|
1108 |
+
|
1109 |
+
try {
|
1110 |
+
// ํ๋ฉด ๋น์จ ๊ณ์ฐ
|
1111 |
+
const calculateAspectRatio = () => {
|
1112 |
+
const windowWidth = window.innerWidth;
|
1113 |
+
const windowHeight = window.innerHeight;
|
1114 |
+
const aspectRatio = windowWidth / windowHeight;
|
1115 |
+
|
1116 |
+
// ๋๋น ๋๋ ๋์ด ๊ธฐ์ค์ผ๋ก ์ต๋ 90% ์ ํ
|
1117 |
+
let width, height;
|
1118 |
+
if (aspectRatio > 1) { // ๊ฐ๋ก ํ๋ฉด
|
1119 |
+
height = Math.min(windowHeight * 0.9, windowHeight - 40);
|
1120 |
+
width = height * aspectRatio * 0.8; // ๊ฐ๋ก ํ๋ฉด์์๋ ์ฝ๊ฐ ์ค์
|
1121 |
+
if (width > windowWidth * 0.9) {
|
1122 |
+
width = windowWidth * 0.9;
|
1123 |
+
height = width / (aspectRatio * 0.8);
|
1124 |
+
}
|
1125 |
+
} else { // ์ธ๋ก ํ๋ฉด
|
1126 |
+
width = Math.min(windowWidth * 0.9, windowWidth - 40);
|
1127 |
+
height = width / aspectRatio * 0.9; // ์ธ๋ก ํ๋ฉด์์๋ ์ฝ๊ฐ ๋๋ฆผ
|
1128 |
+
if (height > windowHeight * 0.9) {
|
1129 |
+
height = windowHeight * 0.9;
|
1130 |
+
width = height * aspectRatio * 0.9;
|
1131 |
+
}
|
1132 |
+
}
|
1133 |
+
|
1134 |
+
// ์ต์ ์ฌ์ด์ฆ ๋ฐํ
|
1135 |
+
return {
|
1136 |
+
width: Math.round(width),
|
1137 |
+
height: Math.round(height)
|
1138 |
+
};
|
1139 |
+
};
|
1140 |
+
|
1141 |
+
// ์ด๊ธฐ ํ๋ฉด ๋น์จ ๊ณ์ฐ
|
1142 |
+
const size = calculateAspectRatio();
|
1143 |
+
viewer.style.width = size.width + 'px';
|
1144 |
+
viewer.style.height = size.height + 'px';
|
1145 |
+
|
1146 |
+
fb = new FlipBook(viewer, {
|
1147 |
+
pages: pages,
|
1148 |
+
viewMode: 'webgl',
|
1149 |
+
autoSize: true,
|
1150 |
+
flipDuration: 800,
|
1151 |
+
backgroundColor: '#fff',
|
1152 |
+
/* ๐ ๋ด์ฅ ์ฌ์ด๋ */
|
1153 |
+
sound: true,
|
1154 |
+
assets: {flipMp3: 'static/turnPage2.mp3', hardFlipMp3: 'static/turnPage2.mp3'},
|
1155 |
+
controlsProps: {
|
1156 |
+
enableFullscreen: true,
|
1157 |
+
enableToc: true,
|
1158 |
+
enableDownload: false,
|
1159 |
+
enablePrint: false,
|
1160 |
+
enableZoom: true,
|
1161 |
+
enableShare: false,
|
1162 |
+
enableSearch: true,
|
1163 |
+
enableAutoPlay: true,
|
1164 |
+
enableAnnotation: false,
|
1165 |
+
enableSound: true,
|
1166 |
+
enableLightbox: false,
|
1167 |
+
layout: 10, // ๋ ์ด์์ ์ต์
|
1168 |
+
skin: 'light', // ์คํจ ์คํ์ผ
|
1169 |
+
autoNavigationTime: 3600, // ์๋ ๋๊น ์๊ฐ(์ด)
|
1170 |
+
hideControls: false, // ์ปจํธ๋กค ์จ๊น ๋นํ์ฑํ
|
1171 |
+
paddingTop: 10, // ์๋จ ํจ๋ฉ
|
1172 |
+
paddingLeft: 10, // ์ข์ธก ํจ๋ฉ
|
1173 |
+
paddingRight: 10, // ์ฐ์ธก ํจ๋ฉ
|
1174 |
+
paddingBottom: 10, // ํ๋จ ํจ๋ฉ
|
1175 |
+
pageTextureSize: 1024, // ํ์ด์ง ํ
์ค์ฒ ํฌ๊ธฐ
|
1176 |
+
thumbnails: true, // ์ฌ๋ค์ผ ํ์ฑํ
|
1177 |
+
autoHideControls: false, // ์๋ ์จ๊น ๋นํ์ฑํ
|
1178 |
+
controlsTimeout: 8000 // ์ปจํธ๋กค ํ์ ์๊ฐ ์ฐ์ฅ
|
1179 |
+
}
|
1180 |
+
});
|
1181 |
+
|
1182 |
+
// ํ๋ฉด ํฌ๊ธฐ ๋ณ๊ฒฝ ์ FlipBook ํฌ๊ธฐ ์กฐ์
|
1183 |
+
window.addEventListener('resize', () => {
|
1184 |
+
if (fb) {
|
1185 |
+
const newSize = calculateAspectRatio();
|
1186 |
+
viewer.style.width = newSize.width + 'px';
|
1187 |
+
viewer.style.height = newSize.height + 'px';
|
1188 |
+
fb.resize();
|
1189 |
+
}
|
1190 |
+
});
|
1191 |
+
|
1192 |
+
// FlipBook ์์ฑ ํ ์ปจํธ๋กค๋ฐ ๊ฐ์ ํ์
|
1193 |
+
setTimeout(() => {
|
1194 |
+
try {
|
1195 |
+
// ์ปจํธ๋กค๋ฐ ๊ด๋ จ ์์ ์ฐพ๊ธฐ ๋ฐ ์คํ์ผ ์ ์ฉ
|
1196 |
+
const menuBars = document.querySelectorAll('.flipbook-container .fb3d-menu-bar');
|
1197 |
+
if (menuBars && menuBars.length > 0) {
|
1198 |
+
menuBars.forEach(menuBar => {
|
1199 |
+
menuBar.style.display = 'block';
|
1200 |
+
menuBar.style.opacity = '1';
|
1201 |
+
menuBar.style.visibility = 'visible';
|
1202 |
+
menuBar.style.zIndex = '9999';
|
1203 |
+
});
|
1204 |
+
}
|
1205 |
+
} catch (e) {
|
1206 |
+
console.warn('์ปจํธ๋กค๋ฐ ์คํ์ผ ์ ์ฉ ์ค ์ค๋ฅ:', e);
|
1207 |
+
}
|
1208 |
+
}, 1000);
|
1209 |
+
|
1210 |
+
console.log('FlipBook ์์ฑ ์๋ฃ');
|
1211 |
+
} catch (error) {
|
1212 |
+
console.error('FlipBook ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:', error);
|
1213 |
+
showError("FlipBook์ ์์ฑํ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
|
1214 |
+
}
|
1215 |
+
}
|
1216 |
+
|
1217 |
+
/* โโ ๋ค๋น๊ฒ์ด์
โโ */
|
1218 |
+
$id('homeBtn').onclick=()=>{
|
1219 |
+
if(fb) {
|
1220 |
+
fb.destroy();
|
1221 |
+
viewer.innerHTML = '';
|
1222 |
+
fb = null;
|
1223 |
+
}
|
1224 |
+
toggle(true);
|
1225 |
+
};
|
1226 |
+
|
1227 |
+
function toggle(showHome){
|
1228 |
+
$id('home').style.display=showHome?'block':'none';
|
1229 |
+
$id('viewerPage').style.display=showHome?'none':'block';
|
1230 |
+
$id('homeBtn').style.display=showHome?'none':'inline-block';
|
1231 |
+
|
1232 |
+
// ๋ทฐ์ด ๋ชจ๋์ผ ๋ ์คํ์ผ ๋ณ๊ฒฝ
|
1233 |
+
if(!showHome) {
|
1234 |
+
document.body.classList.add('viewer-mode');
|
1235 |
+
$id('homeBtn').style.position = 'fixed';
|
1236 |
+
$id('homeBtn').style.top = '20px';
|
1237 |
+
$id('homeBtn').style.left = '20px';
|
1238 |
+
$id('homeBtn').style.zIndex = '9999';
|
1239 |
+
$id('homeBtn').style.fontSize = '';
|
1240 |
+
$id('homeBtn').style.width = '52px';
|
1241 |
+
$id('homeBtn').style.height = '52px';
|
1242 |
+
$id('homeBtn').style.boxShadow = '0 4px 15px rgba(0,0,0,0.15)';
|
1243 |
+
} else {
|
1244 |
+
document.body.classList.remove('viewer-mode');
|
1245 |
+
$id('homeBtn').style.position = '';
|
1246 |
+
$id('homeBtn').style.top = '';
|
1247 |
+
$id('homeBtn').style.left = '';
|
1248 |
+
$id('homeBtn').style.zIndex = '';
|
1249 |
+
$id('homeBtn').style.fontSize = '';
|
1250 |
+
$id('homeBtn').style.width = '';
|
1251 |
+
$id('homeBtn').style.height = '';
|
1252 |
+
$id('homeBtn').style.boxShadow = '';
|
1253 |
+
}
|
1254 |
+
}
|
1255 |
+
|
1256 |
+
/* -- ๋ก๋ฉ ๋ฐ ์ค๋ฅ ํ์ -- */
|
1257 |
+
function showLoading(message, progress = -1) {
|
1258 |
+
// ๊ธฐ์กด ๋ก๋ฉ ์ปจํ
์ด๋๊ฐ ์๋ค๋ฉด ์ ๊ฑฐ
|
1259 |
+
hideLoading();
|
1260 |
+
|
1261 |
+
const loadingContainer = document.createElement('div');
|
1262 |
+
loadingContainer.className = 'loading-container fade-in';
|
1263 |
+
loadingContainer.id = 'loadingContainer';
|
1264 |
+
|
1265 |
+
let progressBarHtml = '';
|
1266 |
+
if (progress >= 0) {
|
1267 |
+
progressBarHtml = `
|
1268 |
+
<div class="progress-bar-container">
|
1269 |
+
<div id="progressBar" class="progress-bar" style="width: ${progress}%;"></div>
|
1270 |
+
</div>
|
1271 |
+
`;
|
1272 |
+
}
|
1273 |
+
|
1274 |
+
loadingContainer.innerHTML = `
|
1275 |
+
<div class="loading-spinner"></div>
|
1276 |
+
<p class="loading-text" id="loadingText">${message || '๋ก๋ฉ ์ค...'}</p>
|
1277 |
+
${progressBarHtml}
|
1278 |
+
`;
|
1279 |
+
|
1280 |
+
document.body.appendChild(loadingContainer);
|
1281 |
+
}
|
1282 |
+
|
1283 |
+
function updateLoading(message, progress = -1) {
|
1284 |
+
const loadingText = $id('loadingText');
|
1285 |
+
if (loadingText) {
|
1286 |
+
loadingText.textContent = message;
|
1287 |
+
}
|
1288 |
+
|
1289 |
+
if (progress >= 0) {
|
1290 |
+
let progressBar = $id('progressBar');
|
1291 |
+
|
1292 |
+
if (!progressBar) {
|
1293 |
+
const loadingContainer = $id('loadingContainer');
|
1294 |
+
if (loadingContainer) {
|
1295 |
+
const progressContainer = document.createElement('div');
|
1296 |
+
progressContainer.className = 'progress-bar-container';
|
1297 |
+
progressContainer.innerHTML = `<div id="progressBar" class="progress-bar" style="width: ${progress}%;"></div>`;
|
1298 |
+
loadingContainer.appendChild(progressContainer);
|
1299 |
+
progressBar = $id('progressBar');
|
1300 |
+
}
|
1301 |
+
} else {
|
1302 |
+
progressBar.style.width = `${progress}%`;
|
1303 |
+
}
|
1304 |
+
}
|
1305 |
+
}
|
1306 |
+
|
1307 |
+
function hideLoading() {
|
1308 |
+
const loadingContainer = $id('loadingContainer');
|
1309 |
+
if (loadingContainer) {
|
1310 |
+
loadingContainer.remove();
|
1311 |
+
}
|
1312 |
+
}
|
1313 |
+
|
1314 |
+
function showError(message) {
|
1315 |
+
// ๊ธฐ์กด ์ค๋ฅ ๋ฉ์์ง๊ฐ ์๋ค๋ฉด ์ ๊ฑฐ
|
1316 |
+
const existingError = $id('errorContainer');
|
1317 |
+
if (existingError) {
|
1318 |
+
existingError.remove();
|
1319 |
+
}
|
1320 |
+
|
1321 |
+
const errorContainer = document.createElement('div');
|
1322 |
+
errorContainer.className = 'loading-container fade-in';
|
1323 |
+
errorContainer.id = 'errorContainer';
|
1324 |
+
errorContainer.innerHTML = `
|
1325 |
+
<p class="loading-text" style="color: #e74c3c;">${message}</p>
|
1326 |
+
<button id="errorCloseBtn" style="margin-top: 15px; padding: 8px 16px; background: #3498db; color: white; border: none; border-radius: 4px; cursor: pointer;">ํ์ธ</button>
|
1327 |
+
`;
|
1328 |
+
|
1329 |
+
document.body.appendChild(errorContainer);
|
1330 |
+
|
1331 |
+
// ํ์ธ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
1332 |
+
$id('errorCloseBtn').onclick = () => {
|
1333 |
+
errorContainer.remove();
|
1334 |
+
};
|
1335 |
+
|
1336 |
+
// 5์ด ํ ์๋์ผ๋ก ๋ซ๊ธฐ
|
1337 |
+
setTimeout(() => {
|
1338 |
+
if ($id('errorContainer')) {
|
1339 |
+
$id('errorContainer').remove();
|
1340 |
+
}
|
1341 |
+
}, 5000);
|
1342 |
+
}
|
1343 |
+
|
1344 |
+
// ํ์ด์ง ๋ก๋ ์ ์๋ฒ PDF ๋ก๋
|
1345 |
+
window.addEventListener('DOMContentLoaded', () => {
|
1346 |
+
loadServerPDFs();
|
1347 |
+
|
1348 |
+
// ์บ์ ์ํ๋ฅผ ์ฃผ๊ธฐ์ ์ผ๋ก ํ์ธ (5์ด๋ง๋ค)
|
1349 |
+
setInterval(checkCacheStatus, 5000);
|
1350 |
+
});
|
1351 |
+
</script>
|
1352 |
+
</body>
|
1353 |
+
</html>
|
1354 |
+
"""
|
1355 |
+
|
1356 |
@app.get("/", response_class=HTMLResponse)
|
1357 |
async def root():
|
1358 |
+
return get_html_content()
|
1359 |
|
1360 |
if __name__ == "__main__":
|
1361 |
uvicorn.run("app:app", host="0.0.0.0", port=int(os.getenv("PORT", 7860)))
|