Пример #1
0
def get_file():
    logging.info('get file')
    user = ctx.request.user
    if not user:
        raise APIError('Authencation:fail','token','token is wrong','-1')
    i = ctx.request.input(filetype='.jpg')

    logging.info('the file is %s'% (i.file))
    if not isinstance(i.file, MultipartFile):
        raise APIError('IO:error','file','read the file error','-1')
    logging.info('the token is %s' % i.token)
    file_name = next_id()+'.'+i.filetype
    file_path = os.path.join(image_path, file_name)
    logging.info('the out file is %s' % file_path)
    fw = open(file_path, 'wb')
    buf = i.file.file.readline()
    while buf != '':
        fw.write(buf)
        buf = i.file.file.readline()
    fw.close()
    image = Image(image_path=image_path_relative+file_name,user_id=user.id)
    image.insert()
    image.pop('image_path')
    image.errcode='0'
    return image