Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,22 @@ import shutil
|
|
| 4 |
import uuid
|
| 5 |
from pathlib import Path
|
| 6 |
import json
|
|
|
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
import fitz # PyMuPDF for PDF handling
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Constants
|
| 11 |
TEMP_DIR = "temp"
|
| 12 |
UPLOAD_DIR = os.path.join(TEMP_DIR, "uploads")
|
|
@@ -18,6 +31,7 @@ HTML_DIR = os.path.join("public", "flipbooks") # Directory accessible via web
|
|
| 18 |
for dir_path in [TEMP_DIR, UPLOAD_DIR, OUTPUT_DIR, THUMBS_DIR, HTML_DIR]:
|
| 19 |
os.makedirs(dir_path, exist_ok=True)
|
| 20 |
|
|
|
|
| 21 |
def create_thumbnail(image_path, output_path, size=(300, 300)):
|
| 22 |
"""Create a thumbnail from an image."""
|
| 23 |
try:
|
|
@@ -26,113 +40,103 @@ def create_thumbnail(image_path, output_path, size=(300, 300)):
|
|
| 26 |
img.save(output_path)
|
| 27 |
return output_path
|
| 28 |
except Exception as e:
|
| 29 |
-
|
| 30 |
return None
|
| 31 |
|
|
|
|
| 32 |
def process_pdf(pdf_path, session_id):
|
| 33 |
"""Extract pages from a PDF and save as images with thumbnails."""
|
| 34 |
pages_info = []
|
| 35 |
output_folder = os.path.join(OUTPUT_DIR, session_id)
|
| 36 |
thumbs_folder = os.path.join(THUMBS_DIR, session_id)
|
| 37 |
-
|
| 38 |
os.makedirs(output_folder, exist_ok=True)
|
| 39 |
os.makedirs(thumbs_folder, exist_ok=True)
|
| 40 |
-
|
| 41 |
try:
|
| 42 |
-
# Open the PDF
|
| 43 |
pdf_document = fitz.open(pdf_path)
|
| 44 |
-
|
| 45 |
-
# Process each page
|
| 46 |
for page_num, page in enumerate(pdf_document):
|
| 47 |
-
# Render page to an image with a higher resolution
|
| 48 |
pix = page.get_pixmap(matrix=fitz.Matrix(2, 2))
|
| 49 |
image_path = os.path.join(output_folder, f"page_{page_num + 1}.png")
|
| 50 |
pix.save(image_path)
|
| 51 |
-
|
| 52 |
-
# Create thumbnail
|
| 53 |
thumb_path = os.path.join(thumbs_folder, f"thumb_{page_num + 1}.png")
|
| 54 |
create_thumbnail(image_path, thumb_path)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
html_content = ""
|
| 58 |
-
|
| 59 |
-
if page_num == 0: # First page example
|
| 60 |
-
html_content = """
|
| 61 |
<div style="position: absolute; top: 50px; left: 50px; background-color: rgba(255,255,255,0.7); padding: 10px; border-radius: 5px;">
|
| 62 |
<div style="color: #333; font-size: 18px; font-weight: bold;">μΈν°λν°λΈ νλ¦½λΆ μμ </div>
|
| 63 |
<div style="color: #666; margin-top: 5px;">μ΄ νμ΄μ§λ μΈν°λν°λΈ 컨ν
μΈ κΈ°λ₯μ 보μ¬μ€λλ€.</div>
|
| 64 |
</div>
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
# Add page info with web-accessible paths
|
| 68 |
pages_info.append({
|
| 69 |
"src": f"./temp/output/{session_id}/page_{page_num + 1}.png",
|
| 70 |
"thumb": f"./temp/output/thumbs/{session_id}/thumb_{page_num + 1}.png",
|
| 71 |
"title": f"νμ΄μ§ {page_num + 1}",
|
| 72 |
-
"htmlContent": html_content
|
| 73 |
})
|
| 74 |
-
|
| 75 |
-
|
| 76 |
return pages_info
|
| 77 |
except Exception as e:
|
| 78 |
-
|
| 79 |
return []
|
| 80 |
|
|
|
|
| 81 |
def process_images(image_paths, session_id):
|
| 82 |
"""Process uploaded images and create thumbnails."""
|
| 83 |
pages_info = []
|
| 84 |
output_folder = os.path.join(OUTPUT_DIR, session_id)
|
| 85 |
thumbs_folder = os.path.join(THUMBS_DIR, session_id)
|
| 86 |
-
|
| 87 |
os.makedirs(output_folder, exist_ok=True)
|
| 88 |
os.makedirs(thumbs_folder, exist_ok=True)
|
| 89 |
-
|
| 90 |
for i, img_path in enumerate(image_paths):
|
| 91 |
try:
|
| 92 |
-
# Copy original image to output folder
|
| 93 |
dest_path = os.path.join(output_folder, f"image_{i + 1}.png")
|
| 94 |
shutil.copy(img_path, dest_path)
|
| 95 |
-
|
| 96 |
-
# Create thumbnail
|
| 97 |
thumb_path = os.path.join(thumbs_folder, f"thumb_{i + 1}.png")
|
| 98 |
create_thumbnail(img_path, thumb_path)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
html_content = ""
|
| 102 |
-
|
| 103 |
-
if i == 0: # First image example with HTML content
|
| 104 |
html_content = """
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
"""
|
| 110 |
-
elif i == 1:
|
| 111 |
html_content = """
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
"""
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
pages_info.append({
|
| 120 |
"src": f"./temp/output/{session_id}/image_{i + 1}.png",
|
| 121 |
"thumb": f"./temp/output/thumbs/{session_id}/thumb_{i + 1}.png",
|
| 122 |
"title": f"μ΄λ―Έμ§ {i + 1}",
|
| 123 |
-
"htmlContent": html_content
|
| 124 |
})
|
| 125 |
-
|
| 126 |
-
|
| 127 |
except Exception as e:
|
| 128 |
-
|
| 129 |
-
|
| 130 |
return pages_info
|
| 131 |
|
|
|
|
| 132 |
def create_flipbook_from_pdf(pdf_file, view_mode="2d", skin="light"):
|
| 133 |
"""Create a flipbook from an uploaded PDF."""
|
| 134 |
session_id = str(uuid.uuid4())
|
| 135 |
-
debug_info =
|
| 136 |
|
| 137 |
if not pdf_file:
|
| 138 |
return (
|
|
@@ -141,146 +145,108 @@ def create_flipbook_from_pdf(pdf_file, view_mode="2d", skin="light"):
|
|
| 141 |
)
|
| 142 |
|
| 143 |
try:
|
| 144 |
-
pdf_path = pdf_file.name
|
| 145 |
-
debug_info
|
| 146 |
|
| 147 |
-
# 1) PDF
|
| 148 |
pages_info = process_pdf(pdf_path, session_id)
|
| 149 |
-
debug_info
|
| 150 |
|
| 151 |
if not pages_info:
|
| 152 |
-
|
| 153 |
-
"<div style='color:red;padding:20px;'>PDF μ²λ¦¬ μ€ν¨.</div>",
|
| 154 |
-
"No pages processed",
|
| 155 |
-
)
|
| 156 |
|
| 157 |
-
# 2)
|
| 158 |
-
iframe_html = generate_flipbook_html(
|
| 159 |
-
|
| 160 |
-
)
|
| 161 |
-
return iframe_html, debug_info
|
| 162 |
|
| 163 |
except Exception as e:
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
| 166 |
return (
|
| 167 |
f"<div style='color:red;padding:20px;'>μ€λ₯κ° λ°μνμ΅λλ€: {e}</div>",
|
| 168 |
-
|
| 169 |
)
|
| 170 |
|
| 171 |
|
| 172 |
def create_flipbook_from_images(images, view_mode="2d", skin="light"):
|
| 173 |
"""Create a flipbook from uploaded images."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
try:
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
image_paths = [img.name for img in images]
|
| 182 |
-
debug_info += f"Image paths: {image_paths}\n"
|
| 183 |
-
|
| 184 |
-
pages_info = process_images(image_paths, session_id)
|
| 185 |
-
debug_info += f"Number of images processed: {len(pages_info)}\n"
|
| 186 |
-
else:
|
| 187 |
-
return """<div style="color: red; padding: 20px;">μ΅μ ν κ° μ΄μμ μ΄λ―Έμ§λ₯Ό μ
λ‘λν΄μ£ΌμΈμ.</div>""", "No images uploaded"
|
| 188 |
-
|
| 189 |
if not pages_info:
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
# Generate HTML file and return iframe HTML
|
| 193 |
iframe_html = generate_flipbook_html(pages_info, session_id, view_mode, skin)
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
return iframe_html, debug_info
|
| 197 |
-
|
| 198 |
except Exception as e:
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
def generate_flipbook_html(pages_info, session_id, view_mode, skin):
|
| 204 |
"""Generate a standalone HTML file for the flipbook and return link HTML."""
|
| 205 |
-
# Clean up pages_info to remove None values for JSON serialization
|
| 206 |
for page in pages_info:
|
| 207 |
-
if
|
| 208 |
-
|
| 209 |
-
if
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
# Convert pages_info to JSON for JavaScript
|
| 213 |
pages_json = json.dumps(pages_info)
|
| 214 |
-
|
| 215 |
-
# Create a unique filename for this session
|
| 216 |
html_filename = f"flipbook_{session_id}.html"
|
| 217 |
html_path = os.path.join(HTML_DIR, html_filename)
|
| 218 |
-
|
| 219 |
-
# Create the full HTML file content
|
| 220 |
html_content = f"""
|
| 221 |
<!DOCTYPE html>
|
| 222 |
-
<html lang
|
| 223 |
<head>
|
| 224 |
-
<meta charset
|
| 225 |
-
<meta name
|
| 226 |
<title>3D ν립λΆ</title>
|
| 227 |
-
<link rel
|
| 228 |
<style>
|
| 229 |
-
body, html {{
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
}}
|
| 235 |
-
#flipbook-container {{
|
| 236 |
-
width: 100%;
|
| 237 |
-
height: 100%;
|
| 238 |
-
position: absolute;
|
| 239 |
-
top: 0;
|
| 240 |
-
left: 0;
|
| 241 |
-
}}
|
| 242 |
-
.loading {{
|
| 243 |
-
position: absolute;
|
| 244 |
-
top: 50%;
|
| 245 |
-
left: 50%;
|
| 246 |
-
transform: translate(-50%, -50%);
|
| 247 |
-
text-align: center;
|
| 248 |
-
font-family: Arial, sans-serif;
|
| 249 |
-
}}
|
| 250 |
-
.loading .spinner {{
|
| 251 |
-
width: 50px;
|
| 252 |
-
height: 50px;
|
| 253 |
-
border: 5px solid #f3f3f3;
|
| 254 |
-
border-top: 5px solid #3498db;
|
| 255 |
-
border-radius: 50%;
|
| 256 |
-
animation: spin 1s linear infinite;
|
| 257 |
-
margin: 0 auto 20px;
|
| 258 |
-
}}
|
| 259 |
-
@keyframes spin {{
|
| 260 |
-
0% {{ transform: rotate(0deg); }}
|
| 261 |
-
100% {{ transform: rotate(360deg); }}
|
| 262 |
-
}}
|
| 263 |
</style>
|
| 264 |
-
<script src
|
| 265 |
-
<script src
|
| 266 |
-
<script src
|
| 267 |
-
<script src
|
| 268 |
-
<script src
|
| 269 |
</head>
|
| 270 |
<body>
|
| 271 |
-
<div id
|
| 272 |
-
<div id
|
| 273 |
-
<div class
|
| 274 |
<div>νλ¦½λΆ λ‘λ© μ€...</div>
|
| 275 |
</div>
|
| 276 |
-
|
| 277 |
<script>
|
| 278 |
-
document.addEventListener('DOMContentLoaded', function() {{
|
| 279 |
-
|
| 280 |
-
function hideLoading() {{
|
| 281 |
-
document.getElementById('loading').style.display = 'none';
|
| 282 |
-
}}
|
| 283 |
-
|
| 284 |
try {{
|
| 285 |
const options = {{
|
| 286 |
pages: {pages_json},
|
|
@@ -302,14 +268,12 @@ def generate_flipbook_html(pages_info, session_id, view_mode, skin):
|
|
| 302 |
btnExpand: {{ enabled: true }},
|
| 303 |
rightToLeft: false,
|
| 304 |
autoplayOnStart: false,
|
| 305 |
-
autoplayInterval: 3000
|
| 306 |
}};
|
| 307 |
-
|
| 308 |
const container = document.getElementById('flipbook-container');
|
| 309 |
if (container) {{
|
| 310 |
-
console.log('Initializing flipbook...');
|
| 311 |
new FlipBook(container, options);
|
| 312 |
-
setTimeout(hideLoading, 1000);
|
| 313 |
}} else {{
|
| 314 |
console.error('Flipbook container not found');
|
| 315 |
alert('μ€λ₯: νλ¦½λΆ μ»¨ν
μ΄λλ₯Ό μ°Ύμ μ μμ΅λλ€.');
|
|
@@ -324,14 +288,17 @@ def generate_flipbook_html(pages_info, session_id, view_mode, skin):
|
|
| 324 |
</body>
|
| 325 |
</html>
|
| 326 |
"""
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
with open(html_path, 'w', encoding='utf-8') as f:
|
| 330 |
f.write(html_content)
|
| 331 |
-
|
| 332 |
-
# Return HTML with a direct link to open the flipbook in a new tab
|
| 333 |
public_url = f"/public/flipbooks/{html_filename}"
|
| 334 |
link_html = f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
<div style="text-align:center; padding:20px; background-color:#f9f9f9; border-radius:5px; margin-bottom:20px;">
|
| 336 |
<h2 style="margin-top:0; color:#333;">ν립λΆμ΄ μ€λΉλμμ΅λλ€!</h2>
|
| 337 |
<p style="margin-bottom:20px;">μλ λ²νΌμ ν΄λ¦νμ¬ ν립λΆμ μ μ°½μμ μ΄μ΄λ³΄μΈμ.</p>
|
|
|
|
| 4 |
import uuid
|
| 5 |
from pathlib import Path
|
| 6 |
import json
|
| 7 |
+
import logging
|
| 8 |
+
import traceback
|
| 9 |
from PIL import Image
|
| 10 |
import fitz # PyMuPDF for PDF handling
|
| 11 |
|
| 12 |
+
# ββββββββββββββββββββββββββββββββ
|
| 13 |
+
# Logging μ€μ
|
| 14 |
+
# ββββββββββββββββββββββββββββββββ
|
| 15 |
+
logging.basicConfig(
|
| 16 |
+
level=logging.INFO,
|
| 17 |
+
format="%(asctime)s [%(levelname)s] %(message)s",
|
| 18 |
+
filename="app.log", # μ€ν λλ ν°λ¦¬μ app.log νμΌ μ μ₯
|
| 19 |
+
filemode="a",
|
| 20 |
+
)
|
| 21 |
+
logging.info("π Flipbook app started")
|
| 22 |
+
|
| 23 |
# Constants
|
| 24 |
TEMP_DIR = "temp"
|
| 25 |
UPLOAD_DIR = os.path.join(TEMP_DIR, "uploads")
|
|
|
|
| 31 |
for dir_path in [TEMP_DIR, UPLOAD_DIR, OUTPUT_DIR, THUMBS_DIR, HTML_DIR]:
|
| 32 |
os.makedirs(dir_path, exist_ok=True)
|
| 33 |
|
| 34 |
+
|
| 35 |
def create_thumbnail(image_path, output_path, size=(300, 300)):
|
| 36 |
"""Create a thumbnail from an image."""
|
| 37 |
try:
|
|
|
|
| 40 |
img.save(output_path)
|
| 41 |
return output_path
|
| 42 |
except Exception as e:
|
| 43 |
+
logging.error("Error creating thumbnail: %s", e)
|
| 44 |
return None
|
| 45 |
|
| 46 |
+
|
| 47 |
def process_pdf(pdf_path, session_id):
|
| 48 |
"""Extract pages from a PDF and save as images with thumbnails."""
|
| 49 |
pages_info = []
|
| 50 |
output_folder = os.path.join(OUTPUT_DIR, session_id)
|
| 51 |
thumbs_folder = os.path.join(THUMBS_DIR, session_id)
|
| 52 |
+
|
| 53 |
os.makedirs(output_folder, exist_ok=True)
|
| 54 |
os.makedirs(thumbs_folder, exist_ok=True)
|
| 55 |
+
|
| 56 |
try:
|
|
|
|
| 57 |
pdf_document = fitz.open(pdf_path)
|
| 58 |
+
|
|
|
|
| 59 |
for page_num, page in enumerate(pdf_document):
|
|
|
|
| 60 |
pix = page.get_pixmap(matrix=fitz.Matrix(2, 2))
|
| 61 |
image_path = os.path.join(output_folder, f"page_{page_num + 1}.png")
|
| 62 |
pix.save(image_path)
|
| 63 |
+
|
|
|
|
| 64 |
thumb_path = os.path.join(thumbs_folder, f"thumb_{page_num + 1}.png")
|
| 65 |
create_thumbnail(image_path, thumb_path)
|
| 66 |
+
|
| 67 |
+
html_content = """
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
<div style="position: absolute; top: 50px; left: 50px; background-color: rgba(255,255,255,0.7); padding: 10px; border-radius: 5px;">
|
| 69 |
<div style="color: #333; font-size: 18px; font-weight: bold;">μΈν°λν°λΈ νλ¦½λΆ μμ </div>
|
| 70 |
<div style="color: #666; margin-top: 5px;">μ΄ νμ΄μ§λ μΈν°λν°λΈ 컨ν
μΈ κΈ°λ₯μ 보μ¬μ€λλ€.</div>
|
| 71 |
</div>
|
| 72 |
+
""" if page_num == 0 else None
|
| 73 |
+
|
|
|
|
| 74 |
pages_info.append({
|
| 75 |
"src": f"./temp/output/{session_id}/page_{page_num + 1}.png",
|
| 76 |
"thumb": f"./temp/output/thumbs/{session_id}/thumb_{page_num + 1}.png",
|
| 77 |
"title": f"νμ΄μ§ {page_num + 1}",
|
| 78 |
+
"htmlContent": html_content,
|
| 79 |
})
|
| 80 |
+
logging.info("Processed PDF page %d: %s", page_num + 1, image_path)
|
| 81 |
+
|
| 82 |
return pages_info
|
| 83 |
except Exception as e:
|
| 84 |
+
logging.error("Error processing PDF: %s", e)
|
| 85 |
return []
|
| 86 |
|
| 87 |
+
|
| 88 |
def process_images(image_paths, session_id):
|
| 89 |
"""Process uploaded images and create thumbnails."""
|
| 90 |
pages_info = []
|
| 91 |
output_folder = os.path.join(OUTPUT_DIR, session_id)
|
| 92 |
thumbs_folder = os.path.join(THUMBS_DIR, session_id)
|
| 93 |
+
|
| 94 |
os.makedirs(output_folder, exist_ok=True)
|
| 95 |
os.makedirs(thumbs_folder, exist_ok=True)
|
| 96 |
+
|
| 97 |
for i, img_path in enumerate(image_paths):
|
| 98 |
try:
|
|
|
|
| 99 |
dest_path = os.path.join(output_folder, f"image_{i + 1}.png")
|
| 100 |
shutil.copy(img_path, dest_path)
|
| 101 |
+
|
|
|
|
| 102 |
thumb_path = os.path.join(thumbs_folder, f"thumb_{i + 1}.png")
|
| 103 |
create_thumbnail(img_path, thumb_path)
|
| 104 |
+
|
| 105 |
+
if i == 0:
|
|
|
|
|
|
|
|
|
|
| 106 |
html_content = """
|
| 107 |
+
<div style=\"position: absolute; top: 50px; left: 50px; background-color: rgba(255,255,255,0.7); padding: 10px; border-radius: 5px;\">
|
| 108 |
+
<div style=\"color: #333; font-size: 18px; font-weight: bold;\">μ΄λ―Έμ§ κ°€λ¬λ¦¬</div>
|
| 109 |
+
<div style=\"color: #666; margin-top: 5px;\">κ°€λ¬λ¦¬μ 첫 λ²μ§Έ μ΄λ―Έμ§μ
λλ€.</div>
|
| 110 |
+
</div>
|
| 111 |
"""
|
| 112 |
+
elif i == 1:
|
| 113 |
html_content = """
|
| 114 |
+
<div style=\"position: absolute; top: 50px; left: 50px; background-color: rgba(255,255,255,0.7); padding: 10px; border-radius: 5px;\">
|
| 115 |
+
<div style=\"color: #333; font-size: 18px; font-weight: bold;\">λ λ²μ§Έ μ΄λ―Έμ§</div>
|
| 116 |
+
<div style=\"color: #666; margin-top: 5px;\">νμ΄μ§λ₯Ό λκΈ°κ±°λ λͺ¨μ리λ₯Ό λλκ·Ένμ¬ μ΄λ―Έμ§λ₯Ό νμν μ μμ΅λλ€.</div>
|
| 117 |
+
</div>
|
| 118 |
"""
|
| 119 |
+
else:
|
| 120 |
+
html_content = None
|
| 121 |
+
|
| 122 |
pages_info.append({
|
| 123 |
"src": f"./temp/output/{session_id}/image_{i + 1}.png",
|
| 124 |
"thumb": f"./temp/output/thumbs/{session_id}/thumb_{i + 1}.png",
|
| 125 |
"title": f"μ΄λ―Έμ§ {i + 1}",
|
| 126 |
+
"htmlContent": html_content,
|
| 127 |
})
|
| 128 |
+
logging.info("Processed image %d: %s", i + 1, dest_path)
|
| 129 |
+
|
| 130 |
except Exception as e:
|
| 131 |
+
logging.error("Error processing image %s: %s", img_path, e)
|
| 132 |
+
|
| 133 |
return pages_info
|
| 134 |
|
| 135 |
+
|
| 136 |
def create_flipbook_from_pdf(pdf_file, view_mode="2d", skin="light"):
|
| 137 |
"""Create a flipbook from an uploaded PDF."""
|
| 138 |
session_id = str(uuid.uuid4())
|
| 139 |
+
debug_info = []
|
| 140 |
|
| 141 |
if not pdf_file:
|
| 142 |
return (
|
|
|
|
| 145 |
)
|
| 146 |
|
| 147 |
try:
|
| 148 |
+
pdf_path = pdf_file.name
|
| 149 |
+
debug_info.append(f"PDF path: {pdf_path}")
|
| 150 |
|
| 151 |
+
# 1) PDF β μ΄λ―Έμ§
|
| 152 |
pages_info = process_pdf(pdf_path, session_id)
|
| 153 |
+
debug_info.append(f"Number of pages: {len(pages_info)}")
|
| 154 |
|
| 155 |
if not pages_info:
|
| 156 |
+
raise RuntimeError("PDF μ²λ¦¬ κ²°κ³Όκ° λΉμ΄ μμ΅λλ€.")
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
# 2) HTML μμ±
|
| 159 |
+
iframe_html = generate_flipbook_html(pages_info, session_id, view_mode, skin)
|
| 160 |
+
return iframe_html, "\n".join(debug_info)
|
|
|
|
|
|
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
+
tb = traceback.format_exc()
|
| 164 |
+
logging.error(tb)
|
| 165 |
+
debug_info.append("β ERROR βββ")
|
| 166 |
+
debug_info.append(tb)
|
| 167 |
return (
|
| 168 |
f"<div style='color:red;padding:20px;'>μ€λ₯κ° λ°μνμ΅λλ€: {e}</div>",
|
| 169 |
+
"\n".join(debug_info),
|
| 170 |
)
|
| 171 |
|
| 172 |
|
| 173 |
def create_flipbook_from_images(images, view_mode="2d", skin="light"):
|
| 174 |
"""Create a flipbook from uploaded images."""
|
| 175 |
+
session_id = str(uuid.uuid4())
|
| 176 |
+
debug_info = []
|
| 177 |
+
|
| 178 |
+
if not images:
|
| 179 |
+
return (
|
| 180 |
+
"<div style='color:red;padding:20px;'>μ΅μ ν κ° μ΄μμ μ΄λ―Έμ§λ₯Ό μ
λ‘λν΄μ£ΌμΈμ.</div>",
|
| 181 |
+
"No images uploaded",
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
try:
|
| 185 |
+
image_paths = [img.name for img in images]
|
| 186 |
+
debug_info.append(f"Image paths: {image_paths}")
|
| 187 |
+
|
| 188 |
+
pages_info = process_images(image_paths, session_id)
|
| 189 |
+
debug_info.append(f"Number of images processed: {len(pages_info)}")
|
| 190 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
if not pages_info:
|
| 192 |
+
raise RuntimeError("μ΄λ―Έμ§ μ²λ¦¬ κ²°κ³Όκ° λΉμ΄ μμ΅λλ€.")
|
| 193 |
+
|
|
|
|
| 194 |
iframe_html = generate_flipbook_html(pages_info, session_id, view_mode, skin)
|
| 195 |
+
return iframe_html, "\n".join(debug_info)
|
| 196 |
+
|
|
|
|
|
|
|
| 197 |
except Exception as e:
|
| 198 |
+
tb = traceback.format_exc()
|
| 199 |
+
logging.error(tb)
|
| 200 |
+
debug_info.append("β ERROR βββ")
|
| 201 |
+
debug_info.append(tb)
|
| 202 |
+
return (
|
| 203 |
+
f"<div style='color:red;padding:20px;'>μ€λ₯κ° λ°μνμ΅λλ€: {e}</div>",
|
| 204 |
+
"\n".join(debug_info),
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
|
| 208 |
def generate_flipbook_html(pages_info, session_id, view_mode, skin):
|
| 209 |
"""Generate a standalone HTML file for the flipbook and return link HTML."""
|
|
|
|
| 210 |
for page in pages_info:
|
| 211 |
+
if page.get("htmlContent") is None:
|
| 212 |
+
page.pop("htmlContent", None)
|
| 213 |
+
if page.get("items") is None:
|
| 214 |
+
page.pop("items", None)
|
| 215 |
+
|
|
|
|
| 216 |
pages_json = json.dumps(pages_info)
|
|
|
|
|
|
|
| 217 |
html_filename = f"flipbook_{session_id}.html"
|
| 218 |
html_path = os.path.join(HTML_DIR, html_filename)
|
| 219 |
+
|
|
|
|
| 220 |
html_content = f"""
|
| 221 |
<!DOCTYPE html>
|
| 222 |
+
<html lang=\"ko\">
|
| 223 |
<head>
|
| 224 |
+
<meta charset=\"UTF-8\">
|
| 225 |
+
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
|
| 226 |
<title>3D ν립λΆ</title>
|
| 227 |
+
<link rel=\"stylesheet\" type=\"text/css\" href=\"../flipbook.css\">
|
| 228 |
<style>
|
| 229 |
+
body, html {{ margin: 0; padding: 0; height: 100%; overflow: hidden; }}
|
| 230 |
+
#flipbook-container {{ width: 100%; height: 100%; position: absolute; top: 0; left: 0; }}
|
| 231 |
+
.loading {{ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center; font-family: Arial, sans-serif; }}
|
| 232 |
+
.loading .spinner {{ width: 50px; height: 50px; border: 5px solid #f3f3f3; border-top: 5px solid #3498db; border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto 20px; }}
|
| 233 |
+
@keyframes spin {{ 0% {{ transform: rotate(0deg); }} 100% {{ transform: rotate(360deg); }} }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
</style>
|
| 235 |
+
<script src=\"../flipbook.js\"></script>
|
| 236 |
+
<script src=\"../flipbook.webgl.js\"></script>
|
| 237 |
+
<script src=\"../flipbook.swipe.js\"></script>
|
| 238 |
+
<script src=\"../flipbook.scroll.js\"></script>
|
| 239 |
+
<script src=\"../flipbook.book3.js\"></script>
|
| 240 |
</head>
|
| 241 |
<body>
|
| 242 |
+
<div id=\"flipbook-container\"></div>
|
| 243 |
+
<div id=\"loading\" class=\"loading\">
|
| 244 |
+
<div class=\"spinner\"></div>
|
| 245 |
<div>νλ¦½λΆ λ‘λ© μ€...</div>
|
| 246 |
</div>
|
|
|
|
| 247 |
<script>
|
| 248 |
+
document.addEventListener('DOMContentLoaded', function () {{
|
| 249 |
+
function hideLoading() {{ document.getElementById('loading').style.display = 'none'; }}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
try {{
|
| 251 |
const options = {{
|
| 252 |
pages: {pages_json},
|
|
|
|
| 268 |
btnExpand: {{ enabled: true }},
|
| 269 |
rightToLeft: false,
|
| 270 |
autoplayOnStart: false,
|
| 271 |
+
autoplayInterval: 3000,
|
| 272 |
}};
|
|
|
|
| 273 |
const container = document.getElementById('flipbook-container');
|
| 274 |
if (container) {{
|
|
|
|
| 275 |
new FlipBook(container, options);
|
| 276 |
+
setTimeout(hideLoading, 1000);
|
| 277 |
}} else {{
|
| 278 |
console.error('Flipbook container not found');
|
| 279 |
alert('μ€λ₯: νλ¦½λΆ μ»¨ν
μ΄λλ₯Ό μ°Ύμ μ μμ΅λλ€.');
|
|
|
|
| 288 |
</body>
|
| 289 |
</html>
|
| 290 |
"""
|
| 291 |
+
|
| 292 |
+
with open(html_path, "w", encoding="utf-8") as f:
|
|
|
|
| 293 |
f.write(html_content)
|
| 294 |
+
|
|
|
|
| 295 |
public_url = f"/public/flipbooks/{html_filename}"
|
| 296 |
link_html = f"""
|
| 297 |
+
<div style=\"text-align:center; padding:20px; background-color:#f9f9f9; border-radius:5px; margin-bottom:20px;\">
|
| 298 |
+
<h2 style=\"margin-top:0; color:#333;\">ν립λΆμ΄ μ€λΉλμμ΅λλ€!</h2>
|
| 299 |
+
<p style=\"margin-bottom:20px;\">μλ λ²νΌμ ν΄λ¦νμ¬ ν립λΆμ μ μ°½μμ μ΄μ΄λ³΄μΈμ.</p>
|
| 300 |
+
<a href=\"{public_url}\" target=\"_blank\" style=\"display:inline-block; background-color:#4CAF50; color:white; padding
|
| 301 |
+
|
| 302 |
<div style="text-align:center; padding:20px; background-color:#f9f9f9; border-radius:5px; margin-bottom:20px;">
|
| 303 |
<h2 style="margin-top:0; color:#333;">ν립λΆμ΄ μ€λΉλμμ΅λλ€!</h2>
|
| 304 |
<p style="margin-bottom:20px;">μλ λ²νΌμ ν΄λ¦νμ¬ ν립λΆμ μ μ°½μμ μ΄μ΄λ³΄μΈμ.</p>
|