Example #1
0
    async def _get_image_raw_subs(self, fp: FilePath):
        img_path = App().abs_data_path(fp.path)
        data = App().file_reader.get_data(img_path, "preview")

        data = lz4.frame.compress(data)

        return Response(data, media_type="application/octet-stream")
Example #2
0
def init_server():
    static_files = os.path.abspath(
        os.path.join(os.path.dirname(os.path.realpath(__file__)),
                     "../static/"))

    app = FastAPI(debug=True)

    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    fb = FileBrowser()
    imr = ImageReader()

    app.include_router(fb.router, prefix="/api/file-browser")
    app.include_router(imr.router, prefix="/api/imageview")

    app.mount("/", StaticFiles(directory=static_files, html=True))

    sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins="*")
    sio_asgi_app = socketio.ASGIApp(sio, app, socketio_path="/api/socket.io")

    sio.register_namespace(ws.ConnectNS('/'))

    App.init_app(static_files, sio)

    return sio_asgi_app
Example #3
0
    async def _preload_image(self, fp: FilePath):
        img_path = App().abs_data_path(fp.path)
        hdr = App().file_reader.preload(img_path)

        # Call to get_image_data caches image data to enable parallel download of
        # full and subsampled data sets. The data is read once and fecthed by
        # simultaneoulsy by two different requests.

        return hdr
Example #4
0
    async def _get_image_raw_full(self, fp: FilePath):
        img_path = App().abs_data_path(fp.path)
        raw_data = App().file_reader.get_data(img_path, "raw")

        return Response(
            raw_data,
            #headers={
            #    "Content-Encoding": "deflate"
            #},
            media_type="application/octet-stream")
Example #5
0
def client(loop, aiohttp_client):
    """PyTest fixture for REST API"""
    app = server.init_server()
    braggy_app = App()

    # Force data path to test data directory
    dpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
    braggy_app.CONFIG["DATA_PATH"] = dpath

    client = TestClient(app)

    return client
Example #6
0
    async def _stop_follow(self):
        app = App()

        await App().sio.emit(
            "set-follow", {
                "follow": False,
                "wavelength": "null",
                "detector_distance": "null",
                "detector_radius": "null"
            })

        app.follow_stop()
        return {}
Example #7
0
    async def _show_image(self, path: str):
        app = App()

        if app.follow_enabled():
            img_path = App().abs_data_path(path)
            App().file_reader.preload(img_path)

            await App().sio.emit("show-image", {"path": img_path})
            status = 200
            response = {"msg": "OK"}
        else:
            response = {"msg": "FOLLOW NOT ENABLED"}
            status = 400

        return response, status
def test_get_image_raw_data(client):
    """Test root route"""
    bapp = App()
    imgpath = os.path.join(bapp.CONFIG["DATA_PATH"], "in16c_010001.cbf")

    params = {"path": imgpath}
    resp = client.post("/api/imageview/raw-full", json=params)

    assert resp.status_code == 200
    assert resp.headers.get("content-type") == 'application/octet-stream'
def test_get_image(client):
    """Test root route"""
    bapp = App()
    imgpath = os.path.join(bapp.CONFIG["DATA_PATH"], "in16c_010001.cbf")

    params = {"path": imgpath}
    resp = client.get("/api/imageview/image?%s" % urlencode(params, quote_via=quote_plus))

    assert resp.status_code == 200
    assert resp.headers.get("content-type") == 'image/png'
Example #10
0
def test_get_preload(client):
    """Test root route"""
    bapp = App()
    imgpath = os.path.join(bapp.CONFIG["DATA_PATH"], "in16c_010001.cbf")

    params = {"path": imgpath}
    resp = client.post("/api/imageview/preload", json=params)
    data = (resp.json())

    assert resp.status_code == 200
    assert 'Wavelength' in data['parsed_ext_hdr']
    assert 'wavelength' in data['braggy_hdr']
Example #11
0
    async def _start_follow(self, p: BLParams):
        app = App()

        await App().sio.emit(
            "set-follow", {
                "follow": True,
                "wavelength": p.wavelength,
                "detector_distance": p.detector_distance,
                "detector_radius": p.detector_radius
            })

        app.follow_set_bl_params(p.wavelength, p.detector_distance,
                                 p.detector_radius)
        app.follow_start()
        return {}
Example #12
0
 async def _get_list_dir(self):
     content = {"items": App().file_browser.list_dir("")}
     return content
Example #13
0
 async def _post_list_dir(self, path: FilePath):
     content = {"items": App().file_browser.list_dir(path.path)}
     return content
Example #14
0
    async def _get_image_bin(self, fp: FilePath):
        img_path = App().abs_data_path(fp.path)
        data = App().file_reader.get_data(img_path, "png")

        return Response(data, media_type="application/octet-stream")
Example #15
0
 async def _get_image(self, path: str):
     img_path = App().abs_data_path(path)
     data = App().file_reader.get_data(img_path, "png")
     return Response(data, media_type='image/png')
Example #16
0
    async def _get_image_hdr(self, fp: FilePath):
        img_path = App().abs_data_path(fp.path)
        _img_hdr = App().file_reader.get_hdr(img_path)

        return _img_hdr