Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -57,7 +57,7 @@ h2{margin:0;font-size:1.5rem;font-weight:600}
|
|
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:
|
61 |
border:1px solid #ccc;
|
62 |
border-radius:6px;
|
63 |
cursor:pointer;
|
@@ -71,24 +71,45 @@ h2{margin:0;font-size:1.5rem;font-weight:600}
|
|
71 |
justify-content: center;
|
72 |
}
|
73 |
.card img{
|
74 |
-
width:
|
75 |
height:auto;
|
76 |
object-fit:contain;
|
77 |
-
position:
|
78 |
-
|
79 |
-
|
|
|
80 |
border: 1px solid #ddd;
|
81 |
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
|
82 |
}
|
83 |
.card p{
|
84 |
text-align:center;
|
85 |
margin:6px 0;
|
|
|
|
|
|
|
|
|
86 |
background: rgba(255, 255, 255, 0.7);
|
87 |
padding: 4px 8px;
|
88 |
border-radius: 4px;
|
|
|
|
|
|
|
|
|
|
|
89 |
}
|
90 |
button.upload{all:unset;cursor:pointer;border:1px solid #bbb;padding:8px 14px;border-radius:6px;background:#fff;margin:0 8px}
|
91 |
-
#viewer{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
</style></head><body>
|
93 |
|
94 |
<header>
|
@@ -129,7 +150,13 @@ function addCard(i,thumb,title){
|
|
129 |
const d=document.createElement('div');
|
130 |
d.className='card';
|
131 |
d.onclick=()=>open(i);
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
grid.appendChild(d);
|
134 |
}
|
135 |
|
@@ -203,43 +230,70 @@ function open(i){
|
|
203 |
if(pages[0].path) {
|
204 |
// μλ² PDF νμΌ λ‘λ
|
205 |
fetch(`/api/pdf-content?path=${encodeURIComponent(pages[0].path)}`)
|
206 |
-
.then(response =>
|
|
|
|
|
|
|
|
|
|
|
207 |
.then(pdfData => {
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
|
|
218 |
|
219 |
-
|
220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
})
|
222 |
.catch(error => {
|
223 |
-
console.error('PDF
|
|
|
224 |
});
|
225 |
} else {
|
226 |
// μ
λ‘λλ νλ‘μ νΈ λ³΄κΈ°
|
|
|
227 |
createFlipBook(pages);
|
228 |
}
|
229 |
}
|
230 |
|
231 |
function createFlipBook(pages) {
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
}
|
244 |
|
245 |
/* ββ λ€λΉκ²μ΄μ
ββ */
|
@@ -285,14 +339,32 @@ async def get_pdf_thumbnail(path: str):
|
|
285 |
except Exception as e:
|
286 |
return {"error": str(e), "thumbnail": None}
|
287 |
|
288 |
-
# API μλν¬μΈνΈ: PDF μ½ν
μΈ κ°μ Έμ€κΈ°
|
289 |
@app.get("/api/pdf-content")
|
290 |
async def get_pdf_content(path: str):
|
291 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
with open(path, "rb") as pdf_file:
|
293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
except Exception as e:
|
295 |
-
|
|
|
|
|
|
|
296 |
|
297 |
@app.get("/", response_class=HTMLResponse)
|
298 |
async def root():
|
|
|
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: 130%; /* λ°°κ²½ μ΄λ―Έμ§ 30% νλ */
|
61 |
border:1px solid #ccc;
|
62 |
border-radius:6px;
|
63 |
cursor:pointer;
|
|
|
71 |
justify-content: center;
|
72 |
}
|
73 |
.card img{
|
74 |
+
width:65%; /* μΈλ€μΌ ν¬κΈ° μ‘°μ */
|
75 |
height:auto;
|
76 |
object-fit:contain;
|
77 |
+
position:absolute; /* μ λ μμΉλ‘ λ³κ²½ */
|
78 |
+
top:50%; /* μλ¨μμ 50% */
|
79 |
+
left:50%; /* μ’μΈ‘μμ 50% */
|
80 |
+
transform: translate(-50%, -50%); /* μ μ€μ λ°°μΉ */
|
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 |
}
|
100 |
button.upload{all:unset;cursor:pointer;border:1px solid #bbb;padding:8px 14px;border-radius:6px;background:#fff;margin:0 8px}
|
101 |
+
#viewer{
|
102 |
+
width:100%;
|
103 |
+
height:100vh; /* μ 체 νλ©΄ λμ΄λ‘ νμ₯ */
|
104 |
+
max-width:100%; /* μ΅λ λλΉ μ ν ν΄μ */
|
105 |
+
margin:0; /* λ§μ§ μ κ±° */
|
106 |
+
background:#fff;
|
107 |
+
border:none; /* ν
λ리 μ κ±° */
|
108 |
+
position:fixed; /* κ³ μ μμΉ */
|
109 |
+
top:0;
|
110 |
+
left:0;
|
111 |
+
z-index:1000; /* μ΅μμ νμ */
|
112 |
+
}
|
113 |
</style></head><body>
|
114 |
|
115 |
<header>
|
|
|
150 |
const d=document.createElement('div');
|
151 |
d.className='card';
|
152 |
d.onclick=()=>open(i);
|
153 |
+
|
154 |
+
// μ λͺ© 10κΈμ μ ν λ° λ§μ€μν μ²λ¦¬
|
155 |
+
const displayTitle = title ?
|
156 |
+
(title.length > 10 ? title.substring(0, 10) + '...' : title) :
|
157 |
+
'νλ‘μ νΈ ' + (i+1);
|
158 |
+
|
159 |
+
d.innerHTML=`<img src="${thumb}"><p title="${title || 'νλ‘μ νΈ ' + (i+1)}">${displayTitle}</p>`;
|
160 |
grid.appendChild(d);
|
161 |
}
|
162 |
|
|
|
230 |
if(pages[0].path) {
|
231 |
// μλ² PDF νμΌ λ‘λ
|
232 |
fetch(`/api/pdf-content?path=${encodeURIComponent(pages[0].path)}`)
|
233 |
+
.then(response => {
|
234 |
+
if (!response.ok) {
|
235 |
+
throw new Error('PDF λ‘λ μ€ν¨: ' + response.statusText);
|
236 |
+
}
|
237 |
+
return response.arrayBuffer();
|
238 |
+
})
|
239 |
.then(pdfData => {
|
240 |
+
// PDF λ°μ΄ν° λ‘λ νμΈ λ‘κΉ
|
241 |
+
console.log('PDF λ°μ΄ν° λ‘λ μλ£:', pdfData.byteLength + ' λ°μ΄νΈ');
|
242 |
+
|
243 |
+
return pdfjsLib.getDocument({data: pdfData}).promise;
|
244 |
+
})
|
245 |
+
.then(async pdf => {
|
246 |
+
console.log('PDF λ¬Έμ λ‘λ μλ£. νμ΄μ§ μ:', pdf.numPages);
|
247 |
+
|
248 |
+
const pdfPages = [];
|
249 |
+
for(let p = 1; p <= pdf.numPages; p++) {
|
250 |
+
console.log('νμ΄μ§ λ λλ§ μ€:', p + '/' + pdf.numPages);
|
251 |
|
252 |
+
const pg = await pdf.getPage(p);
|
253 |
+
const vp = pg.getViewport({scale: 1});
|
254 |
+
const c = document.createElement('canvas');
|
255 |
+
c.width = vp.width;
|
256 |
+
c.height = vp.height;
|
257 |
+
|
258 |
+
await pg.render({canvasContext: c.getContext('2d'), viewport: vp}).promise;
|
259 |
+
pdfPages.push({src: c.toDataURL(), thumb: c.toDataURL()});
|
260 |
+
}
|
261 |
+
|
262 |
+
console.log('λͺ¨λ νμ΄μ§ λ λλ§ μλ£:', pdfPages.length);
|
263 |
+
createFlipBook(pdfPages);
|
264 |
})
|
265 |
.catch(error => {
|
266 |
+
console.error('PDF μ²λ¦¬ μ€ μ€λ₯ λ°μ:', error);
|
267 |
+
alert('PDFλ₯Ό λ‘λνλ μ€ μ€λ₯κ° λ°μνμ΅λλ€: ' + error.message);
|
268 |
});
|
269 |
} else {
|
270 |
// μ
λ‘λλ νλ‘μ νΈ λ³΄κΈ°
|
271 |
+
console.log('λ‘컬 μ
λ‘λλ νλ‘μ νΈ λ λλ§:', pages.length + 'νμ΄μ§');
|
272 |
createFlipBook(pages);
|
273 |
}
|
274 |
}
|
275 |
|
276 |
function createFlipBook(pages) {
|
277 |
+
console.log('FlipBook μμ± μμ. νμ΄μ§ μ:', pages.length);
|
278 |
+
|
279 |
+
try {
|
280 |
+
fb = new FlipBook(viewer, {
|
281 |
+
pages: pages,
|
282 |
+
viewMode: 'webgl',
|
283 |
+
autoSize: true,
|
284 |
+
flipDuration: 800,
|
285 |
+
backgroundColor: '#fff',
|
286 |
+
/* π λ΄μ₯ μ¬μ΄λ */
|
287 |
+
sound: true,
|
288 |
+
assets: {flipMp3: 'static/turnPage2.mp3', hardFlipMp3: 'static/turnPage2.mp3'},
|
289 |
+
controlsProps: {enableFullscreen: true, thumbnails: true}
|
290 |
+
});
|
291 |
+
|
292 |
+
console.log('FlipBook μμ± μλ£');
|
293 |
+
} catch (error) {
|
294 |
+
console.error('FlipBook μμ± μ€ μ€λ₯ λ°μ:', error);
|
295 |
+
alert('FlipBookμ μμ±νλ μ€ μ€λ₯κ° λ°μνμ΅λλ€: ' + error.message);
|
296 |
+
}
|
297 |
}
|
298 |
|
299 |
/* ββ λ€λΉκ²μ΄μ
ββ */
|
|
|
339 |
except Exception as e:
|
340 |
return {"error": str(e), "thumbnail": None}
|
341 |
|
|
|
342 |
@app.get("/api/pdf-content")
|
343 |
async def get_pdf_content(path: str):
|
344 |
try:
|
345 |
+
# νμΌ μ‘΄μ¬ μ¬λΆ νμΈ
|
346 |
+
pdf_path = pathlib.Path(path)
|
347 |
+
if not pdf_path.exists():
|
348 |
+
return {"error": f"νμΌμ μ°Ύμ μ μμ΅λλ€: {path}"}, 404
|
349 |
+
|
350 |
+
# νμΌ μ½κΈ°
|
351 |
with open(path, "rb") as pdf_file:
|
352 |
+
content = pdf_file.read()
|
353 |
+
|
354 |
+
# μλ΅ ν€λ μ€μ - PDF νμΌμμ λͺ
μ
|
355 |
+
headers = {
|
356 |
+
"Content-Type": "application/pdf",
|
357 |
+
"Content-Disposition": f"inline; filename={pdf_path.name}"
|
358 |
+
}
|
359 |
+
|
360 |
+
# νμΌ μ½ν
μΈ λ°ν
|
361 |
+
from fastapi.responses import Response
|
362 |
+
return Response(content=content, headers=headers)
|
363 |
except Exception as e:
|
364 |
+
import traceback
|
365 |
+
error_details = traceback.format_exc()
|
366 |
+
print(f"PDF μ½ν
μΈ λ‘λ μ€λ₯: {str(e)}\n{error_details}")
|
367 |
+
return {"error": str(e)}, 500
|
368 |
|
369 |
@app.get("/", response_class=HTMLResponse)
|
370 |
async def root():
|