🐛 Fix corrupt zip and remove project download on server

This commit is contained in:
brunosimon
2017-12-25 17:49:53 +01:00
parent 3d93205750
commit 2620b00ffe
23 changed files with 148 additions and 60 deletions
+5 -5
View File
@@ -2,7 +2,7 @@
const slug = require('slug')
const Files = require('./files.js')
const Chat = require('./chat.js')
const AdmZip = require('adm-zip')
const JSZip = require('jszip')
const glob = require('glob')
const fs = require('fs')
const path = require('path')
@@ -72,8 +72,8 @@ class Project
// Search files with glob
const files = glob.sync(this.path + '/**', { dot: true })
// Create a zip file
const zip = new AdmZip()
// // Create a zip file
const zip = new JSZip()
// Add files to zip
for(const _file of files)
@@ -85,12 +85,12 @@ class Project
{
const basename = path.basename(_file)
const relativePath = _file.replace(basename, '').replace(this.path, '')
zip.addLocalFile(_file, relativePath)
zip.file(`${relativePath}${basename}`, fs.readFileSync(_file))
}
}
// Create and return zip buffer
this.zipBuffer = zip.toBuffer()
this.zipBuffer = zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
this.zipNeedsUpdate = false
// Debug
+4
View File
@@ -18,6 +18,10 @@ class Projects
// Connection event
this.projectsSocket.on('connection', (socket) =>
{
// // Send the config to the new socket
socket.emit('config', { domain: this.config.domain, server: this.config.server })
// Send the projects update to all users
this.projectsSocket.emit('update_projects', this.describe())
if(this.config.debug >= 1)
+10 -7
View File
@@ -109,13 +109,16 @@ class Site
// 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')
// Set finename
const date = new Date()
const hours = `${date.getHours() < 10 ? '0' : ''}${date.getHours()}`
const minutes = `${date.getMinutes() < 10 ? '0' : ''}${date.getMinutes()}`
const seconds = `${date.getSeconds() < 10 ? '0' : ''}${date.getSeconds()}`
const name = `${_project.slug}_${hours}-${minutes}-${seconds}.zip`
response.setHeader('Content-disposition', `attachment; filename=${name}`)
zipBuffer.pipe(response)
})
this.express.use('/' + _project.slug + '/files', express.static(_project.path))