From 30b271a136e485d01ad09590561701a43baec60f Mon Sep 17 00:00:00 2001 From: rly Date: Mon, 12 May 2025 16:36:56 -0700 Subject: [PATCH] Fix the neurosift for preview stub files --- .../core/components/NWBFilePreview.js | 26 +++++++++++++++++-- .../frontend/core/components/Neurosift.ts | 8 ------ src/pyflask/app.py | 8 ------ src/pyflask/namespaces/neurosift.py | 1 - 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/electron/frontend/core/components/NWBFilePreview.js b/src/electron/frontend/core/components/NWBFilePreview.js index 43ead6138e..46cb7c8b93 100644 --- a/src/electron/frontend/core/components/NWBFilePreview.js +++ b/src/electron/frontend/core/components/NWBFilePreview.js @@ -1,8 +1,9 @@ import { LitElement, css, html } from "lit"; import { InspectorList } from "./InspectorList"; -import { Neurosift, getURLFromFilePath } from "./Neurosift"; +import { Neurosift } from "./Neurosift"; import { unsafeHTML } from "lit/directives/unsafe-html.js"; import { run } from "../../utils/run"; +import { baseUrl } from "../server/globals"; import { until } from "lit/directives/until.js"; import { InstanceManager } from "./InstanceManager"; @@ -48,9 +49,30 @@ class NWBPreviewInstance extends LitElement { super(); this.file = file; this.project = project; + this.neurosiftUrl = undefined; window.addEventListener("online", () => this.requestUpdate()); window.addEventListener("offline", () => this.requestUpdate()); + + // Register the file when the instance is created + this.registerFile(this.file); + } + + async registerFile(path) { + if (path) { + try { + // Enable access to the explicit file path + const result = await fetch(`${baseUrl}/files/${path}`, { + method: "POST", + }).then((res) => res.text()); + + // Set the URL for Neurosift + if (result) this.neurosiftUrl = result; + this.requestUpdate(); + } catch (error) { + console.error("Error registering file:", error); + } + } } render() { @@ -58,7 +80,7 @@ class NWBPreviewInstance extends LitElement { return isOnline ? new Neurosift({ - url: getURLFromFilePath(this.file, this.project), + url: this.neurosiftUrl, fullscreen: false, }) : until( diff --git a/src/electron/frontend/core/components/Neurosift.ts b/src/electron/frontend/core/components/Neurosift.ts index 3214396f19..9b1eb0d061 100644 --- a/src/electron/frontend/core/components/Neurosift.ts +++ b/src/electron/frontend/core/components/Neurosift.ts @@ -1,14 +1,6 @@ import { LitElement, css, html, CSSResult, TemplateResult } from "lit"; import { Loader } from "./Loader"; import { FullScreenToggle } from "./FullScreenToggle"; -import { baseUrl } from "../server/globals"; - -export function getURLFromFilePath(file: string, projectName: string): string { - const regexp = new RegExp(`.+(${projectName}.+)`); - const match = file.match(regexp); - if (!match) throw new Error(`File path ${file} does not contain project name ${projectName}`); - return `${baseUrl}/preview/${match[1]}`; -} export class Neurosift extends LitElement { static get styles(): CSSResult { diff --git a/src/pyflask/app.py b/src/pyflask/app.py index e2bc1d947a..2b02ca381b 100644 --- a/src/pyflask/app.py +++ b/src/pyflask/app.py @@ -71,14 +71,6 @@ def exception_handler(error: Exception) -> Dict[str, str]: return {"message": str(error), "type": type(error).__name__} -@flask_app.route("/preview/") -def send_preview(file_path): - """ - This endpoint is used to send a file in the stub folder by relative file path to the frontend. - """ - return send_from_directory(directory=STUB_SAVE_FOLDER_PATH, path=file_path) - - @flask_app.route("/files/") def handle_get_file_request(index) -> Union[str, None]: """ diff --git a/src/pyflask/namespaces/neurosift.py b/src/pyflask/namespaces/neurosift.py index 196eaad444..239013715a 100644 --- a/src/pyflask/namespaces/neurosift.py +++ b/src/pyflask/namespaces/neurosift.py @@ -11,7 +11,6 @@ import flask import flask_restx -from manageNeuroconv.info import STUB_SAVE_FOLDER_PATH neurosift_namespace = flask_restx.Namespace( name="neurosift", description="Handle file system communication with the " "standalone Neurosift preview page."