summaryrefslogtreecommitdiff
path: root/source/readfile.js
diff options
context:
space:
mode:
Diffstat (limited to 'source/readfile.js')
-rw-r--r--source/readfile.js109
1 files changed, 109 insertions, 0 deletions
diff --git a/source/readfile.js b/source/readfile.js
new file mode 100644
index 0000000..c3970f6
--- /dev/null
+++ b/source/readfile.js
@@ -0,0 +1,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>&#10005;</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
+