Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/electron/frontend/core/components/NWBFilePreview.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -48,17 +49,38 @@ 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() {
const isOnline = navigator.onLine;

return isOnline
? new Neurosift({
url: getURLFromFilePath(this.file, this.project),
url: this.neurosiftUrl,
fullscreen: false,
})
: until(
Expand Down
8 changes: 0 additions & 8 deletions src/electron/frontend/core/components/Neurosift.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
8 changes: 0 additions & 8 deletions src/pyflask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ def exception_handler(error: Exception) -> Dict[str, str]:
return {"message": str(error), "type": type(error).__name__}


@flask_app.route("/preview/<path:file_path>")
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/<int:index>")
def handle_get_file_request(index) -> Union[str, None]:
"""
Expand Down
1 change: 0 additions & 1 deletion src/pyflask/namespaces/neurosift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
Loading