:sparkles:🎨 Add project zip download and remove lib/app folder
This commit is contained in:
+134
@@ -0,0 +1,134 @@
|
||||
'use strict'
|
||||
|
||||
// Dependencies
|
||||
const express = require('express')
|
||||
const helmet = require('helmet')
|
||||
const http = require('http')
|
||||
const colors = require('colors')
|
||||
const path = require('path')
|
||||
|
||||
/**
|
||||
* Site class
|
||||
*/
|
||||
class Site
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
constructor(_config, _success, _alreadyRunning, _error)
|
||||
{
|
||||
this.config = _config
|
||||
this.success = _success
|
||||
this.alreadyRunning = _alreadyRunning
|
||||
this.error = _error
|
||||
|
||||
this.setExpress()
|
||||
this.setServer()
|
||||
}
|
||||
|
||||
/**
|
||||
* Set express
|
||||
* Start express and set controllers
|
||||
*/
|
||||
setExpress()
|
||||
{
|
||||
// Set up
|
||||
this.express = express()
|
||||
this.express.use(helmet())
|
||||
this.express.set('view engine', 'pug')
|
||||
this.express.set('views', path.join(__dirname, 'views'))
|
||||
}
|
||||
|
||||
/**
|
||||
* Set server
|
||||
*/
|
||||
setServer()
|
||||
{
|
||||
// Set up
|
||||
this.server = http.createServer(this.express)
|
||||
|
||||
// Error event
|
||||
this.server.on('error', (error) =>
|
||||
{
|
||||
// Server already running
|
||||
if(error.code === 'EADDRINUSE')
|
||||
{
|
||||
if(this.config.debug >= 1)
|
||||
{
|
||||
console.log('site'.green.bold + ' - ' + 'already running'.cyan)
|
||||
}
|
||||
|
||||
// Callback
|
||||
if(typeof this.alreadyRunning === 'function')
|
||||
{
|
||||
this.alreadyRunning()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
else
|
||||
{
|
||||
if(this.config.debug >= 1)
|
||||
{
|
||||
console.log('site'.green.bold + ' - ' + 'unknow error'.cyan)
|
||||
console.log(error)
|
||||
}
|
||||
|
||||
// Callback
|
||||
if(typeof this.error === 'function')
|
||||
{
|
||||
this.alreadyRunning()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Start
|
||||
this.server.listen(this.config.port, () =>
|
||||
{
|
||||
// URL
|
||||
if(this.config.debug >= 1)
|
||||
{
|
||||
console.log('site'.green.bold + ' - ' + 'started'.cyan)
|
||||
}
|
||||
|
||||
if(typeof this.success === 'function')
|
||||
{
|
||||
this.success()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Create project
|
||||
*/
|
||||
createProjectRoute(_project)
|
||||
{
|
||||
// Project route
|
||||
this.express.get('/' + _project.slug, (request, response) =>
|
||||
{
|
||||
response.send(`
|
||||
<a href="/${_project.slug}/download" download>download project</a>
|
||||
<a href="/${_project.slug}/files/empty.txt" download>download file</a>
|
||||
`)
|
||||
})
|
||||
|
||||
// Project download route
|
||||
this.express.get('/' + _project.slug + '/download', (request, response) =>
|
||||
{
|
||||
// Get zip buffer
|
||||
const zipBuffer = _project.getZipBuffer()
|
||||
|
||||
// Send
|
||||
response.writeHead(200, {
|
||||
'Content-Type': 'application/zip',
|
||||
'Content-Length': zipBuffer.length
|
||||
})
|
||||
response.write(zipBuffer, 'binary')
|
||||
response.end(null, 'binary')
|
||||
})
|
||||
|
||||
this.express.use('/' + _project.slug + '/files', express.static(_project.path))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Site
|
||||
Reference in New Issue
Block a user