|
|
|
import os, uuid
|
|
|
|
from flask import (
|
|
|
|
Blueprint, flash, g, redirect, render_template, request, url_for, session
|
|
|
|
)
|
|
|
|
from werkzeug.exceptions import abort
|
|
|
|
|
|
|
|
from app.auth import login_required
|
|
|
|
from app.db import get_db
|
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
|
|
|
|
ALLOWED_EXTENSIONS = {'md'}
|
|
|
|
UPLOAD_FOLDER = os.getcwd() + '/app/uploads'
|
|
|
|
|
|
|
|
bp = Blueprint('index', __name__)
|
|
|
|
|
|
|
|
@bp.route('/')
|
|
|
|
@login_required
|
|
|
|
def index():
|
|
|
|
db = get_db()
|
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
|
|
def allowed_file(filename):
|
|
|
|
return '.' in filename and \
|
|
|
|
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
|
|
|
|
@bp.route('/upload', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def upload_file():
|
|
|
|
if request.method == 'POST':
|
|
|
|
# check if the post request has the file part
|
|
|
|
if 'file' not in request.files:
|
|
|
|
flash('No file part')
|
|
|
|
return redirect(request.url)
|
|
|
|
file = request.files['file']
|
|
|
|
# If the user does not select a file, the browser submits an
|
|
|
|
# empty file without a filename.
|
|
|
|
if file.filename == '':
|
|
|
|
flash('No selected file')
|
|
|
|
return redirect(request.url)
|
|
|
|
if file and allowed_file(file.filename):
|
|
|
|
filename = secure_filename(file.filename)
|
|
|
|
path = os.path.join(UPLOAD_FOLDER, filename)
|
|
|
|
file.save(path)
|
|
|
|
createMarkdownEntry(path)
|
|
|
|
os.remove(path)
|
|
|
|
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
return '''
|
|
|
|
<!doctype html>
|
|
|
|
<title>Upload new File</title>
|
|
|
|
<h1>Upload new File</h1>
|
|
|
|
<form method=post enctype=multipart/form-data>
|
|
|
|
<input type=file name=file>
|
|
|
|
<input type=submit value=Upload>
|
|
|
|
</form>
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
def createMarkdownEntry(path):
|
|
|
|
db = get_db()
|
|
|
|
title = os.path.basename(path)
|
|
|
|
title = title.split('.')
|
|
|
|
file = open(path)
|
|
|
|
data = file.read()
|
|
|
|
file.close()
|
|
|
|
uniqueID = str(uuid.uuid4())
|
|
|
|
qrCode = 'QR'
|
|
|
|
db.execute(
|
|
|
|
'INSERT INTO markdownFile (title, body, qrCode, uuid, creator) VALUES (?, ?, ?, ?, ?)',
|
|
|
|
(title[0], data, qrCode, uniqueID, session['user_id'])
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
return
|