Added config and env support for settings

This commit is contained in:
Benjamyn Love 2020-10-12 21:24:20 +11:00
parent 0d6f4fe4e0
commit f7720d7409
3 changed files with 35 additions and 14 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
env/ env/
.vscode/

View File

@ -1,47 +1,63 @@
from flask import Flask, abort, request, send_file from flask import Flask, abort, request, send_file
from pprint import pprint from pprint import pprint
import os.path import os
import string import string
import random import random
useSpecialChars = False import configparser
basePath="/app"
domain = "https://upload.lovelynet.net" basePath = "/app"
# domain = "https://upload.lovelynet.net"
# secret = "thisistheshit"
letters = string.ascii_letters letters = string.ascii_letters
if useSpecialChars: if os.environ.get('UPDOMAIN') and os.environ.get('UPSECRET'):
letters = letters + string.punctuation domain = os.environ.get('UPDOMAIN')
secret = os.environ.get('UPSECRET')
else:
config = configparser.ConfigParser()
config.read('app/config.ini')
domain = config['VARS']['domain']
secret = config['VARS']['secret']
print(domain, secret)
# Functions # Functions
def randomName(nameLength, ext): def randomName(nameLength, ext):
name = "" name = ""
for x in range(0,nameLength): for x in range(0, nameLength):
num = random.randint(0,len(letters)) num = random.randint(0, len(letters))
#print(num)
name += letters[num - 1] name += letters[num - 1]
name = checkDuplicateName(name, ext) name = checkDuplicateName(name, ext)
return f"{name}.{ext}" return f"{name}.{ext}"
def checkDuplicateName(name, ext): def checkDuplicateName(name, ext):
if os.path.isfile(f'{basePath}/imgs/{name}.{ext}'): if os.path.isfile(f'{basePath}/imgs/{name}.{ext}'):
name = randomName(5, ext) name = randomName(5, ext)
return name return name
def checkFileExists(name): def checkFileExists(name):
if os.path.isfile(f'{basePath}/imgs/{name}'): if os.path.isfile(f'{basePath}/imgs/{name}'):
return True return True
return False return False
def saveIMG(imageData, name): def saveIMG(imageData, name):
imageData.save(f'{basePath}/imgs/' + name) imageData.save(f'{basePath}/imgs/' + name)
app = Flask(__name__) app = Flask(__name__)
app.config['DEBUG'] = True app.config['DEBUG'] = True
@app.route('/', methods=['POST', 'GET']) @app.route('/', methods=['POST', 'GET'])
def index(): def index():
try: try:
if 'thisistheshit' in request.headers['X-AUTH']: if secret in request.headers['X-AUTH']:
if request.form['type'] == 'img': if request.form['type'] == 'img':
name = randomName(5, "png") name = randomName(5, "png")
saveIMG(request.files['image.png'], name) saveIMG(request.files['image.png'], name)
@ -54,6 +70,7 @@ def index():
except: except:
return "" return ""
@app.route('/<path:path>') @app.route('/<path:path>')
def getImage(path): def getImage(path):
if (checkFileExists(path)): if (checkFileExists(path)):
@ -63,8 +80,8 @@ def getImage(path):
@app.errorhandler(404) @app.errorhandler(404)
def page_not_found(error): def page_not_found(error):
return "File not found", 404 return "File not found", 404
if __name__ == '__main__': # if __name__ == '__main__':
app.run(host='0.0.0.0') # app.run(host='0.0.0.0')

3
app/config.ini Normal file
View File

@ -0,0 +1,3 @@
[VARS]
domain = https://this.domain.tld
secret = secretcode