Skip to content

Commit 2e4fc0c

Browse files
authored
Merge pull request #984 from NeurodataWithoutBorders/fix_preview_neurosift
2 parents a7b6427 + 30b271a commit 2e4fc0c

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

src/electron/frontend/core/components/NWBFilePreview.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { LitElement, css, html } from "lit";
22
import { InspectorList } from "./InspectorList";
3-
import { Neurosift, getURLFromFilePath } from "./Neurosift";
3+
import { Neurosift } from "./Neurosift";
44
import { unsafeHTML } from "lit/directives/unsafe-html.js";
55
import { run } from "../../utils/run";
6+
import { baseUrl } from "../server/globals";
67

78
import { until } from "lit/directives/until.js";
89
import { InstanceManager } from "./InstanceManager";
@@ -48,17 +49,38 @@ class NWBPreviewInstance extends LitElement {
4849
super();
4950
this.file = file;
5051
this.project = project;
52+
this.neurosiftUrl = undefined;
5153

5254
window.addEventListener("online", () => this.requestUpdate());
5355
window.addEventListener("offline", () => this.requestUpdate());
56+
57+
// Register the file when the instance is created
58+
this.registerFile(this.file);
59+
}
60+
61+
async registerFile(path) {
62+
if (path) {
63+
try {
64+
// Enable access to the explicit file path
65+
const result = await fetch(`${baseUrl}/files/${path}`, {
66+
method: "POST",
67+
}).then((res) => res.text());
68+
69+
// Set the URL for Neurosift
70+
if (result) this.neurosiftUrl = result;
71+
this.requestUpdate();
72+
} catch (error) {
73+
console.error("Error registering file:", error);
74+
}
75+
}
5476
}
5577

5678
render() {
5779
const isOnline = navigator.onLine;
5880

5981
return isOnline
6082
? new Neurosift({
61-
url: getURLFromFilePath(this.file, this.project),
83+
url: this.neurosiftUrl,
6284
fullscreen: false,
6385
})
6486
: until(

src/electron/frontend/core/components/Neurosift.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { LitElement, css, html, CSSResult, TemplateResult } from "lit";
22
import { Loader } from "./Loader";
33
import { FullScreenToggle } from "./FullScreenToggle";
4-
import { baseUrl } from "../server/globals";
5-
6-
export function getURLFromFilePath(file: string, projectName: string): string {
7-
const regexp = new RegExp(`.+(${projectName}.+)`);
8-
const match = file.match(regexp);
9-
if (!match) throw new Error(`File path ${file} does not contain project name ${projectName}`);
10-
return `${baseUrl}/preview/${match[1]}`;
11-
}
124

135
export class Neurosift extends LitElement {
146
static get styles(): CSSResult {

src/pyflask/app.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@ def exception_handler(error: Exception) -> Dict[str, str]:
7171
return {"message": str(error), "type": type(error).__name__}
7272

7373

74-
@flask_app.route("/preview/<path:file_path>")
75-
def send_preview(file_path):
76-
"""
77-
This endpoint is used to send a file in the stub folder by relative file path to the frontend.
78-
"""
79-
return send_from_directory(directory=STUB_SAVE_FOLDER_PATH, path=file_path)
80-
81-
8274
@flask_app.route("/files/<int:index>")
8375
def handle_get_file_request(index) -> Union[str, None]:
8476
"""

src/pyflask/namespaces/neurosift.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import flask
1313
import flask_restx
14-
from manageNeuroconv.info import STUB_SAVE_FOLDER_PATH
1514

1615
neurosift_namespace = flask_restx.Namespace(
1716
name="neurosift", description="Handle file system communication with the " "standalone Neurosift preview page."

0 commit comments

Comments
 (0)