File size: 3,540 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { pluginsApi } from "@/Api/Plugins";
import { messagesApi } from "@/Api/Messages";
import { ResponseType, fetcher } from "@/Core/Fetcher";
import { getUrlParam } from "@/Core/UrlParams";
import { FileInfo } from "./FileInfo";
import { validateShareCode } from "@/Plugins/Core/TemporaryShare/TemporaryShareUtils";
import { parseAndLoadMoleculeFile } from "@/FileSystem/LoadSaveMolModels/ParseMolModels/ParseMoleculeFiles";

/**
 * Open a remote file using its URL. TODO: Good to move this elsewhere, perhaps
 * in FS.
 *
 * @param {string} url  The URL of the file to open.
 */
export async function openRemoteFile(url: string) {
    if (url === null) {
        return;
    }

    // If url is four characters, assume it's a PDB
    if (url.length === 4) {
        url = `https://files.rcsb.org/download/${url}.pdb`;
    }

    try {
        // Fetch the file from the remote URL using axios
        const blob = await fetcher(url, { responseType: ResponseType.BLOB });

        // Create a File object from the Blob.
        const filename = url.split("/").pop() as string;
        const file = new File([blob], filename, { type: blob.type });

        // Since FileList objects are read-only and can't be directly
        // constructed, we'll need to create an object that mimics its
        // structure.
        const files = [file];

        // Now, pass the 'files' object to the 'openmolecules' plugin.
        pluginsApi.runPlugin("openmolecules", files);
    } catch (error) {
        messagesApi.popupError("Error fetching and opening the file. " + error);
    }
}

/**
 * Check if the URL has an open parameter and open the file if it does.
 * This function is called when the app is loaded.
 *
 * @returns {Promise<void>}  A promise that resolves when the file is opened.
 */
export async function checkIfUrlOpen(): Promise<void> {
    const params = [
        "open",
        "load",
        "src",
        "file",
        "url",
        "pdb",
        "smi",
        "smiles",
        "code",
    ];
    let urlValue: string | null = null;
    let paramName = "";

    for (const param of params) {
        urlValue = getUrlParam(param);
        if (urlValue !== null) {
            paramName = param;
            break;
        }
    }

    if (urlValue === null) {
        return;
    }

    // Check if the parameter value is a share code, regardless of the parameter name
    if (validateShareCode(urlValue)) {
        // NOTE: Below must be durrantlab.pitt.edu, not molmoda.org.
        const fullUrl = `https://durrantlab.pitt.edu/tmp/${encodeURIComponent(
            urlValue
        )}.molmoda`;
        await openRemoteFile(fullUrl);
        return;
    }

    // If not a share code, proceed with existing logic
    if (paramName !== "" && ["smi", "smiles"].includes(paramName)) {
        const smiles = urlValue as string;
        const fileInfo = new FileInfo({ name: "smiles.smi", contents: smiles });

        await parseAndLoadMoleculeFile({
            fileInfo,
            tag: null,
            addToTree: true
        });
    } else {
        // Not a SMILES string, treat as a direct URL to a file.
        await openRemoteFile(urlValue as string);
    }
}

/**
 * Set the URL with the open parameter.
 *
 * @param {string} url  The URL to set.
 */
// export function setUrlWithOpen(url: string) {
//     const urlParams = new U_RLSearchParams(window.location.search);
//     urlParams.set("open", url);
//     window.history.replaceState({}, "", `${window.location.pathname}?${urlParams}`);
// }