diff --git a/app.py b/app.py index e6e41a1..fa98df7 100644 --- a/app.py +++ b/app.py @@ -1,30 +1,56 @@ -from flask import Flask, render_template +import os +from flask import Flask, render_template, redirect, url_for from flask.globals import request from werkzeug.utils import secure_filename +from PyPDF2 import PdfFileReader + +# Constants +UPLOAD_FOLDER = './pdf/' + # Init an app object app = Flask(__name__) - -UPLOAD_PATH = './pdf/' +app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER @app.route('/') def index(): """ The landing page for the app """ - return render_template('index.html') # render foe template from ./templates + return render_template('index.html') -@app.route('/upload', methods=['GET', 'POST']) -def file_upload(): +@app.route('/quiz', methods=['GET', 'POST']) +def quiz(): + UPLOAD_STATUS = False if request.method == 'POST': try: uploaded_file = request.files['file'] - uploaded_file.save( - UPLOAD_PATH + secure_filename(uploaded_file.filename)) - uploaded = True - return render_template('index.html', uploaded=uploaded) + # Make directory to store uploaded files + if not os.path.isdir('./pdf'): + os.mkdir('./pdf') + # Save uploaded file + uploaded_file.save(os.path.join( + app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename))) + UPLOAD_STATUS = True + + # Identify file type and other stuff + uploaded_content = None + file_exten = uploaded_file.filename.rsplit('.', 1)[1].lower() + if file_exten == 'pdf': + # TODO: Move PDF2Text conversion to another file + print('PDF detected') + with open(os.path.join( + app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename)), 'rb') as pdf_file: + pdf_reader = PdfFileReader(pdf_file) + uploaded_content = pdf_reader.getPage(0).extractText() + print(uploaded_content) + else: + # Read text file and store contents + pass + except Exception as e: - return 'failed to upload file' + print(e) + return render_template('quiz.html', uploaded=UPLOAD_STATUS, pdftext=uploaded_content) if __name__ == "__main__": diff --git a/info.txt b/info.txt new file mode 100644 index 0000000..5639b0b --- /dev/null +++ b/info.txt @@ -0,0 +1 @@ +I have worked as a backend developer for creating REST API based based systems and worked with front-end integration for MEAN stack apps. For developing apps with a decoupled architecture. I have focused on security and performance to improve response time. I am well versed in Python, NodeJS and related tools like redis for building and maintaining a scalable architecture. I also have deployed production ready apps used by hundreds of people using AWS EC2, S3 and other cloud based tools. I frequently contribute to open source and support open source tools by creating issues, PRs for tools that I use frequently to try and give back to the community. \ No newline at end of file diff --git a/pdf/test.pdf b/pdf/test.pdf new file mode 100644 index 0000000..a16ab9d Binary files /dev/null and b/pdf/test.pdf differ diff --git a/pdf/test.txt b/pdf/test.txt deleted file mode 100644 index af27ff4..0000000 --- a/pdf/test.txt +++ /dev/null @@ -1 +0,0 @@ -This is a test file. \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 2f1c9d6..2f1f862 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ itsdangerous==1.1.0 Jinja2==2.11.2 MarkupSafe==1.1.1 pkg-resources==0.0.0 +PyPDF2==1.26.0 Werkzeug==1.0.1 diff --git a/templates/index.html b/templates/index.html index fae04c3..a7916ae 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,15 +4,13 @@
-Contents of your file
+