-
-
Save AndyNovo/8bfccd8538469c57b9d8b9c777f47410 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#service running at - 159.203.163.82:8080 | |
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.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Provides a filestore. | |
There is not intended to be any vulnerability contained within this code. | |
""" | |
import os | |
class NotFound(Exception): | |
pass | |
class LocalStore: | |
def __init__(self): | |
import tempfile | |
self.upload_directory = tempfile.mkdtemp() | |
def read(self, key): | |
filepath = os.path.join(self.upload_directory, key) | |
try: | |
with open(filepath, "rb") as fp: | |
return fp.read() | |
except FileNotFoundError: | |
raise NotFound | |
def save(self, key, data): | |
with open(os.path.join(self.upload_directory, key), "wb") as fp: | |
fp.write(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment