2018-03-26 11:52:25 +02:00
|
|
|
from flask import request, flash
|
|
|
|
from app import app
|
|
|
|
import random, string, os
|
|
|
|
|
|
|
|
def getExtension(filename):
|
|
|
|
return filename.rsplit(".", 1)[1].lower() if "." in filename else None
|
2018-03-23 18:33:34 +01:00
|
|
|
|
|
|
|
def isFilenameAllowed(filename, exts):
|
2018-03-26 11:52:25 +02:00
|
|
|
return getExtension(filename) in exts
|
2018-03-23 18:33:34 +01:00
|
|
|
|
|
|
|
def shouldReturnJson():
|
|
|
|
return "application/json" in request.accept_mimetypes and \
|
|
|
|
not "text/html" in request.accept_mimetypes
|
2018-03-26 11:52:25 +02:00
|
|
|
|
|
|
|
def randomString(n):
|
|
|
|
return ''.join(random.choice(string.ascii_lowercase + \
|
|
|
|
string.ascii_uppercase + string.digits) for _ in range(n))
|
|
|
|
|
|
|
|
def doFileUpload(file, allowedExtensions, fileTypeName):
|
|
|
|
if not file or file is None or file.filename == "":
|
|
|
|
flash("No selected file", "error")
|
|
|
|
return None
|
|
|
|
|
|
|
|
ext = getExtension(file.filename)
|
|
|
|
if ext is None or not ext in allowedExtensions:
|
|
|
|
flash("Please upload load " + fileTypeName, "error")
|
|
|
|
return None
|
|
|
|
|
|
|
|
filename = randomString(10) + "." + ext
|
|
|
|
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
|
|
|
|
return "/uploads/" + filename
|