#!/usr/bin/python3 import os import sys import time import string import random import datetime from datetime import timedelta from flask_qrcode import QRcode from werkzeug.utils import secure_filename from flask import Flask, render_template, Response, request, redirect, url_for, session, escape, send_from_directory, send_file, after_this_request from flask_fontawesome import FontAwesome from simplepam import authenticate from flask_navigation import Navigation from flask_dropzone import Dropzone ##Variables #Flask config flask_port=5006 flask_host='0.0.0.0' flask_thread=True flask_debug=False #Other random_size = 12 #path base for service lauch path = os.path.dirname(__file__) storage_path= path+'/storage/' #Flask init app = Flask(__name__,static_url_path='',static_folder='static',template_folder='templates') #Init QRcode QRcode(app) #Init FontAwsome fa = FontAwesome(app) #Init Nav nav = Navigation(app) nav.Bar('top', [ nav.Item('Index', 'index'), nav.Item('Upload', 'uploads'), ]) #DropZone app.config.update( UPLOADED_PATH= storage_path) dropzone = Dropzone(app) ##Functions def today_format(): x = datetime.datetime.now() return x.strftime("%d%m%y%H%M%S") def time_end(date_up,retention): retention = int(retention) tdate = datetime.datetime.strptime(date_up, '%d%m%y%H%M%S') end_date = tdate + timedelta(hours = retention) end_date = end_date.strftime("%d%m%y%H%M%S") return end_date def del_expired_files(): files=os.listdir(storage_path) for file in files: list_file=file.split("_") retention = ''.join(list_file[-2:-1]) if retention != '0': date_up = ''.join(list_file[-3:-2]) end_date = time_end(date_up,retention) end_date = datetime.datetime.strptime(end_date,"%d%m%y%H%M%S") actual_date = datetime.datetime.now() if end_date < actual_date: if os.path.exists(storage_path+file): os.remove(storage_path+file) def random_link(): res = ''.join(random.choices(string.ascii_letters+string.digits, k=random_size)) return res def list_files(): out_files=[] files=os.listdir(storage_path) for file in files: middle=[] list_file=file.split("_") singled = ''.join(list_file[-1:]) retention=''.join(list_file[-2:-1]) date_up=''.join(list_file[-3:-2]) link=''.join(list_file[-4:-3]) list_file_name=list_file[:-4] list_file_name='_'.join(list_file_name) end_date = time_end(date_up,retention) middle.append(list_file_name) middle.append(retention) middle.append(date_up) middle.append(link) middle.append(end_date) middle.append(request.url_root+"download/"+link) middle.append(singled) out_files.append(middle) return out_files ##Pages @app.route('/') def index(): if 'username' in session: del_expired_files() return render_template('index.html',loguser=session['username'],listdir=list_files()) return render_template('login.html',title="Login") @app.route('/down/') def down(link): del_expired_files() files=list_files() for file in files: if link == file[3]: filename=file[0]+"_"+file[3]+"_"+file[2]+"_"+file[1]+"_"+file[6] if file[6] == 'YES': @after_this_request def remove_file(reponse): os.remove(storage_path+filename) return reponse return send_file(storage_path+filename, download_name=secure_filename(file[0])) return render_template('nodown.html',title='No Download') @app.route('/download/') def download(link): del_expired_files() files=list_files() for file in files: if link == file[3]: filename=file[0] url_todl=request.url_root+"down/"+link return render_template('download.html',title="Download",filename=filename,url_todl=url_todl,qrcode=QRcode.qrcode(url_todl, mode="base64")) @app.route('/uploads') def uploads(): if 'username' in session: del_expired_files() return render_template('upload.html',loguser=session['username']) return render_template('login.html',title="Login") @app.route('/upload', methods = ['POST']) def upload(): del_expired_files() retention = request.form['retention'] r = ['on'] if request.form.getlist('uniqd') == r: singled = 'YES' else: singled = 'NO' my_files = request.files for item in my_files: uploaded_file = my_files.get(item) uploaded_file.filename = secure_filename(uploaded_file.filename) link = random_link() today = today_format() print(uploaded_file.filename+"_"+link+"_"+today+"_"+retention+"_"+singled) uploaded_file.save(os.path.join(app.config['UPLOADED_PATH'],uploaded_file.filename+"_"+link+"_"+today+"_"+retention+"_"+singled)) return render_template('upload.html',title="Upload") @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] if username == 'root': resultats='
Root not allowed
' return render_template('login.html', alertmessage=resultats) else: password = request.form['password'] if authenticate(str(username), str(password)): session['username'] = request.form['username'] return redirect(url_for('index')) else: resultats='Invalid Username/Password !
' return render_template('login.html', alertmessage=resultats) @app.route('/logout') def logout(): session.pop('username', None) return redirect(url_for('index')) app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' if __name__ == '__main__': app.run(host=flask_host, port=flask_port, threaded=flask_thread, debug=flask_debug, ssl_context='adhoc')