This commit is contained in:
3err0 2019-05-18 13:55:30 +08:00
parent b01ab47a63
commit fbeb9b3f67
6 changed files with 160 additions and 0 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
Upload storage script

14
app.ini Normal file
View File

@ -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

72
app.py Normal file
View File

@ -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('/<filename>')
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)

29
static/css/styles.css Normal file
View File

@ -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;
}

30
templates/index.html Normal file
View File

@ -0,0 +1,30 @@
<html>
<head>
<meta charset="utf-8" />
<title>Storage</title>
<link rel="stylesheet" href="static/css/styles.css" />
<link rel="icon" href="/static/images/favicon.png">
</head>
<script type="text/javascript">
function getFile(){
document.getElementById("upfile").click();
}
function sub(obj){
var file = obj.value;
var fileName = file.split("\\");
document.getElementById("selectBtn").innerHTML = fileName[fileName.length-1];
document.myForm.submit();
event.preventDefault();
}
</script>
<body>
<form action="" method=post enctype=multipart/form-data name=myForm>
<div class="block">
<div id="selectBtn" onclick="getFile()">Select file</div>
<div style='height: 0px; width: 0px;overflow:hidden;'>
<input id="upfile" type="file" name="file" value="upload" onchange="sub(this)"/>
</div>
</div>
</form>
</body>
</html>

14
templates/index.html.orig Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Storage</title>
</head>
<body>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value="Загрузить">
</form>
</body>
</html>