-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfallback_handler.py
392 lines (343 loc) · 14.5 KB
/
fallback_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
import os
import json
import time
import hashlib
import logging
from datetime import datetime
import tempfile
from functools import wraps
import requests
from PIL import Image
from fpdf import FPDF
# CloudConvert API key (replace with your actual key)
CLOUDCONVERT_API_KEY = "your_cloudconvert_api_key"
# Configure logging
LOG_DIR = "logs"
os.makedirs(LOG_DIR, exist_ok=True)
logging.basicConfig(
filename=os.path.join(
LOG_DIR, f'converter_{datetime.now().strftime("%Y%m%d_%H%M%S")}.log'
),
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Add new global constants
HISTORY_FILE = "conversion_history.json"
CONVERSION_CACHE = ".conversion_cache"
# Add new constants for PDF settings
PAGE_SIZES = {
'A4': (210, 297),
'Letter': (216, 279),
'Legal': (216, 356),
'Custom': None
}
def get_file_hash(filepath):
"""Generate hash of file content for change detection."""
with open(filepath, "rb") as f:
return hashlib.md5(f.read()).hexdigest()
def load_conversion_history():
"""Load conversion history from JSON file."""
try:
if os.path.exists(HISTORY_FILE):
with open(HISTORY_FILE, 'r') as f:
return json.load(f)
except Exception as e:
logging.error(f"Error loading conversion history: {e}")
return {}
def save_conversion_history(history):
"""Save conversion history to JSON file."""
try:
with open(HISTORY_FILE, 'w') as f:
json.dump(history, f, indent=2)
except Exception as e:
logging.error(f"Error saving conversion history: {e}")
def scan_directory(directory, supported_formats, min_date=None):
"""Recursively scan directory for supported files."""
files_info = []
for root, _, files in os.walk(directory):
for file in files:
if file.lower().endswith(supported_formats):
file_path = os.path.join(root, file)
mod_time = os.path.getmtime(file_path)
if min_date is None or mod_time >= min_date:
files_info.append({
'path': file_path,
'modified': mod_time,
'hash': get_file_hash(file_path)
})
return files_info
def retry_on_failure(max_retries=3, delay=1):
"""Decorator to retry failed operations."""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
last_exception = None
for attempt in range(max_retries):
try:
return func(*args, **kwargs)
except Exception as e:
last_exception = e
logging.warning(
f"Attempt {attempt + 1} failed: {str(e)}"
)
time.sleep(delay * (attempt + 1))
logging.error(
f"All {max_retries} attempts failed: {str(last_exception)}"
)
raise last_exception
return wrapper
return decorator
@retry_on_failure(max_retries=3)
def convert_heic_to_jpeg_with_cloudconvert(input_path, output_path):
"""Convert a HEIC file to JPEG using CloudConvert API with retry."""
logging.info(f"Starting CloudConvert conversion for {input_path}")
try:
url = "https://api.cloudconvert.com/v2/jobs"
headers = {
"Authorization": f"Bearer {CLOUDCONVERT_API_KEY}",
"Content-Type": "application/json"
}
data = {
"tasks": {
"import-my-file": {"operation": "import/upload"},
"convert-my-file": {
"operation": "convert",
"input": "import-my-file",
"output_format": "jpg"
},
"export-my-file": {
"operation": "export/url",
"input": "convert-my-file"
}
}
}
# Create a conversion job
response = requests.post(url, json=data, headers=headers)
response.raise_for_status()
job = response.json()
# Upload the HEIC file
upload_url = job["data"]["tasks"]["import-my-file"]["result"]["form"]["url"]
with open(input_path, "rb") as file:
upload_response = requests.post(upload_url, files={"file": file})
upload_response.raise_for_status()
# Wait for conversion and download the result
job_id = job["data"]["id"]
result_url = f"https://api.cloudconvert.com/v2/jobs/{job_id}"
while True:
result_response = requests.get(result_url, headers=headers)
result_response.raise_for_status()
result_data = result_response.json()
status = result_data["data"]["status"]
if status == "finished":
files = result_data["data"]["tasks"]["export-my-file"]["result"]["files"]
download_url = files[0]["url"]
download_response = requests.get(download_url)
with open(output_path, "wb") as f:
f.write(download_response.content)
break
elif status in {"error", "failed"}:
raise RuntimeError("CloudConvert job failed.")
time.sleep(2)
except Exception as e:
raise RuntimeError(f"CloudConvert API error: {e}")
def check_image_issues(image_path):
"""Check for potential issues in the image."""
issues = []
try:
with Image.open(image_path) as img:
# Check resolution
if any(dim > 5000 for dim in img.size):
issues.append("High resolution might cause memory issues")
elif any(dim < 100 for dim in img.size):
issues.append("Low resolution might affect quality")
# Check color mode
if img.mode not in ['RGB', 'RGBA']:
issues.append(f"Unusual color mode: {img.mode}")
# Check file size
file_size = os.path.getsize(image_path) / (1024 * 1024) # MB
if file_size > 10:
issues.append(f"Large file size: {file_size:.1f}MB")
except Exception as e:
issues.append(f"Error analyzing image: {str(e)}")
return issues
class CustomPDF(FPDF):
"""Extended FPDF class with watermark and page numbers."""
def __init__(
self, orientation='P', unit='mm', format='A4',
watermark_text=None
):
super().__init__(orientation=orientation, unit=unit, format=format)
self.watermark_text = watermark_text
def header(self):
if self.watermark_text:
self.set_font('Arial', 'I', 30)
self.set_text_color(200, 200, 200) # Light gray
self.rotate(45)
self.text(40, 80, self.watermark_text)
self.rotate(0)
def footer(self):
if self.page_numbers:
self.set_y(-15)
self.set_font('Arial', 'I', 8)
self.set_text_color(128)
self.cell(0, 10, f'Page {self.page_no()}', 0, 0, 'C')
def merge_pdfs(pdf_files, output_path):
"""Merge multiple PDF files into one."""
from PyPDF2 import PdfMerger
merger = PdfMerger()
for pdf in pdf_files:
merger.append(pdf)
merger.write(output_path)
merger.close()
def heic_to_pdf_with_fallback(input_folder, output_pdf, compression_quality, supported_formats,
progress_bar, status_label, pdf_options=None, recursive=True, min_date=None,
skip_converted=True, delete_source=False):
"""Enhanced conversion function with new features."""
conversion_issues = {}
try:
# Load conversion history
history = load_conversion_history()
# Scan for files
files_info = scan_directory(input_folder, supported_formats, min_date)
if not files_info:
messagebox.showwarning("No Files", "No matching files found!")
return
# Filter already converted files
if skip_converted:
files_info = [f for f in files_info
if f['hash'] not in history or
history[f['hash']]['timestamp'] < f['modified']]
if not files_info:
messagebox.showinfo("Info", "All files are up to date!")
return
# Sort files by modification date
files_info.sort(key=lambda x: x['modified'])
# Pre-scan for potential issues
for file_info in files_info:
file_path = file_info['path']
issues = check_image_issues(file_path)
if issues:
conversion_issues[file_info['path']] = issues
logging.warning(f"Issues detected in {file_info['path']}: {issues}")
# Show preview of issues if any
if conversion_issues:
issues_text = "\n".join(
f"{fname}: {', '.join(issues)}"
for fname, issues in conversion_issues.items()
)
if not messagebox.askyesno("Potential Issues",
f"The following issues were detected:\n\n{issues_text}\n\nContinue anyway?"):
return
images = []
progress_bar["maximum"] = len(files_info)
progress_bar["value"] = 0
for i, file_info in enumerate(files_info):
file_path = file_info['path']
output_image_path = os.path.join(input_folder, f"converted_{os.path.splitext(os.path.basename(file_path))[0]}.jpg")
# Handle HEIC files
if file_path.lower().endswith(".heic"):
try:
# Attempt to convert HEIC locally
import pyheif
heif_file = pyheif.read(file_path)
image = Image.frombytes(
heif_file.mode, heif_file.size, heif_file.data,
"raw", heif_file.mode, heif_file.stride
)
if image.mode != "RGB":
image = image.convert("RGB")
image.save(output_image_path, "JPEG", quality=compression_quality)
except ImportError:
# Fallback to CloudConvert
status_label.config(text=f"Falling back to CloudConvert for {os.path.basename(file_path)}")
convert_heic_to_jpeg_with_cloudconvert(file_path, output_image_path)
else:
# Handle JPEG and PNG directly
image = Image.open(file_path)
if image.mode != "RGB":
image = image.convert("RGB")
image.save(output_image_path, "JPEG", quality=compression_quality)
images.append(Image.open(output_image_path))
progress_bar["value"] += 1
status_label.config(text=f"Processed: {os.path.basename(file_path)} ({i + 1}/{len(files_info)})")
progress_bar.update_idletasks()
# Create PDF with custom options
pdf_options = pdf_options or {}
orientation = pdf_options.get('orientation', 'P')
page_size = pdf_options.get('page_size', 'A4')
custom_size = pdf_options.get('custom_size', None)
watermark = pdf_options.get('watermark', None)
page_numbers = pdf_options.get('page_numbers', False)
if page_size == 'Custom' and custom_size:
pdf = CustomPDF(orientation=orientation, format=custom_size, watermark_text=watermark)
else:
pdf = CustomPDF(orientation=orientation, format=page_size, watermark_text=watermark)
pdf.page_numbers = page_numbers
# Process images
for img in images:
pdf.add_page()
# Calculate image placement
if orientation == 'P':
max_w = pdf.w - 20
max_h = pdf.h - 30
else:
max_w = pdf.w - 30
max_h = pdf.h - 20
# Scale image
img_w, img_h = img.size
ratio = min(max_w/img_w, max_h/img_h)
new_w = img_w * ratio
new_h = img_h * ratio
# Center image
x = (pdf.w - new_w) / 2
y = (pdf.h - new_h) / 2
temp_image_path = "temp_image.jpg"
img.save(temp_image_path, "JPEG", quality=compression_quality)
pdf.image(temp_image_path, x, y, new_w, new_h)
os.remove(temp_image_path)
pdf.output(output_pdf)
# Handle PDF merging if requested
if pdf_options.get('merge_files'):
merge_files = pdf_options['merge_files']
if merge_files:
temp_output = output_pdf
final_output = os.path.splitext(output_pdf)[0] + "_merged.pdf"
merge_pdfs([temp_output] + merge_files, final_output)
os.remove(temp_output) # Remove temporary PDF
output_pdf = final_output
messagebox.showinfo("Success", f"PDF created successfully: {output_pdf}")
logging.info(f"Successfully created PDF: {output_pdf}")
# Update conversion history
for file_info in files_info:
history[file_info['hash']] = {
'path': file_info['path'],
'timestamp': datetime.now().timestamp(),
'output': output_pdf
}
# Delete source files if requested
if delete_source:
for file_info in files_info:
try:
os.remove(file_info['path'])
logging.info(f"Deleted source file: {file_info['path']}")
except Exception as e:
logging.error(f"Failed to delete {file_info['path']}: {e}")
# Save updated history
save_conversion_history(history)
except Exception as e:
error_msg = f"Error during conversion: {str(e)}"
logging.error(error_msg, exc_info=True)
messagebox.showerror("Error", error_msg)
# Generate error report
report_path = os.path.join(LOG_DIR, f"error_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt")
with open(report_path, 'w') as f:
f.write(f"Error Report\n\nTimestamp: {datetime.now()}\n")
f.write(f"Error: {str(e)}\n\n")
f.write("Conversion Issues:\n")
for fname, issues in conversion_issues.items():
f.write(f"{fname}: {', '.join(issues)}\n")
messagebox.showinfo("Error Report",
f"An error report has been generated at:\n{report_path}")
finally:
progress_bar["value"] = 0
status_label.config(text="Ready")