views.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from app import app
  2. from flask import Flask, request, redirect, url_for, send_from_directory, render_template
  3. import subprocess
  4. import order_bounding_boxes_in_each_block
  5. import redis
  6. import random
  7. import json
  8. import os
  9. #https://medium.com/@emerico/convert-pdf-to-image-using-python-flask-2864fb655e01
  10. #UPLOAD_FOLDER = '/Users/beatescheibel/Desktop/flask/uploads'
  11. UPLOAD_FOLDER = '/home/bscheibel/uploads_app'
  12. app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
  13. ALLOWED_EXTENSIONS = set(['pdf', 'png', 'jpg', 'jpeg', 'PDF'])
  14. def allowed_file(filename):
  15. return '.' in filename and \
  16. filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
  17. def convert_pdf(filename):
  18. PDFFILE = UPLOAD_FOLDER +"/" + filename
  19. subprocess.call(['pdftoppm', '-jpeg', '-singlefile',
  20. PDFFILE, '/home/bscheibel/uploads_app/out'])
  21. def extract_all(uuid, filename):
  22. order_bounding_boxes_in_each_block.main(uuid, UPLOAD_FOLDER + "/" + filename)
  23. subprocess.call(['python3','/home/bscheibel/PycharmProjects/dxf_reader/main.py', str(uuid), str(uuid)+"out.html",'localhost'])
  24. @app.route('/', methods=['GET', 'POST'])
  25. def upload_file():
  26. if request.method == 'POST':
  27. file = request.files['file']
  28. if file and allowed_file(file.filename):
  29. filename = file.filename
  30. file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
  31. uuid = random.randint(100,10000000)
  32. extract_all(uuid, filename)
  33. return redirect(url_for('uploaded_file', filename=filename, uuid=uuid))
  34. return '''
  35. <!doctype html>
  36. <title>Upload new File</title>
  37. <h1>Upload new File</h1>
  38. <form action="" method=post enctype=multipart/form-data>
  39. <p><input type=file name=file>
  40. <input type=submit value=Upload>
  41. </form>
  42. '''
  43. @app.route('/show/<filename>&<uuid>')
  44. def uploaded_file(filename, uuid):
  45. file_out = "out.jpg"
  46. if filename.endswith(".pdf") or filename.endswith(".PDF"):
  47. convert_pdf(filename)
  48. db = redis.Redis("localhost")
  49. iso = db.get(uuid+"dims")
  50. print(iso)
  51. isos = json.loads(db.get(uuid+"isos"))
  52. dims = json.loads(db.get(uuid+"dims"))
  53. return render_template('show_image.html', filename=file_out, isos=isos, dims=dims)
  54. else:
  55. filename = filename
  56. return render_template('show_image.html', filename=filename)
  57. @app.route('/uploads/<filename>')
  58. def send_file(filename):
  59. return send_from_directory(UPLOAD_FOLDER, filename)
  60. # No caching at all for API endpoints.
  61. @app.after_request
  62. def add_header(response):
  63. # response.cache_control.no_store = True
  64. response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
  65. response.headers['Pragma'] = 'no-cache'
  66. response.headers['Expires'] = '-1'
  67. return response
  68. @app.route('/generate/<name>')
  69. def generate(name):
  70. name = name.replace(" ","")
  71. url = name+".PDF"
  72. url1 = "./static/isos/"+url
  73. print(url1)
  74. try:
  75. file = send_from_directory("static/isos",url)
  76. return file
  77. except:
  78. return"Sorry file not found"
  79. @app.route('/show_results', methods=['POST'])
  80. def form_post():
  81. text = request.args.get("form")
  82. return render_template('display_results.html', text=text)