Skip to content

Instantly share code, notes, and snippets.

@kageurufu
Created August 4, 2016 15:44
Show Gist options
  • Save kageurufu/ad747faa447075c48204c6cad9114157 to your computer and use it in GitHub Desktop.
Save kageurufu/ad747faa447075c48204c6cad9114157 to your computer and use it in GitHub Desktop.
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