Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from flask import Flask,request,render_template, g, flash, send_from_directory
- import datetime
- import locale
- from sqlite3 import dbapi2 as sqlite3
- from werkzeug import secure_filename
- DATABASE = "./tmp/fileserver.db"
- TMP_FOLDER = "./tmp/"
- app = Flask(__name__)
- app.config.from_object(__name__)
- app.config.from_envvar('FLASKR_SETTINGS', silent=True)
- def connect_db():
- return sqlite3.connect(app.config['DATABASE'])
- @app.before_request
- def before_request():
- """Make sure we are connected to the database each request."""
- g.db = connect_db()
- @app.after_request
- def after_request(response):
- """Closes the database again at the end of the request."""
- g.db.close()
- return response
- @app.route("/")
- def show():
- return render_template('post_form.html',a="1")
- @app.route("/list")
- def list():
- cur = g.db.execute('select filename, filepath from entries order by id desc')
- entries = [dict(filename=row[0], filepath=row[1]) for row in cur.fetchall()]
- return render_template('show_list.html', entries=entries)
- @app.route('/add', methods=['GET', 'POST'])
- def upload():
- if request.method == 'POST':
- # get post data and save
- #print request.form["label"]
- #print "upload"
- filename = request.form["label"]
- filedata = request.form['upfile']
- d = datetime.datetime.today()
- timestamp = d.strftime("%Y%m%d%H%M%S")
- print timestamp
- filename = "./tmp/"+timestamp+secure_filename(filename)
- f=open(filename, "w")
- f.write(filedata.encode("utf-8"))
- f.close()
- g.db.execute('insert into entries (filename, filepath) values (?, ?)',
- [request.form['label'], filename])
- g.db.commit()
- #flash('New entry was successfully posted')
- return filedata
- @app.route('/tmp/<filename>')
- def uploaded_file(filename):
- return send_from_directory(app.config['TMP_FOLDER'],filename)
- if __name__ == "__main__":
- if len(sys.argv) >= 2 and sys.argv[1] == 'debug':
- print("starting debug mode")
- app.run(host='0.0.0.0', debug=True)
- else:
- app.run(host='0.0.0.0')
Add Comment
Please, Sign In to add comment