forked from cinely/mule-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_backend.py
230 lines (175 loc) · 6.65 KB
/
example_backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from flask import Flask, request, render_template
from sqlalchemy import create_engine, Column, Integer
from sqlalchemy import String, DateTime, Text, MetaData
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from werkzeug import SharedDataMiddleware
from hashlib import sha1
from settings import DEBUG, AWS_ACCESS_KEY, AWS_SECRET, MIME_TYPE, BUCKET
from settings import ENGINE, PORT, CHUNK_SIZE
import os
import hmac
import base64
import time
import json
import random
## The Upload DB Model
engine = create_engine(ENGINE, convert_unicode=True)
db = scoped_session(sessionmaker(
autocommit=False, autoflush=True, bind=engine))
Base = declarative_base()
Base.query = db.query_property()
metadata = MetaData()
class Upload(Base):
__tablename__ = 'upload'
id = Column(Integer, primary_key=True)
filename = Column(String(256))
filesize = Column(String(64))
last_modified = Column(String(64))
upload_start = Column(DateTime)
last_information = Column(DateTime)
key = Column(String(256))
upload_id = Column(String(128))
chunks_uploaded = Column(Text)
Upload.metadata.create_all(bind=engine)
## Boilerplate
app = Flask(__name__)
app.debug = DEBUG
def init_db():
metadata.create_all(bind=engine)
init_db()
@app.teardown_request
def teardown_db(exception=None):
db.remove()
## Helper Functions
def _process_string(string):
return base64.b64encode(
hmac.new(AWS_SECRET,
string, sha1).digest())
def _http_date():
return time.strftime("%a, %d %b %Y %X %Z", time.localtime())
def _action_init(key, date=None):
date = date or _http_date()
return _process_string(
"POST\n\n\n\nx-amz-acl:public-read\nx-amz-date:{}\n/{}/{}?uploads".format(
date, BUCKET, key)), date
def _action_chunk(key, upload_id, chunk, date=None):
date = date or _http_date()
return _process_string(
"PUT\n\n{}\n\nx-amz-date:{}\n/{}/{}?partNumber={}&uploadId={}".format(
MIME_TYPE, date, BUCKET, key, chunk, upload_id)), date
def _action_list(key, upload_id, date=None):
date = date or _http_date()
return _process_string(
"GET\n\n\n\nx-amz-date:{}\n/{}/{}?uploadId={}".format(
date, BUCKET, key, upload_id)), date
def _action_end(key, upload_id, date=None):
date = date or _http_date()
return _process_string(
"POST\n\n{}\n\nx-amz-date:{}\n/{}/{}?uploadId={}".format(
MIME_TYPE, date, BUCKET, key, upload_id)), date
def _action_delete(key, upload_id, date=None):
date = date or _http_date()
return _process_string("DELETE\n\n\n\nx-amz-date:{}\n/{}/{}?uploadId={}".format(
date, BUCKET, key, upload_id)), date
## Actual backend
@app.route("/upload-backend/<action>/")
def upload_action(action):
key = request.args.get('key')
upload_id = request.args.get('upload_id')
chunk = request.args.get('chunk')
string = date = None
if action == 'chunk_loaded':
filename = request.args['filename']
filesize = request.args['filesize']
last_modified = request.args['last_modified']
chunk = int(request.args['chunk'])
if filesize > CHUNK_SIZE:
try:
u = db.query(Upload).filter(
Upload.filename == filename,
Upload.filesize == filesize,
Upload.last_modified == last_modified
).first()
assert u
chunks = set(map(int, u.chunks_uploaded.split(',')))
chunks.add(chunk)
u.chunks_uploaded = ','.join(map(str, chunks))
db.commit()
except AssertionError:
u = Upload(
filename=filename,
filesize=filesize,
last_modified=last_modified,
chunks_uploaded=str(chunk),
key=key,
upload_id=upload_id,
)
db.add(u)
db.commit()
return ''
if action == 'get_all_signatures':
date = _http_date()
list_signature, _ = _action_list(key, upload_id, date)
end_signature, _ = _action_end(key, upload_id, date)
delete_signature, _ = _action_delete(key, upload_id, date)
num_chunks = int(request.args['num_chunks'])
chunk_signatures = dict([(chunk, (_action_chunk(key, upload_id, chunk, date)))
for chunk in xrange(1, num_chunks + 1)])
return json.dumps({
'list_signature': [list_signature, date],
'end_signature': [end_signature, date],
'chunk_signatures': chunk_signatures,
})
if action == 'get_init_signature':
filename = request.args['filename']
filesize = request.args['filesize']
last_modified = request.args['last_modified']
try:
assert 'force' not in request.args
u = db.query(Upload).filter(
Upload.filename == filename,
Upload.filesize == filesize,
Upload.last_modified == last_modified
).first()
assert u
string, date = _action_init(u.key)
return json.dumps({
"signature": string,
"date": date,
"key": u.key,
"upload_id": u.upload_id,
"chunks": map(int, u.chunks_uploaded.split(','))
})
except AssertionError:
db.query(Upload).filter(
Upload.filename == filename,
Upload.filesize == filesize,
Upload.last_modified == last_modified
).delete()
db.commit()
string, date = _action_init(key)
elif action == 'get_chunk_signature':
string, date = _action_chunk(key, upload_id, chunk)
elif action == 'get_list_signature':
string, date = _action_list(key, upload_id)
elif action == 'get_end_signature':
string, date = _action_end(key, upload_id)
elif action == 'get_delete_signature':
string, date = _action_delete(key, upload_id)
return json.dumps({
'signature': string,
'date': date,
})
## Static files (debugging only)
@app.route("/")
def index():
return render_template('index.html', aws_access_key=AWS_ACCESS_KEY,
mime_type=MIME_TYPE, bucket=BUCKET,
key=str(random.randint(1, 1000000)))
if app.debug:
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/': os.path.join(os.path.dirname(__file__), '')
})
if __name__ == "__main__":
app.run(host='0.0.0.0', port=PORT)