Create a temporary upload service server side (by hacking demos/webserver.py) and client side with an angularjs service component.

This commit is contained in:
Emmanuel ROHEE 2014-08-14 18:38:42 +02:00
parent 30da8c81c7
commit f5973d8ddb
2 changed files with 71 additions and 1 deletions

View file

@ -2,9 +2,32 @@ import argparse
import BaseHTTPServer
import os
import SimpleHTTPServer
import cgi, logging
from daemonize import Daemonize
class SimpleHTTPRequestHandlerWithPOST(SimpleHTTPServer.SimpleHTTPRequestHandler):
UPLOAD_PATH = "upload"
"""
Accept all post request as file upload
"""
def do_POST(self):
path = os.path.join(self.UPLOAD_PATH, os.path.basename(self.path))
length = self.headers['content-length']
data = self.rfile.read(int(length))
with open(path, 'wb') as fh:
fh.write(data)
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
# Return the absolute path of the uploaded file
self.wfile.write('{"url":"/%s"}' % path)
def setup():
parser = argparse.ArgumentParser()
@ -19,7 +42,7 @@ def setup():
httpd = BaseHTTPServer.HTTPServer(
('', args.port),
SimpleHTTPServer.SimpleHTTPRequestHandler
SimpleHTTPRequestHandlerWithPOST
)
def run():