summaryrefslogtreecommitdiff
path: root/source/readfile.js
blob: def56a188c1e0aa1c729cb06d9fcd340e54aa38b (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fs = require('fs')
const path = require('path')
const { SitemapStream, streamToPromise } = require('sitemap')
const { Readable } = require('stream')
const logger = require('./logger')()

/*
 * This class will output any files in the assets folder
 * Eg. `./assets/{foldername}/{filename}`
 *
 * You can also create a function that will output html
 * On the client side the `main.js` will use `innerHTML = {output}`
 */

class ReadFile {
    constructor(x) {
        try {
            this.object = {
                baseUrl: x.baseUrl, 
                mime: '',
                main: '',
                types: x.mime,
                layouts: {
                    header: fs.readFileSync('./views/layouts/header.html', 'utf-8'),
                    loading: fs.readFileSync('./views/layouts/loading.html', 'utf-8'),
                    footer: fs.readFileSync('./views/layouts/footer.html', 'utf-8')
                }
            }
            for (const key in this.object.layouts) {this.object.layouts[key] = this.#RemoveSpaces(this.object.layouts[key])}
        } catch(err) {
            logger.Error(err)
        }
    }

    GetMain(x) {
        const data = {
            header: `
                <!DOCTYPE html>
                <html lang='en'>
                <head>
                <title>${x.hasOwnProperty('title') ? x.title : 'Undefined'}</title>
                <meta name='description' content='${x.hasOwnProperty('description') ? x.description : 'Undefined'}'>
                <meta name='keywords' content='${x.hasOwnProperty('keywords') ? x.keywords : 'Undefined'}'>
                <meta name='viewport' content='width=device-width, initial-scale=1'>
                <link rel='stylesheet' type='text/css' href='/css/loading.css'>
                </head>
                <body>
                ${this.object.layouts.loading}
                <section id='main'>
            `,
            footer: `
                </section>
                <script src='/js/purify.js'></script>
                <script src='/js/loading.js'></script>
                <script src='/js/main.js'></script>
                </body>
                </html>
            `
        }

        return this.#RemoveSpaces(data.header + data.footer)
    }
    async Create(x) {
        try {
            return this.#SetData({data: x, mime: this.object.types})
        } catch(err) {
            try {
                return await x.then(output => {
                    return this.#SetData({data: output, mime: this.object.types})
                })
            } catch (err) {
                logger.Error(`${err.code}: Failed to create HTML`)
            }
        }
    }
    async Sitemap(x) {
        const object = {
            keys: x.keys,
            links: [],
            remove: ['/', '/blog'],
            stream: new SitemapStream({hostname: x.url})
        }

        Array.from(object.remove).forEach(url => {
            object.keys.splice(object.keys.indexOf(url), 1)
            object.links.push({url: url, changefreq: 'daily', priority: 1})
        })
        
        object.keys.forEach(link => object.links.push({url: link, changefreq: 'weekly', priority: 0.8}))
        
        return streamToPromise(Readable.from(object.links).pipe(object.stream)).then(data => {return data.toString()})
    }
    GetFile(x) {
        const path = x.split('/')
        const object = {
            file: fs.createReadStream(`./assets/${path[1]}/${path[2]}`).on('error', (err) => {
                logger.Error(err)
                return null
            }),
            mime: this.#GetFileType(path).then(output => {return output})
        }
        if (object.file) {return object} else {return null}
    }
    GetFavicon() {
        const object = {
            file: fs.createReadStream('./favicon/favicon.ico').on('error', (err) => {
                logger.Error(`${err.code}: Failed to get 'favicon.ico'`)
                return null
            }),
            mime: this.#GetFileType('favicon.ico')
        }
        if (object.file) {return object} else {return null}
    }
    GetRobots() {
        const object = {
            file: fs.createReadStream('./assets/robots/robots.txt').on('error', (err) => {
                logger.Error(`${err.code}: Failed to get 'robots.txt'`)
                return null
            }),
            mime: this.#GetFileType('robots.txt')
        }
        if (object.file) {return object} else {return null}
    }
    CreateBlog(x) {
        const data = {
            start: `
                <section id='blog'>
                <div class='blogitem'>
            `,
            body: x,
            end: `
                </div>
                </section>
            `
        }
        return this.#SetData(data)
    }

    #SetData(x) {
        const object = JSON.parse(JSON.stringify(this.object))
        object.mime = x.mime.html
        delete object.types
        object.layouts.data = this.#RemoveSpaces(x.data)
        return object
    }
    #RemoveSpaces(x) {return x.replace(/^(\n)\s+/gm, '$1')}
    async #GetFileType(x) {return this.object.types[path.extname(`./assets/${x[1]}/${x[2]}`).slice(1)] || 'text/plain'}
}

module.exports = ReadFile