summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2022-06-26 12:12:27 +0200
committerAnton Khirnov <anton@khirnov.net>2022-06-26 12:12:27 +0200
commit479e11d7d3a73c3ea25effcbb42e1527bd59049d (patch)
tree7e20e546c182302777b517575c5d13d428c9f9d6
parent343eeaa39f29df311b5c4cdc14f83cedbe2c21ca (diff)
Do not expect the reuquest to be form-encoded.HEADmaster
Just store the request payload as-is. Allows simplifying uploaders.
-rwxr-xr-xfhost.py24
1 files changed, 9 insertions, 15 deletions
diff --git a/fhost.py b/fhost.py
index 9910773..7e7ac60 100755
--- a/fhost.py
+++ b/fhost.py
@@ -156,12 +156,11 @@ def in_upload_bl(addr):
return False
-def store_file(f, addr):
+def store_file(r, addr):
if in_upload_bl(addr):
return "Your host is blocked from uploading files.\n", 451
- data = f.stream.read()
- digest = sha256(data).hexdigest()
+ digest = sha256(r.data).hexdigest()
existing = File.query.filter_by(sha256=digest).first()
if existing:
@@ -172,7 +171,7 @@ def store_file(f, addr):
if not os.path.exists(epath):
with open(epath, "wb") as of:
- of.write(data)
+ of.write(r.data)
if existing.nsfw_score == None:
if app.config["NSFW_DETECT"]:
@@ -184,12 +183,12 @@ def store_file(f, addr):
return existing.geturl()
else:
- guessmime = mimedetect.from_buffer(data)
+ guessmime = mimedetect.from_buffer(r.data)
- if not f.content_type or not "/" in f.content_type or f.content_type == "application/octet-stream":
+ if not r.content_type or not "/" in r.content_type or r.content_type == "application/octet-stream":
mime = guessmime
else:
- mime = f.content_type
+ mime = r.content_type
if mime in app.config["FHOST_MIME_BLACKLIST"] or guessmime in app.config["FHOST_MIME_BLACKLIST"]:
abort(415)
@@ -197,7 +196,7 @@ def store_file(f, addr):
if mime.startswith("text/") and not "charset" in mime:
mime += "; charset=utf-8"
- ext = os.path.splitext(f.filename)[1]
+ ext = os.path.splitext(r.base_url)[1]
if not ext:
gmime = mime.split(";")[0]
@@ -215,7 +214,7 @@ def store_file(f, addr):
spath = getpath(digest)
with open(spath, "wb") as of:
- of.write(data)
+ of.write(r.data)
if app.config["NSFW_DETECT"]:
nsfw_score = nsfw.detect(spath)
@@ -287,12 +286,7 @@ def dump_urls(start=0):
@app.route("/", methods=["GET", "POST"])
def fhost():
if request.method == "POST":
- sf = None
-
- if "file" in request.files:
- return store_file(request.files["file"], request.remote_addr)
-
- abort(400)
+ return store_file(request, request.remote_addr)
else:
return "Nothing to see here"