import os
import http.server as server
from constant import *


class My_http_server(server.SimpleHTTPRequestHandler):
    PORT = 8000
    blackboard = None

    def do_PUT(self):
        filename = os.path.basename("./data.txt")

        file_length = int(self.headers['Content-Length'])
        read = 0
        with open(filename, 'wb+') as output_file:
            while read < file_length:
                new_read = self.rfile.read(min(66556, file_length - read))
                read += len(new_read)
                output_file.write(new_read)
        self.send_response(201, 'Created')
        self.end_headers()
        reply_body = 'Saved "%s"\n' % filename
        self.wfile.write(reply_body.encode('utf-8'))
        self.blackboard.data[name_new] = filename
        self.blackboard.type_event = event_first_file
        self.blackboard.notify()
