You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.0 KiB
66 lines
2.0 KiB
2 years ago
|
import os, uuid
|
||
|
from flask import (
|
||
|
Blueprint, flash, g, redirect, render_template, request, url_for, session
|
||
|
)
|
||
|
from app.auth import login_required
|
||
|
from app.db import get_db
|
||
|
from werkzeug.utils import secure_filename
|
||
|
|
||
|
from app.qrCode import createQrCode
|
||
|
|
||
|
|
||
|
bp = Blueprint('upload', __name__, url_prefix='/upload')
|
||
|
|
||
|
ALLOWED_EXTENSIONS = {'md'}
|
||
|
UPLOAD_FOLDER = os.getcwd() + '/app/static/uploads'
|
||
|
|
||
|
@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 render_template('upload.html')
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
def allowed_file(filename):
|
||
|
return '.' in filename and \
|
||
|
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||
|
|
||
|
|
||
|
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())
|
||
|
base_uri = request.url_root
|
||
|
qrData = base_uri + 'viewfile/' + uniqueID
|
||
|
createQrCode(qrData, uniqueID)
|
||
|
qrCode = qrData
|
||
|
db.execute(
|
||
|
'INSERT INTO markdownFile (title, body, qrCode, uuid, creator) VALUES (?, ?, ?, ?, ?)',
|
||
|
(title[0], data, qrCode, uniqueID, session['user_id'])
|
||
|
)
|
||
|
db.commit()
|
||
|
return
|