uploadboi/app/app.py
2020-10-12 21:30:17 +11:00

87 lines
2.0 KiB
Python

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