def do_PUT(self, serv):
    alog(('fileapi access' + serv.path))
    qs = get_qs(serv.path)
    if (('accessToken' not in qs) or ('uploadToken' not in qs)):
        elog('fileapi: invalid tokens')
        serv.send_error(400)
        return
    tok = qs['accessToken'][0]
    utoken = qs['uploadToken'][0]
    userid = do_auth(tok)
    if (userid == None):
        elog('invalid authorization')
        serv.send_error(401)
        return
    status = UploadStatus(utoken)
    if status.invalid:
        elog('invalid upload token ', utoken)
        serv.send_error(401)
        return
    if ('Content-Range' not in serv.headers):
        elog(('missing header ' + json.dumps(serv.headers)))
        serv.send_error(400)
        return
    r = serv.headers['Content-Range'].strip()
    if (not r.startswith('bytes')):
        elog('malformed request 1')
        serv.send_error(400)
        return
    r = r[len('bytes'):].strip()
    r = r.split('/')
    if ((r == None) or (len(r) != 2)):
        elog('malformed request 2')
        serv.send_error(400)
        return
    try:
        max_size = int(r[1])
    except ValueError:
        elog('malformed request 3')
        serv.send_error(400)
        return
    r = r[0].split('-')
    if ((r == None) or (len(r) != 2)):
        elog('malformed request 4')
        serv.send_error(400)
        return
    try:
        r = [int(r[0]), int(r[1])]
    except ValueError:
        elog('malformed request 4')
        serv.send_error(400)
        return
    if ((r[0] < 0) or (r[1] < 0) or (r[0] >= max_size) or (r[1] >= max_size) or (r[0] > r[1])):
        elog('malformed request 5')
        serv.send_error(400)
        return
    if (status.size == (- 1)):
        status.size = max_size
    buflen = ((r[1] - r[0]) + 1)
    if (serv.rfile == None):
        elog(('serv.rfile was None! eek! ' + str(buflen)))
        serv.send_error(500)
        return
    buf = serv.rfile.read(buflen)
    if (len(buf) != buflen):
        elog('malformed request 6')
        serv.send_error(400)
        return
    if (r[0] == 0):
        mode = 'wb'
    else:
        mode = 'ab'
    status.file = open(status.realpath, mode)
    status.file.seek(r[0])
    status.file.write(buf)
    status.file.flush()
    status.file.close()
    status.commit()
    body = json.dumps({'success': True})
    body = bstr(body)
    serv.gen_headers('PUT', len(body), json_mimetype)
    serv.wfile.write(body)
def do_PUT(self, serv):
    alog(('fileapi access' + serv.path))
    qs = get_qs(serv.path)
    if (('accessToken' not in qs) or ('uploadToken' not in qs)):
        elog('fileapi: invalid tokens')
        serv.send_error(400)
        return
    tok = qs['accessToken'][0]
    utoken = qs['uploadToken'][0]
    userid = do_auth(tok)
    if (userid == None):
        elog('invalid authorization')
        serv.send_error(401)
        return
    status = UploadStatus(utoken)
    if status.invalid:
        elog('invalid upload token ', utoken)
        serv.send_error(401)
        return
    if ('Content-Range' not in serv.headers):
        elog(('missing header ' + json.dumps(serv.headers)))
        serv.send_error(400)
        return
    r = serv.headers['Content-Range'].strip()
    if (not r.startswith('bytes')):
        elog('malformed request 1')
        serv.send_error(400)
        return
    r = r[len('bytes'):].strip()
    r = r.split('/')
    if ((r == None) or (len(r) != 2)):
        elog('malformed request 2')
        serv.send_error(400)
        return
    try:
        max_size = int(r[1])
    except ValueError:
        elog('malformed request 3')
        serv.send_error(400)
        return
    r = r[0].split('-')
    if ((r == None) or (len(r) != 2)):
        elog('malformed request 4')
        serv.send_error(400)
        return
    try:
        r = [int(r[0]), int(r[1])]
    except ValueError:
        elog('malformed request 4')
        serv.send_error(400)
        return
    if ((r[0] < 0) or (r[1] < 0) or (r[0] >= max_size) or (r[1] >= max_size) or (r[0] > r[1])):
        elog('malformed request 5')
        serv.send_error(400)
        return
    if (status.size == (- 1)):
        status.size = max_size
    buflen = ((r[1] - r[0]) + 1)
    if (serv.rfile == None):
        elog(('serv.rfile was None! eek! ' + str(buflen)))
        serv.send_error(500)
        return
    buf = serv.rfile.read(buflen)
    if (len(buf) != buflen):
        elog('malformed request 6')
        serv.send_error(400)
        return
    if (r[0] == 0):
        mode = 'wb'
    else:
        mode = 'ab'
    status.file = open(status.realpath, mode)
    status.file.seek(r[0])
    status.file.write(buf)
    status.file.flush()
    status.file.close()
    status.commit()
    body = json.dumps({'success': True})
    body = bstr(body)
    serv.gen_headers('PUT', len(body), json_mimetype)
    serv.wfile.write(body)