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
|
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: this.#Build(),
types: x.mime,
layouts: {
header: `
<section id='header'>
<nav class='menulist' id='menu-list'>
<img src='' alt='logo'/>
<a class='list' href='/'>Home</a>
<a class='list' href='/about'>About</a>
<a class='list' href='/blog'>Blog</a>
<a class='list' href='/contact'>Contact</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'>
<nav>
<a href='/'>Home</a>
<a href='/about'>About</a>
<a href='/contact'></a>
</nav>
</section>
`
}
}
for (const key in this.object.layouts) {this.object.layouts[key] = this.#RemoveSpaces(this.object.layouts[key])}
} catch(err) {
logger.Error(err)
}
}
GetMain() {return this.object.main}
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) {
//console.log(x)
}
}
}
GetFile(x) {
const path = x.split('/')
const object = {
file: fs.createReadStream('./assets/' + path[1] + '/' + path[2]).on('error', (err) => {
logger.Error(err)
return false
}),
mime: this.#GetFileType(path).then(output => {return output})
}
if (object.file) {return object} else {return false}
}
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
}
async #GetFileType(x) {return this.object.types[path.extname('./assets/'+ x[1] + '/' + x[2]).slice(1)] || 'text/plain'}
#Build() {return fs.readFileSync('./views/layouts/header.html', 'utf-8') + fs.readFileSync('./views/layouts/footer.html', 'utf-8')}
#RemoveSpaces(x) {return x.replace(/(\n)\s+/g, '$1')}
}
module.exports = ReadFile
|