forked from TrisSherliker/buntool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·465 lines (399 loc) · 21.3 KB
/
app.py
File metadata and controls
executable file
·465 lines (399 loc) · 21.3 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
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
465
from flask import Flask, render_template, request, jsonify, send_file, session
from werkzeug.utils import secure_filename
import os
# import sys
import bundle as buntool
import convert as converter
import shutil
import logging
import tempfile
import uuid
from waitress import serve
import csv
#import boto3
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # file size limit in MB
app.logger.setLevel(logging.DEBUG)
APP_VERSION = "1.3.0"
# s3 = boto3.client('s3')
# bucket_name = os.environ.get('s3_bucket', 'your-default-bucket')
# def upload_to_s3(file_path, s3_key):
# s3.upload_file(file_path, bucket_name, s3_key)
# return f"s3://{bucket_name}/{s3_key}"
def is_running_in_lambda():
return 'AWS_LAMBDA_FUNCTION_NAME' in os.environ # seems to work?
if is_running_in_lambda():
logs_dir = os.path.join(tempfile.gettempdir(), 'logs')
else:
logs_dir = os.path.join('logs')
os.makedirs(logs_dir, exist_ok=True)
BUNDLES_DIR = os.path.join(tempfile.gettempdir(), 'buntool', 'bundles')
os.makedirs(BUNDLES_DIR, exist_ok=True)
def save_uploaded_file(file, directory, filename=None):
# Takes in a file object, the tmpfiles directory path, and an optional filename.
# passes the filename (supplied, or original) through secure_filename.
# creates a filepath by joining the tmp directory and filename.
# saves the file to the filepath
# returns the filepath if successful, None if not.
if file and file.filename:
filename = secure_filename(filename) or secure_filename(file.filename)
filepath = os.path.join(directory, filename)
file.save(filepath)
app.logger.debug(f"Saved file: {filepath}")
return filepath
return None
def strtobool(value: str) -> bool:
value = value.lower()
if value in ("y", "yes", "on", "1", "true", "t", "True", "enabled"):
return True
return False
def get_output_filename(bundle_title, case_name, timestamp, fallback="Bundle"):
# Takes in the bundle title, case name, and a timestamp.
# purpose is to guard against extra-long filenames.
# Returns the output filename picked among many fallback options.
output_file = f"{bundle_title}_{case_name}_{timestamp}.pdf"
if len(output_file) > 100:
# take first 20 chars of bundle title and add case name:
output_file = f"{bundle_title[:20]}_{case_name}_{timestamp}.pdf"
if len(output_file) > 100:
# take first 20 chars of bundle title, first 20 chars of case name, and add timestamp:
output_file = f"{bundle_title[:30]}_{case_name[:30]}_{timestamp}.pdf"
if len(output_file) > 100:
# this should never happen:
output_file = f"{fallback}_{timestamp}.pdf"
if len(output_file) > 100:
# this should doubly never happen:
output_file = f"{timestamp}.pdf"
return output_file
def synchronise_csv_index(uploaded_csv_path, filename_mappings):
# takes the path of the uploaded csv file and a dictionary of filename mappings (due to sanitising filenames of uploads).
# creates a new csv file with the same structure as the original, but with the filenames replaced with secure versions.
# returns the path of the new csv file.
sanitised_filenames_csv_path = uploaded_csv_path.replace('index_', 'securefilenames_index_')
app.logger.info(f"secure_csv_path: {sanitised_filenames_csv_path}")
try:
with open(uploaded_csv_path, 'r', newline='', encoding='utf-8') as infile:
with open(sanitised_filenames_csv_path, 'w', newline='', encoding='utf-8') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
# print content of input csv:
app.logger.debug(f"Reading input CSV:")
# try:
# header = next(reader)
# app.logger.debug(f"[APP]-- Read header: {header}")
# writer.writerow(header)
# app.logger.debug(f"[APP]-- Wrote header")
# except StopIteration:
# app.logger.error("[APP]-- CSV file is empty!")
# return
for row in reader:
app.logger.debug(f"Processing row: {row}")
try:
if row[0] == 'Filename' and row[2] == 'Page':
app.logger.debug(f"..Found header row")
writer.writerow(row)
continue
if len(row) > 3 and row[3] == '1':
app.logger.debug(f"..Found section marker row")
writer.writerow(row)
continue
original_upload_filename = row[0]
secure_name = filename_mappings.get(original_upload_filename)
if secure_name is None:
secure_name = secure_filename(original_upload_filename)
row[0] = secure_name
writer.writerow(row)
app.logger.debug(f"..Wrote processed file row: {row}")
except Exception as e:
app.logger.error(f"..Error processing row {row}: {str(e)}")
raise
except Exception as e:
app.logger.error(f"..Error in save_csv_index: {str(e)}")
raise
app.logger.info(f"..saved csv index as {sanitised_filenames_csv_path}")
return sanitised_filenames_csv_path
@app.route('/')
def index():
return render_template('index.html', version=APP_VERSION)
@app.route('/capabilities', methods=['GET'])
def capabilities():
"""Return what file types the server can handle, based on available tools."""
supported = ['.pdf'] + list(converter.IMAGE_EXTENSIONS)
if converter.has_libreoffice():
supported += list(converter.OFFICE_EXTENSIONS)
return jsonify({
"supported_extensions": sorted(supported),
"libreoffice_available": converter.has_libreoffice()
})
@app.route('/convert', methods=['POST'])
def convert_file():
"""Convert an uploaded non-PDF file to PDF and return it."""
if 'file' not in request.files:
return jsonify({"status": "error", "message": "No file provided"}), 400
file = request.files['file']
if not file.filename:
return jsonify({"status": "error", "message": "No filename"}), 400
if not converter.is_convertible(file.filename):
return jsonify({"status": "error", "message": f"Unsupported file type: {file.filename}"}), 400
try:
# Create a temp directory for this conversion
import tempfile as tf
conv_dir = tf.mkdtemp(prefix='buntool_conv_')
secure_name = secure_filename(file.filename)
input_path = os.path.join(conv_dir, secure_name)
file.save(input_path)
output_pdf_path = converter.convert_to_pdf(input_path, conv_dir)
response = send_file(output_pdf_path, mimetype='application/pdf',
as_attachment=True,
download_name=os.path.splitext(secure_name)[0] + '.pdf')
# Flag if this was a lossy fallback conversion
ext = converter.get_file_extension(file.filename)
if ext in converter.OFFICE_EXTENSIONS and not converter.has_libreoffice():
response.headers['X-Conversion-Warning'] = 'fallback'
return response
except Exception as e:
app.logger.error(f"Conversion error: {str(e)}")
return jsonify({"status": "error", "message": f"Conversion failed: {str(e)}"}), 500
@app.route('/create_bundle', methods=['GET', 'POST'])
def create_bundle():
if request.method == 'GET':
return render_template('index.html')
timestamp = buntool.datetime.now().strftime('%Y%m%d_%H%M%S')
session_id = str(uuid.uuid4())[:8]
user_agent = request.headers.get('User-Agent')
app.logger.debug(f"******************APP HEARS A CALL******************")
app.logger.debug(f"New session ID: {session_id} {user_agent}")
# check if csv has been passed:
# check whether input files are actually povided:
if 'files' not in request.files:
app.logger.error(f"Cannot create bundle: No files found in form submission")
return jsonify({"status": "error", "message": "No files found. Please add files and try again."})
try:
# Create temporary working directory in /tmp/tempfiles/{session_id}:
base_dir = tempfile.gettempdir() if is_running_in_lambda() else '.'
temp_dir = os.path.join(base_dir, 'tempfiles', session_id)
os.makedirs(temp_dir, exist_ok=True)
app.logger.debug(f"Temporary directory created: {temp_dir}")
# Add FileHandler for session-specific logging
logs_path = os.path.join(logs_dir, f'buntool_{session_id}.log')
session_file_handler = logging.FileHandler(logs_path)
session_file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s-%(levelname)s-[APP]: %(message)s')
session_file_handler.setFormatter(formatter)
app.logger.addHandler(session_file_handler)
# Get form data
# Ingest csv index
app.logger.debug(f"Ingesting form information...")
if request.files.get('csv_index'):
app.logger.info(f"..index file found in form submission")
app.logger.info(f"..index data: {request.files.get('csv_index')}")
else:
app.logger.info(f"..No CSV index found.")
# ingest other form data:
bundle_title = request.form.get('bundle_title', 'Bundle') if request.form.get('bundle_title') else 'Bundle'
case_name = request.form.get('case_name')
claim_no = request.form.get('claim_no')
page_num_align = request.form.get('page_num_align')
footer_font = request.form.get('footer_font')
index_font = request.form.get('index_font')
page_num_style = request.form.get('page_num_style')
footer_prefix = request.form.get('footer_prefix')
confidential_bool = request.form.get('confidential_bool')
date_setting = request.form.get('date_setting')
roman_for_preface = bool(strtobool(request.form.get('roman_for_preface')))
case_details = [bundle_title, claim_no, case_name]
zip_bool = True # option not implemented for GUI control.
bookmark_setting = request.form.get('bookmark_setting')
header_filename = bool(strtobool(request.form.get('header_filename', 'false')))
output_file = get_output_filename(bundle_title, case_name, timestamp, footer_prefix)
app.logger.debug(f"generated output filename: {output_file}")
# Save uploaded files
app.logger.debug(f"Gathering uploaded files...")
files = request.files.getlist('files')
# check whether files exceed max allowed overall size of MAX_CONTENT_LENGTH:
total_size = sum([f.content_length for f in files])
if total_size > app.config['MAX_CONTENT_LENGTH']:
app.logger.error(
f"Total size of files exceeds maximum allowed size: {total_size} > {app.config['MAX_CONTENT_LENGTH']}")
return jsonify({"status": "error",
"message": f"Total size of files exceeds maximum allowed size: {total_size} > {app.config['MAX_CONTENT_LENGTH']}"})
app.logger.debug(f"....{files}")
input_files = []
filename_mappings = {}
for file in files:
app.logger.debug(f"..Processing {file.filename}")
if file.filename:
secure_name = secure_filename(file.filename)
filename_mappings[file.filename] = secure_name
# app.logger.debug(f"[{session_id}-{timestamp}-APP]--filename_mappings: {filename_mappings}")
filepath = save_uploaded_file(file, temp_dir, secure_name)
if filepath:
input_files.append(filepath)
else:
app.logger.error(f"..No filename found for {file}")
if not os.path.exists(filepath):
app.logger.error(f"File not found at: {filepath}")
else:
app.logger.info(f"..File saved to: {filepath}")
# Save coversheet if provided
secure_coversheet_filename = None
if 'coversheet' in request.files and request.files['coversheet'].filename != '':
app.logger.debug(f"Coversheet found in form submission")
cover_file = request.files['coversheet']
# if cover_file and cover_file.filename:
# Generate a secure and unique filename for coversheet
secure_coversheet_filename = secure_filename(f'coversheet_{session_id}_{timestamp}.pdf')
coversheet_filepath = save_uploaded_file(cover_file, temp_dir, secure_coversheet_filename)
app.logger.debug(f"Coversheet path: {coversheet_filepath}")
else:
app.logger.debug(f"No coversheet found in form submission")
# Save CSV index
saved_csv_path = ""
if 'csv_index' in request.files:
app.logger.debug(f"CSV index found in form submission: {request.files['csv_index']}")
csv_file = request.files['csv_index']
if csv_file and csv_file.filename:
secure_csv_filename = secure_filename(f'index_{session_id}_{timestamp}.csv')
saved_csv_path = save_uploaded_file(csv_file, temp_dir, secure_csv_filename)
# if secure_csv_path:
# permanent_csv_path = os.path.join(temp_dir, secure_csv_filename)
sanitised_filenames_index_csv = synchronise_csv_index(saved_csv_path, filename_mappings)
else:
app.logger.debug(f"No CSV index found in form submission")
sanitised_filenames_index_csv = None
if not os.path.exists(saved_csv_path):
app.logger.error(f"CSV file not found at: {saved_csv_path}")
return jsonify(
{"status": "error", "message": f"Index data did not upload correctly. Session code: {session_id}"}), 400
else:
app.logger.debug(f"CSV saved to: {saved_csv_path}")
# Create bundle - main function call
try:
app.logger.info(f"Calling buntool.create_bundle with params:")
app.logger.info(f"....input_files: {input_files}")
app.logger.info(f"....output_file: {output_file}")
app.logger.info(f"....secure_coversheet_filename: {secure_coversheet_filename}")
app.logger.info(f"....sanitised_filenames_index_csv: {sanitised_filenames_index_csv}")
app.logger.info(f"....bundle_config elements:")
app.logger.info(f"........timestamp: {timestamp}")
app.logger.info(f"........case_details: {case_details}")
app.logger.info(f"........confidential_bool: {confidential_bool}")
app.logger.info(f"........zip_bool: {zip_bool}")
app.logger.info(f"........session_id: {session_id}")
app.logger.info(f"........user_agent: {user_agent}")
app.logger.info(f"........page_num_align: {page_num_align}")
app.logger.info(f"........index_font: {index_font}")
app.logger.info(f"........footer_font: {footer_font}")
app.logger.info(f"........page_num_style: {page_num_style}")
app.logger.info(f"........footer_prefix: {footer_prefix}")
app.logger.info(f"........date_setting: {date_setting}")
app.logger.info(f"........roman_for_preface: {roman_for_preface}")
app.logger.info(f"........temp_dir: {temp_dir}")
app.logger.info(f"........logs_dir: {logs_dir}")
app.logger.info(f"........bookmark_setting: {bookmark_setting}")
# Create BundleConfig instance
bundle_config = buntool.BundleConfig(
timestamp=timestamp,
case_details=case_details,
csv_string=None,
confidential_bool=confidential_bool,
zip_bool=zip_bool,
session_id=session_id,
user_agent=user_agent,
page_num_align=page_num_align,
index_font=index_font,
footer_font=footer_font,
page_num_style=page_num_style,
footer_prefix=footer_prefix,
date_setting=date_setting,
roman_for_preface=roman_for_preface,
temp_dir=temp_dir,
logs_dir=logs_dir,
bookmark_setting=bookmark_setting,
header_filename=header_filename
)
received_output_file, zip_file_path = buntool.create_bundle(
input_files,
output_file,
secure_coversheet_filename,
sanitised_filenames_index_csv,
bundle_config
)
# Copy both files to bundles folder
if os.path.exists(received_output_file):
final_output_path = os.path.join(BUNDLES_DIR, os.path.basename(received_output_file))
shutil.copy2(received_output_file, final_output_path)
app.logger.debug(f"Copied final PDF to: {final_output_path}")
else:
app.logger.error(f"PDF file not found at: {received_output_file}")
return jsonify({"status": "error",
"message": f"Error preparing PDF file for download. Session code: {session_id}"}), 500
if os.path.exists(zip_file_path):
final_zip_path = os.path.join(BUNDLES_DIR, os.path.basename(zip_file_path))
shutil.copy2(zip_file_path, final_zip_path)
app.logger.debug(f"Copied final ZIP to: {final_zip_path}")
else:
app.logger.error(f"ZIP file not found at: {zip_file_path}")
return jsonify(
{"status": "error", "message": f"Error creating ZIP archive. Session code: {session_id}"}), 500
return jsonify({
"status": "success",
"message": "Bundle created successfully!",
"bundle_path": final_output_path,
"zip_path": final_zip_path
})
except Exception as e:
app.logger.error(f"Fatal Error creating bundle: {str(e)}")
return jsonify(
{"status": "error", "message": "Fatal error creating bundle. Session code: {session_id}"}), 500
except Exception as e:
app.logger.error(f"Fatal Error in processing bundle: {str(e)}")
return jsonify(
{"status": "error", "message": f"Fatal error in creating bundle. Session code: {session_id}"}), 500
finally:
# Remove the session FileHandler to prevent duplicate logs
if session_file_handler and session_file_handler in app.logger.handlers:
app.logger.removeHandler(session_file_handler)
# try:
# # Upload logs to s3:
# # As mentioned in the frontend, logs are always uploaded to s3, and the
# # only private info they contain are is user agent and index
# # data (basically, filenames and titles):
# logsurl = upload_to_s3(logs_path, f'logs/{os.path.basename(logs_path)}')
# # By contrast, the zip file has all the input files and the output bundle
# # itself. It almost certainly contains private data.
# # In the /dev/ instance it's uploaded to s3 for testing purposes
# # and regularly cleaned out.It is disabled in stable version by commenting
# # out the following lin, but is left uncommented here for tranparency:
# #upload_to_s3(final_zip_path, f'bundles/{os.path.basename(final_zip_path)}')
# app.logger.debug(f"Upload to s3 successful: {logsurl}")
# except Exception as e:
# app.logger.error(f"Error uploading logs or zip to s3: {str(e)}")
# pass
@app.route('/download/bundle', methods=['GET'])
def download_bundle():
bundle_path = request.args.get('path')
if not bundle_path:
return jsonify({"status": "error", "message": f"Download Error: Bundle download path could not be found."}), 400
absolute_path = os.path.abspath(bundle_path)
if not os.path.exists(absolute_path):
return jsonify(
{"status": "error", "message": f"Download Error: bundle does not exist in expected location."}), 404
return send_file(absolute_path, as_attachment=True)
@app.route('/download/zip', methods=['GET'])
def download_zip():
zip_path = request.args.get('path')
if not zip_path:
return jsonify({"status": "error", "message": f"Download Error: Zip download path could not be found."}), 400
absolute_path = os.path.abspath(zip_path)
if not os.path.exists(absolute_path):
return jsonify({"status": "error", "message": f"Download Error: zip does not exist in expected location."}), 404
return send_file(absolute_path, as_attachment=True)
if __name__ == '__main__':
app.logger.debug(f"APP - BunTool v{APP_VERSION} - Server started on port 7001")
if converter.has_libreoffice():
app.logger.info(f"LibreOffice found at: {converter.LIBREOFFICE_PATH} — full document conversion enabled.")
else:
app.logger.warning("LibreOffice NOT found. DOCX conversion will use text-extraction fallback (not faithful). "
"Install LibreOffice for accurate document conversion: brew install --cask libreoffice")
serve(app, host='0.0.0.0', port=7001, threads=4, connection_limit=100, channel_timeout=120)