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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
const fs = require('fs')
const path = require('path')
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: `
<section id='header'>
<nav class='menulist' id='menu-list'>
<a class='list' href='/'>Home</a>
<a class='list' href='/about'>About</a>
<a class='list' href='/blog'>Blog</a>
<a class='list' href='https://git.fatalmatrix.xyz/'>Git Server</a>
<div class='close' id='close'>
<p>✕</p>
</div>
</nav>
<div class='menuIcon' id='menu-icon'>
<div></div>
<div></div>
<div></div>
</div>
</section>
`,
footer: `
<section id='footer'>
<div class='copied'>
<p>Copied to clipboard</p>
</div>
<div class='links'>
<p>Email <a href='mailto:mchaeldonald62@pm.me'>mchaeldonald62@pm.me</a></p>
<p>Monero <span class='footer-links'>89dARfwUGJNByHeir8bpCM4YJS2cTz58DNvu9fad1bpg7bfZs2H1QwtJpFghhJJatzRo8rnrE8AH5CAXbQHUpTp5FujsU1h</span></p>
<p>Bitcoin <span class='footer-links'>bc1qplmr9wx7ahcna5zmwlhhl5zcgs8n5nuxp74apw</span></p>
</div>
</section>
`
}
}
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 = {}
try {
data.header = `
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Welcome to my website!</title>
<meta name='description' content='${x.description}'>
<meta name='keywords' content='${x.keywords}'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body>
<section id='main'>
`,
data.footer = `
</section>
<script src='/js/purify.js'></script>
<script src='/js/main.js'></script>
</body>
</html>
`
} catch {
data.header = `
<html>
<head>
<title>Welcome to my website!</title>
<meta name='description' content=''>
<meta name='keywords' content=''>
<meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body>
<section id='main'>
`
data.footer = `
</section>
<script src='/js/purify.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`)
}
}
}
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
|