Skip to content

Commit

Permalink
Merge branch 'code-fixes' into trunk
Browse files Browse the repository at this point in the history
Signed-off-by: Hrishikesh Patil <[email protected]>
  • Loading branch information
riskycase committed Aug 28, 2020
2 parents 3c91bf6 + b30545f commit 0a3ead6
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 150 deletions.
14 changes: 11 additions & 3 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ stages:
- test
- build
- testing
- coverage
- post-test
- publish

Build Latest:
Expand Down Expand Up @@ -58,7 +58,7 @@ Test Latest Options:
- .nyc_output/

Coverage Latest:
stage: coverage
stage: post-test
needs: ['Test Latest Options', 'Test Latest API']
image: node:latest
before_script:
Expand All @@ -83,7 +83,7 @@ Test LTS Options:
needs: ['Build LTS']

Coverage LTS:
stage: coverage
stage: post-test
needs: ['Test LTS Options', 'Test LTS API']
image: node:lts
before_script:
Expand All @@ -98,6 +98,13 @@ Coverage LTS:
reports:
cobertura: coverage/cobertura-coverage.xml

Lint:
stage: post-test
needs: ['Build Latest', 'Build LTS']
image: node:lts
script:
- yarn prettier --check .

Publish:
stage: publish
needs:
Expand All @@ -106,6 +113,7 @@ Publish:
'Test LTS API',
'Test Latest Options',
'Test Latest API',
'Lint',
]
image: node:lts
before_script:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v2.2.1

Fix few CodeClimate issues
Publish package from GitLab CI instead of PC
Update devDependencies

v2.2.0

Use AJAX to show homepage rather than template engine
Expand Down
75 changes: 35 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,35 @@ const indexRouter = require('./routes/index');
const uploadRouter = require('./routes/upload');
const downloadRouter = require('./routes/download');

// Sets the format to be used for logging
const format =
':method request for :url resulted in status code :status - :response-time ms';
/*
* Sets up logging for the app
*
* Takes express app and adds logger to it
*/
function setLogging(app, loggingLevel, stdout) {
// Sets the format to be used for logging
const format =
':method request for :url resulted in status code :status - :response-time ms';

if (isNaN(loggingLevel) || loggingLevel > 2 || loggingLevel < 0)
loggingLevel = 0;

stdout = stdout || process.stdout;

if (loggingLevel === 2)
app.use(
logger(format, {
stream: stdout,
})
);
else if (loggingLevel === 1)
app.use(
logger(format, {
stream: stdout,
skip: (req, res) => res.statusCode < 400,
})
);
}

/*
* Initialises an Express app with the view generator, plugins and routes
Expand Down Expand Up @@ -58,28 +84,6 @@ function handlers(app) {
});
}

/*
* Sets valid values for keys of the options which may be invaild
*
* Takes options object which is modified and returns nothing
*/
function cleanOptions(options) {
if (
isNaN(options.loggingLevel) ||
options.loggingLevel > 2 ||
options.loggingLevel < 0
)
options.loggingLevel = 0;
if (options.restart === undefined) options.restart = true;
if (!Array.isArray(options.files)) options.files = [];
if (!options.destination)
options.destination = path.resolve(
path.basename(require.main.filename),
'../uploads'
);
if (!options.stdout) options.stdout = process.stdout;
}

/*
* Creates an Express app with the options specified
*
Expand All @@ -88,23 +92,14 @@ function cleanOptions(options) {
module.exports.init = (options = {}) => {
const app = express();

cleanOptions(options);

if (options.loggingLevel === 2)
app.use(
logger(format, {
stream: options.stdout,
})
);
else if (options.loggingLevel === 1)
app.use(
logger(format, {
stream: options.stdout,
skip: (req, res) => res.statusCode < 400,
})
);
// Set logging only if neccessary
if (options.loggingLevel)
setLogging(app, options.loggingLevel, options.stdout);

return new Promise((resolve, reject) => {
// If restart flag is not set, default to true
if (options.restart === undefined) options.restart = true;

require('./middleware/fileManager')
.initFiles(options)
.then(() => {
Expand Down
49 changes: 28 additions & 21 deletions middleware/fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ function isPathPresent(arr, path) {
return arr.find(value => value.path === path);
}

/*
* Adds a path to listFiles if not already present in it or directFiles
*
* Takes path of file and returns nothing
*/
function addPath(path) {
if (!isPathPresent(directFiles, path) && !isPathPresent(listFiles, path))
listFiles.push(fileMaker(path));
}

/*
* Reads a file line by line, and add the corresponding data to an array
*
Expand All @@ -32,30 +42,22 @@ function readLines(listPath) {
});

// Resolves the promise if the file was read successfully
readInterface.on('close', resolve);
readInterface.on('close', () => {
resolve();
calcSize(listFiles);
});

let lineNumber = 1;

readInterface.on('line', line => {
// Processes every line of the file
if (
!isPathPresent(directFiles, line) &&
!isPathPresent(listFiles, line)
) {
// Adds the path to array ony if it exists
if (fs.existsSync(line)) {
listFiles.push(fileMaker(line));
if (lineNumber === 1) calcSize(listFiles);
}
// Informs in the error message which path doesn't exist
else
reject(
`${line} does not exist! (at ${path.resolve(
process.cwd(),
listPath
)} at ${lineNumber})`
);
}
if (fs.existsSync(line)) addPath(line);
else
reject(
`${line} does not exist! (at ${path.resolve(
process.cwd(),
listPath
)} at ${lineNumber})`
);
lineNumber++;
});
});
Expand Down Expand Up @@ -117,11 +119,16 @@ function fileMaker(path) {
* Takes options object and returns promise that resolves when the basic setup is successfully completed
*/
function initFiles(options) {
// Set files to empty array if not defined
if (!Array.isArray(options.files)) options.files = [];

// Filters out unique values from the files flag
directFiles = options.files
.filter((value, index, self) => self.indexOf(value) === index)
.map(fileMaker);
destination = options.destination;
destination =
options.destination ||
path.resolve(path.basename(require.main.filename), '../uploads');

// Starts calculating the size of all the files in files flag
calcSize(directFiles);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "broadcastem-core",
"version": "2.2.0",
"version": "2.2.1",
"description": "The core module for Broadcast 'em application",
"keywords": [
"broadcast",
Expand Down Expand Up @@ -36,8 +36,8 @@
"chai-http": "^4.3.0",
"dev-null": "^0.1.1",
"husky": "^4.2.5",
"mocha": "^8.1.1",
"mocha": "^8.1.2",
"mocha-junit-reporter": "^2.0.0",
"prettier": "^2.0.5"
"prettier": "^2.1.1"
}
}
62 changes: 21 additions & 41 deletions public/script/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,10 @@ UIkit.upload('.js-upload', {
url: '/upload',
multiple: true,
type: 'multipart/form-data',
beforeSend: function () {
console.log('beforeSend', arguments);
},
beforeAll: function () {
console.log('beforeAll', arguments);
},
load: function () {
console.log('load', arguments);
},
error: function () {
console.log('error', arguments);
},
complete: function () {
console.log('complete', arguments);
},
loadStart: function (e) {
console.log('loadStart', arguments);
bar.removeAttribute('hidden');
bar.max = e.total;
bar.value = e.loaded;
},
progress: function (e) {
console.log('progress', arguments);
bar.max = e.total;
bar.value = e.loaded;
},
loadEnd: function (e) {
console.log('loadEnd', arguments);
bar.max = e.total;
bar.value = e.loaded;
},
loadStart: setBarValue,
progress: setBarValue,
loadEnd: setBarValue,
completeAll: function () {
console.log('completeAll', arguments);
setTimeout(function () {
bar.setAttribute('hidden', 'hidden');
}, 1000);
Expand All @@ -147,6 +118,15 @@ UIkit.upload('.js-upload', {
},
});

/*
* Sets the value for the progress bar
*/
function setBarValue(e) {
bar.removeAttribute('hidden');
bar.max = e.total;
bar.value = e.loaded;
}

/*
* Once the button for downloading the selected files is clicked, sends the
* appropriate request
Expand All @@ -160,13 +140,13 @@ function downloadSelected() {
* files which will be requested for download
*/
function checkboxHit(id) {
if (document.getElementById(`checkbox-${id}`).checked) {
if (!indices.length)
document.getElementById('selected').style.display = 'inline-block';
indices.push(id);
} else {
indices.splice(indices.indexOf(id), 1);
if (!indices.length)
document.getElementById('selected').style.display = 'none';
}
// If checkbox gets selected, adds that id to indices else removes it
document.getElementById(`checkbox-${id}`).checked
? indices.push(id)
: indices.splice(indices.indexOf(id), 1);

// If indices are empty, hide the 'Selected' download button
document.getElementById('selected').style.display = indices.length
? 'inline-block'
: 'none';
}
Loading

0 comments on commit 0a3ead6

Please sign in to comment.