From c669dca69deab7dd3e9860c72dc086c5c977f212 Mon Sep 17 00:00:00 2001
From: jweg <jweg@informatik.uni-kiel.de>
Date: Wed, 11 Oct 2017 11:51:54 +0200
Subject: [PATCH] clientCounter fixed

---
 app.js             | 4 +---
 routes/download.js | 6 ++----
 routes/home.js     | 8 ++------
 3 files changed, 5 insertions(+), 13 deletions(-)

diff --git a/app.js b/app.js
index 6a5d22b..4892eee 100644
--- a/app.js
+++ b/app.js
@@ -42,10 +42,8 @@ app.set('zipFunctions', zipFunctions);
 
 //clientCounter holds the number of clients who wants to download files
 let clientCounter = 0;
-app.set('clientCounter', clientCounter);
-
+app.locals.clientCounter = clientCounter;
 //routes
-//app.use('/', home);
 require('./routes/home.js')(app);
 require('./routes/download.js')(app);
 
diff --git a/routes/download.js b/routes/download.js
index 4ae8060..2b2d1f1 100644
--- a/routes/download.js
+++ b/routes/download.js
@@ -6,7 +6,6 @@ module.exports= function(app){
 
 const downloadNames = app.get('downloadNames');
 const downloadFiles = app.get('downloadFiles');
-let clientCounter = app.get('clientCounter');
 const clientFolder = app.get('clientFolder');
 const backendFunctions = app.get('backendFunctions');
 const frontendFunctions = app.get('frontendFunctions');
@@ -16,18 +15,17 @@ const zipFunctions = app.get('zipFunctions');
 app.get('/download', function(req, res, next){
 	const checkedFiles = req.query.checkedFiles.split(',');
 	let clientName;
+	let clientCounter = req.app.locals.clientCounter;
 	const zip = new JSZip();
 	zip.file("README.txt", "text for README");
 		//holds promises for backend and/or frontend for synchronizing
 		const promiseArray = []
 		
-		console.log('clientCounter, download: ', clientCounter);
 		//TODO handle multiple clients
 		if (clientCounter > 5){
 			res.render('tooManyClients');
 		}else{
 			clientName = 'client_' + clientCounter;
-			console.log('clientCounter, download.makeClientFolder: ', clientCounter);
 			clientFolder.makeClientFolder(clientName);
 			
 		}
@@ -121,7 +119,7 @@ function createZip(zip){
 		}).on('finish', function () {
 			console.log('explorviz-builds.zip written.');
 			//client finishes everything except getting the download
-			clientCounter = clientCounter - 1;
+			req.app.locals.clientCounter = clientCounter - 1;
 			res.download('explorviz-builds.zip', function(error){
 				if(error){
 					let response = 
diff --git a/routes/home.js b/routes/home.js
index 4864c14..c55e0dd 100644
--- a/routes/home.js
+++ b/routes/home.js
@@ -4,8 +4,6 @@ const express = require('express');
 module.exports= function(app){
 	
 const downloadNames = app.get('downloadNames');
-let clientCounter = app.get('clientCounter');
-console.log('clientCounter, home: ', clientCounter);
 
 //This route shows the homepage.
 app.get('/', function(req, res){
@@ -15,12 +13,10 @@ app.get('/', function(req, res){
 //This route takes the checkedFiles the user selected on the homepage, shows the download page on which the actual download is triggered.
 app.post('/', function(req, res, next){
 	const checkedFiles = Object.keys(req.body);
+	let clientCounter = req.app.locals.clientCounter;
 
 	if(typeof checkedFiles !== 'undefined' && checkedFiles.length > 0){
-let increasedClientCounter = clientCounter + 1;
-
-		app.set('clientCounter', increasedClientCounter);
-		console.log('clientCounter, home +1: ', clientCounter);
+		req.app.locals.clientCounter = clientCounter + 1;
 		res.render('download', {checkedFiles: checkedFiles, downloadFiles: downloadNames});
 	}
 	else {
-- 
GitLab