-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
316 lines (234 loc) · 7.87 KB
/
main.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from base64 import b64encode
from datetime import datetime, timedelta
from flask import Flask, g, make_response, request, jsonify
from functools import wraps
import json
import os
import shutil
import tempfile
import time
from werkzeug.exceptions import (
BadRequest,
Conflict,
NotFound,
RequestedRangeNotSatisfiable,
Unauthorized)
from werkzeug.security import safe_str_cmp
class FlaskContext():
def __init__(self):
with open(CONFIGURATION_LOCATION) as f:
self.config = json.load(f)
app = Flask(__name__)
app.app_ctx_globals_class = FlaskContext
abspath = os.path.abspath(os.path.dirname(__file__))
CONFIGURATION_LOCATION = os.environ.get(
'HUSHFILE_CONFIGURATION_LOCATION',
os.path.join(abspath, 'config.json')
)
DEFAULT_FILE_EXPIRATION = os.environ.get('HUSHFILE_DEFAULT_FILE_EXPIRATION',
60*60*24*30) # 30 days
FILEID_LENGTH = 15
metadata_filename = 'metadata.json'
properties_filename = 'properties.json'
def generate_password(length=40):
return b64encode(os.urandom(42))[:40]
def read_properties_file(file_id):
properties_file = os.path.join(
g.config['data_path'],
file_id,
properties_filename)
with open(properties_file, 'r') as f:
return json.load(f)
def write_properties_file(file_id, properties):
properties_file = os.path.join(
g.config['data_path'],
file_id,
properties_filename)
with open(properties_file, 'w') as f:
json.dump(properties, f)
@app.before_request
def parse_request_data():
if request.method in ['POST', 'PUT']:
g.payload = request.get_json() or request.form
@app.route('/api/file', methods=['POST'])
def post_file():
payload = g.payload
filepath = tempfile.mkdtemp(dir=g.config['data_path'])
file_id = os.path.basename(filepath)
key = generate_password()
expires = payload.get('expires', None)
if not expires:
dt_expires = (
datetime.utcnow() + timedelta(seconds=DEFAULT_FILE_EXPIRATION))
expires = int(time.mktime(dt_expires.timetuple()))
properties = {
'key': key,
'expires': expires,
'chunks': 0,
}
if 'limit' in payload:
properties.update({
'limit': payload['limit'],
'downloads': 0,
})
write_properties_file(file_id, properties)
return jsonify({
'id': file_id,
'key': key,
})
def file_exists(file_id):
file = os.path.join(g.config['data_path'], file_id)
return os.path.exists(file) and os.path.isdir(file)
def chunk_exists(file_id, index):
chunk_file = os.path.join(g.config['data_path'], file_id, index)
return os.path.exists(chunk_file)
def require_file_exists(f):
app.logger.debug("require_file_exists invoked")
@wraps(f)
def check_file_exists(*args, **kwargs):
if not file_exists(kwargs['id']):
raise NotFound("The requested file could not be found")
app.logger.info("Found file %s", kwargs['id'])
return f(*args, **kwargs)
return check_file_exists
@app.route('/api/file/{string:id}', methods=['PUT'])
@require_file_exists
def put_file(id):
payload = g.payload
properties = read_properties_file(id)
limit = payload.get('limit', None)
if limit:
properties['limit'] = limit
elif 'limit' in properties:
del properties['limit']
expires = payload.get('expires', None)
if expires:
properties['expires'] = expires
write_properties_file(id, properties)
return jsonify({})
def require_uploadpassword(f):
@require_file_exists
@wraps(f)
def check_uploadpassword(*args, **kwargs):
properties = read_properties_file(kwargs['id'])
comparison = safe_str_cmp(
request.args.get('key'),
properties['upload_key'])
if not comparison:
raise Unauthorized()
return f(*args, **kwargs)
return check_uploadpassword
@app.route('/api/file/<string:id>/metadata', methods=['PUT'])
@require_uploadpassword
def put_file_metadata(id):
payload = g.payload
metadata_file = os.path.join(
g.config['data_path'],
id,
metadata_filename)
metadata = payload.copy()
uploaded_file = request.files['metadata']
if uploaded_file:
metadata.update({
'metadata': uploaded_file.read()
})
app.logger.debug("Writing metadata to %s", metadata_file)
with open(metadata_file, 'wb') as f:
json.dump(metadata, f)
return jsonify({'id': id})
@app.route('/api/file/<id>/exists', methods=['GET'])
@require_file_exists
def get_file_exists(id):
return jsonify({'id': id})
@app.route('/api/file/<id>/info', methods=['GET'])
@require_file_exists
def get_file_info(id):
'''Return public information about the file'''
properties = read_properties_file(id)
info = {
'id': id,
'chunks': properties['chunks'],
'size_total': properties['size_total'],
'complete': properties['complete'],
'expires': properties['expires'],
}
if 'limit' in properties:
info.update({
'limit': properties['limit'],
'downloads': properties.get('downloads', 0),
})
return jsonify(info)
@app.route('/api/file/<id>/metadata', methods=['GET'])
@require_file_exists
def get_file_metadata(id):
'''Return the metadata file along with its mac'''
metadata_file = os.path.join(g.config)
with open(metadata_file) as f:
return jsonify(f.read())
@app.route('/api/file/<id>/<index>', methods=['PUT'])
@require_uploadpassword
def put_chunk(id, index):
file = request.files['file']
chunk_file = os.path.join(g.config['data_path'], id, '%d.chunk' % index)
properties = read_properties_file(id)
if chunk_exists(id, index):
existing_size = os.path.getsize(chunk_file)
properties['total_size'] -= existing_size
file.save(chunk_file)
properties['total_size'] += os.path.getsize(chunk_file)
else:
file.save(chunk_file)
properties['chunks'] += 1
write_properties_file(id, properties)
return jsonify({
'id': id,
'index': index,
'size': os.path.getsize(chunk_file)
})
@app.route('/api/file/<id>/<index>', methods=['GET'])
@require_file_exists
def get_file(id, index):
chunk_file = os.path.join(g.config['data_path'], id, index)
if not chunk_exists(chunk_file):
raise RequestedRangeNotSatisfiable()
with open(chunk_file) as f:
return make_response(f.read())
def remove_file(id):
shutil.rmtree(os.path.join(g.config['data_path'], id))
return jsonify({
'id': id
})
@app.route('/api/file/<string:id>/complete', methods=['DELETE', 'POST'])
@require_uploadpassword
def post_file_complete(id):
properties = read_properties_file(id)
cmp_chunks = g.payload.get('chunks') == properties['chunks']
if request.method == 'POST' and not cmp_chunks:
raise RequestedRangeNotSatisfiable()
if properties['complete']:
raise Conflict()
if 'DELETE' == request.method:
delete_file(id)
else:
properties['complete'] = True
write_properties_file(id)
@app.route('/api/file/<id>', methods=['DELETE'])
@require_file_exists
def delete_file(id):
properties = read_properties_file(id)
if not safe_str_cmp(request.args.get('key'), properties['delete_key']):
raise Unauthorized()
remove_file(id)
@app.route('/api/info', methods=['GET'])
def get_serverinfo():
c = g.config
return jsonify({
'max_retention_hours': c.get('max_retention_hours', 24),
'max_filesize_bytes': c.get('max_filesize_bytes', 1073741824),
'max_chunksize_bytes': c.get('max_chunksize_bytes', 104857600),
})
if __name__ == "__main__":
app.debug = True
assert os.path.exists(CONFIGURATION_LOCATION) and \
os.path.isfile(CONFIGURATION_LOCATION)
app.run()