diff --git a/app.py b/app.py index fa98df7..4b159c9 100644 --- a/app.py +++ b/app.py @@ -2,7 +2,7 @@ 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 +from workers import pdf2text # Constants UPLOAD_FOLDER = './pdf/' @@ -13,44 +13,39 @@ app = Flask(__name__) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER -@app.route('/') +@ app.route('/') def index(): """ The landing page for the app """ return render_template('index.html') -@app.route('/quiz', methods=['GET', 'POST']) +@ app.route('/quiz', methods=['GET', 'POST']) def quiz(): + """ Handle upload and conversion of file + other stuff """ + UPLOAD_STATUS = False + + # Make directory to store uploaded files, if not exists + if not os.path.isdir('./pdf'): + os.mkdir('./pdf') + if request.method == 'POST': try: + # Retrieve file from request uploaded_file = request.files['file'] - # 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_path = os.path.join( + app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename)) 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 - + # Save uploaded file + uploaded_file.save(file_path) + # Get contents of file + uploaded_content = pdf2text(file_path, file_exten) + # File upload + convert success + if uploaded_content is not None: + UPLOAD_STATUS = True except Exception as e: print(e) - return render_template('quiz.html', uploaded=UPLOAD_STATUS, pdftext=uploaded_content) + return render_template('quiz.html', uploaded=UPLOAD_STATUS) if __name__ == "__main__": diff --git a/pdf/test.pdf b/pdf/test.pdf deleted file mode 100644 index a16ab9d..0000000 Binary files a/pdf/test.pdf and /dev/null differ diff --git a/templates/quiz.html b/templates/quiz.html index 5f728c9..6779e23 100644 --- a/templates/quiz.html +++ b/templates/quiz.html @@ -11,10 +11,6 @@ {% if uploaded == true %}

Your file was uploaded successfully

Your quiz will appear here

- Contents of your file -
-
- {{ pdftext }} {% else %}

Could not upload file

{% endif %} diff --git a/workers.py b/workers.py new file mode 100644 index 0000000..996f02c --- /dev/null +++ b/workers.py @@ -0,0 +1,21 @@ +from PyPDF2 import PdfFileReader + + +def pdf2text(file_path, file_exten) -> str: + """ Converts a given file to text content """ + + _content = None + + # Identify file type and get its contents + if file_exten == 'pdf': + with open(file_path, 'rb') as pdf_file: + _pdf_reader = PdfFileReader(pdf_file) + _content = _pdf_reader + print('PDF operation done!') + + elif file_exten == 'txt': + with open(file_path, 'r') as txt_file: + _content = txt_file.read() + print('TXT operation done!') + + return _content