File size: 3,007 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
115
import { TreeNode } from "@/TreeNodes/TreeNode/TreeNode";
import {
    getFormatInfoGivenType,
    IFormatInfo,
    molFormatInformation,
} from "./LoadSaveMolModels/Types/MolFormats";
import { IFileInfo } from "./Types";
import { getFileType } from "./FileUtils2";

/**
 * Class FileInfo
 */
export class FileInfo {
    name: string;
    contents: any;
    compressedName?: string;
    treeNode?: TreeNode;
    cachedConvertText: { [key: string]: string } = {};
    auxData: any;

    /**
     * The constructor.
     *
     * @param {IFileInfo} fileInfo  The IFileInfo object.
     */
    constructor(fileInfo: IFileInfo) {
        this.name = fileInfo.name;
        this.contents = fileInfo.contents;
        this.compressedName = fileInfo.compressedName;
        this.treeNode = fileInfo.treeNode;
    }

    /**
     * Serializes this FileInfo object.
     *
     * @returns {IFileInfo}  The serialized object.
     */
    public serialize(): IFileInfo {
        return {
            name: this.name,
            contents: this.contents,
            compressedName: this.compressedName,
            treeNode: this.treeNode,
            auxData: this.auxData,
        } as IFileInfo;
    }

    /**
     * Returns the file type.
     *
     * @returns {string | undefined}  The file type.
     */
    public getFileType(): string | undefined {
        return getFileType(this);
    }

    /**
     * Returns the format info.
     *
     * @returns {IFormatInfo | undefined}  The format info.
     */
    public getFormatInfo(): IFormatInfo | undefined {
        const typ = this.getFileType();
        if (typ === undefined) return;
        return getFormatInfoGivenType(typ);
    }

    /**
     * Assigns the extension based on the contents. Guesses the format.
     */
    public assignExtByContents() {
        const format = this.guessFormat();
        if (format) {
            this.name = this.name + "." + format.primaryExt;
        }
    }

    /**
     * Guesses the format.
     *
     * @returns {IFormatInfo | undefined}  The format info.
     */
    public guessFormat(): IFormatInfo | undefined {
        const contents = this.contents.trim();

        for (const formatID in molFormatInformation) {
            const format = molFormatInformation[formatID];
            if (format.validateContents === undefined) {
                // No way of validating this one.
                continue;
            }

            if (format === molFormatInformation.SMI) {
                // Waiting until the end to do SMI (it's the most permissive)
                continue;
            }

            if (format.validateContents(contents)) {
                return format;
            }
        }

        // Now try SMI
        if (
            molFormatInformation.SMI.validateContents &&
            molFormatInformation.SMI.validateContents(contents)
        ) {
            return molFormatInformation.SMI;
        }

        // Return undefined if format not found.
        return;
    }
}