Ejemplo n.º 1
0
def load(req: Request, server):
    filename = re.sub('/load/', '', req.target)
    destination = os.path.join(ROOT_DIR, "tmp", "saved",
                               urllib.parse.unquote(filename))
    content_type = magic.Magic(mime=True).from_file(destination)
    res = Response.build_file_res(req, destination, content_type)
    return res
Ejemplo n.º 2
0
def handle(req: Request, server: Server):
    if req.path.startswith('/') and req.method == 'GET':
        rules = server.configurator._get_rules()

        path = urllib.parse.unquote(req.path)
        res = server.cache.get(path)
        if res:
            Logger.debug_info(f'Cache found for {path}')
            return res
        page = server.router.find_page_description(path, rules)
        destination = server.router.get_destination(path, rules, True)
        content_type = page.get_mime()
        if destination:
            if not content_type:
                mime = magic.Magic(mime=True)
                content_type = mime.from_file(destination)
                server.cache.close()
            res = Response.build_file_res(req,
                                          destination,
                                          content_type,
                                          add_headers=page.get_headers())
            if int(res.status) == 200:
                Logger.debug_info(f'Updating cache for {path}')
                server.cache.set(path, res, expire=EXPIRE, tag='data')
                server.cache.cull()
            return res
        raise Errors.NOT_FOUND
Ejemplo n.º 3
0
def show(req: Request, server):
    res = Response.build_file_res(req,
                                  os.path.join(ROOT_DIR, 'tmp', 'upload.html'),
                                  'text/html')

    dir_list = os.listdir(os.path.join(ROOT_DIR, "tmp", "saved"))
    body = f'{json.dumps(dir_list)}'.encode()
    headers = [
        ('Content-Type', f'application/json'),
        ('Content-Disposition', f'inline; filename=Echo query'),
        ('Content-Length', len(body)),
    ]
    return Response(200, 'OK', headers, body)