diff --git a/README.md b/README.md new file mode 100644 index 0000000..3287ee6 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Upload storage script \ No newline at end of file diff --git a/app.ini b/app.ini new file mode 100644 index 0000000..d10101b --- /dev/null +++ b/app.ini @@ -0,0 +1,14 @@ +[uwsgi] +socket=:3132 +chdir=/srv/WWW/s +module=app +master=true +threads=2 +processes=2 +callable=app +reload-mercy = 8 +cpu-affinity = 1 +max-requests = 2000 +limit-as = 512 +reload-on-as = 256 +reload-on-rss = 192 diff --git a/app.py b/app.py new file mode 100644 index 0000000..f88c754 --- /dev/null +++ b/app.py @@ -0,0 +1,72 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import os +import random +from datetime import datetime +from flask import Flask, render_template, redirect, url_for, request, send_from_directory, jsonify + +from flask.ext.restful import Api, Resource +from flask.ext.restful.reqparse import RequestParser, Argument +from werkzeug.datastructures import FileStorage + +app = Flask(__name__) +api = Api(app) +app.config.from_object(__name__) + +CURRENT_DIR = os.path.dirname(__file__) +UPLOAD_DIR = os.path.join(CURRENT_DIR, 'uploads') + +ALLOWED_EXTENSIONS = ['txt', 'pdf', 'doc', 'xls', + 'png', 'jpg', 'jpeg', 'gif', + 'avi', '3gp', 'flv', 'mp4', + 'rar', 'zip', 'gz', 'deb'] + +def upload(file): + if file and allowed_file(file.filename): + filename = generate_filename(file.filename) + file.save(os.path.join(UPLOAD_DIR, filename)) + return filename + +@app.route("/", methods=["POST", "GET"]) +def index(): + if request.method == 'POST': + filename = upload(request.files['file']) + if filename: + return redirect(url_for('uploaded_file', filename=filename)) + return render_template("index.html") + +class api_upload(Resource): + def post(self): + parser = RequestParser() + parser.add_argument('image', type=FileStorage, location='files') + parser.add_argument('file', type=FileStorage, location='files') + args = parser.parse_args() + file = args['image'] or args['file'] + if not file: + return jsonify({'error': 'not file for upload'}) + else: + filename = upload(file) + return jsonify({'href': '%s%s' % (request.url_root, filename)}) + +@app.route('/') +def uploaded_file(filename): + return send_from_directory(UPLOAD_DIR, filename) + +def generate_filename(filename, length=8): + 'Generates a unique file name containing a-z A-Z 0-9' + pool = range(48, 57) + range(65, 90) + range(97, 122) + name = ''.join(chr(random.choice(pool)) for _ in range(length)) + filename = "%s.%s" % (name, filename.rsplit('.', 1)[1]) + return filename + +def allowed_file(filename): + return '.' in filename \ + and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + +api.add_resource(api_upload, '/upload') + +if __name__ == "__main__": + if not os.path.exists(UPLOAD_DIR): + os.makedirs(UPLOAD_DIR, mode=0777) + app.run('0.0.0.0', debug=True) diff --git a/static/css/styles.css b/static/css/styles.css new file mode 100644 index 0000000..c9b8531 --- /dev/null +++ b/static/css/styles.css @@ -0,0 +1,29 @@ +body{ + width: 100%; + height: 100%; + position: absolute; + top: 0; + left: 0; + overflow: hidden; +} +.block { + width: 250px; + height: 250px; + position: absolute; + top: 0; + right: 0; + bottom: 0; + left: 0; + margin: auto; +} +#selectBtn{ + display: block; + top: 150px; + font-family: tahoma, Arial, sans-serif; + width: auto; + padding: 10px; + border: 1px dashed #BBB; + text-align: center; + background-color: #DDD; + cursor:pointer; +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..8549a15 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,30 @@ + + + + Storage + + + + + +
+
+
Select file
+
+ +
+
+
+ + \ No newline at end of file diff --git a/templates/index.html.orig b/templates/index.html.orig new file mode 100644 index 0000000..ca08cc7 --- /dev/null +++ b/templates/index.html.orig @@ -0,0 +1,14 @@ + + + + + Storage + + +
+

+ +

+ + +