Skip to content

Instantly share code, notes, and snippets.

@AndyNovo

AndyNovo/app.py Secret

Last active December 10, 2021 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndyNovo/8bfccd8538469c57b9d8b9c777f47410 to your computer and use it in GitHub Desktop.
Save AndyNovo/8bfccd8538469c57b9d8b9c777f47410 to your computer and use it in GitHub Desktop.
#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
"""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