Created
September 7, 2018 22:50
-
-
Save ramazanpolat/72ba5d00b5defbf73755db9e70d4c0a3 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 typing import Any | |
from apistar import App, Route, http | |
from apistar.codecs.multipart import MultiPartCodec | |
from werkzeug.datastructures import FileStorage | |
UPLOAD_FOLDER = os.path.join('.', 'uploads') | |
PORT = 8080 | |
def build_response(success: bool = False, error: str = None, data: Any = None): | |
result = { | |
'success': success, | |
'error': error, | |
'data': data | |
} | |
return result | |
def upload(request: http.Request, body: http.Body) -> dict: | |
try: | |
md = MultiPartCodec().decode(body, dict(request.headers)) | |
file_storage: FileStorage = md.get('file') | |
filename = os.path.join(UPLOAD_FOLDER, str(file_storage.filename)) | |
file_storage.save(filename) | |
return build_response(success=True, data="Upload complete") | |
except Exception as all_exceptions: | |
return build_response(error=str(all_exceptions)) | |
routes = [ | |
Route('/upload', method='POST', handler=upload), | |
] | |
app = App(routes=routes) | |
if __name__ == '__main__': | |
app.serve('0.0.0.0', 5000, debug=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment