-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.py
46 lines (32 loc) · 970 Bytes
/
app.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
import os
import re
from flask import Flask, abort, request
import store
GUID_RE = re.compile(
r"\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\Z"
)
app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 512
filestore = store.S3Store()
# Uncomment the following line for simpler local testing of this service
# filestore = store.LocalStore()
@app.route("/files/", methods=["POST"])
def add_file():
if request.headers.get("Content-Type") != "text/plain":
abort(422)
guid = request.headers.get("X-guid", "")
if not GUID_RE.match(guid):
abort(422)
filestore.save(guid, request.data)
return "", 201
@app.route("/files/<guid>", methods=["GET"])
def get_file(guid):
if not GUID_RE.match(guid):
abort(422)
try:
return filestore.read(guid), {"Content-Type": "text/plain"}
except store.NotFound:
abort(404)
@app.route("/", methods=["GET"])
def root():
return "", 204