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: ` `, footer: ` ` } } 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: `
`, body: x, end: `
` } 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