summaryrefslogtreecommitdiffstats
path: root/cgi-bin/upload.py
blob: b3793e04bfe729728305a9a9e7b702ce1051cfdf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
import sys, os
from email.parser import BytesParser
from email.policy import default

print("Content-Type: text/html; charset=utf-8")
print("")

CONTENT_LENGTH = os.environ.get("CONTENT_LENGTH")
CONTENT_TYPE = os.environ.get("CONTENT_TYPE")
if CONTENT_TYPE is None or CONTENT_LENGTH is None:
    exit()

length = int(CONTENT_LENGTH)
raw = b"Content-Type: " + CONTENT_TYPE.encode() + b"\r\n\r\n" + sys.stdin.buffer.read(length)
msg = BytesParser(policy=default).parsebytes(raw)

for part in msg.iter_parts():
    name = part.get_filename()
    if name is None:
        continue
    with open(os.path.join("../uploads", os.path.basename(name)), "wb") as f:
        f.write(part.get_payload(decode=True))