-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
51 lines (35 loc) · 1.16 KB
/
api.py
File metadata and controls
51 lines (35 loc) · 1.16 KB
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
from flask import Flask
from flask_cors import CORS
from flask_restful import Resource, Api, reqparse
import werkzeug
import random, string
app = Flask(__name__)
cors = CORS(app, resources={r"*": {"origins": "*"}})
api = Api(app)
signatures = []
class Uploader(Resource):
def post(self, signature):
if signature not in signatures:
return "Invalid signature", 400
parse = reqparse.RequestParser()
parse.add_argument(
'file',
type=werkzeug.datastructures.FileStorage,
location='files'
)
args = parse.parse_args()
upload_file = args['file']
upload_file.save("temp.txt")
with open('temp.txt', 'r') as f:
lines = f.readlines()
num_lines = len(lines)
return { 'numLines': num_lines }, 201
class Link(Resource):
def get(self):
x = ''.join(random.choices(string.ascii_letters + string.digits, k=16))
signatures.append(x)
return {'link': f'http://localhost:5000/upload/{x}'}
api.add_resource(Uploader, '/upload/<signature>')
api.add_resource(Link, '/link')
if __name__ == '__main__':
app.run(debug=True)