Created
August 4, 2016 15:44
-
-
Save kageurufu/ad747faa447075c48204c6cad9114157 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import os | |
from flask import Flask, request | |
from io import BufferedReader | |
import magic | |
app = Flask(__name__) | |
@app.route("/<string:filename>", methods=['PUT']) | |
def upload(filename): | |
head = request.stream.read(1024) | |
mime = magic.from_buffer(head, mime=True) | |
length = len(head) | |
with open("uploads/{}".format(os.path.basename(filename)), 'wb') as f: | |
f.write(head) | |
while not request.stream.is_exhausted: | |
buf = request.stream.read(1024) | |
length += len(buf) | |
f.write(buf) | |
return "Wrote {} bytes to {}, type {}".format(length, filename, mime) | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment