ramirez/src/logs.js

162 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-02-09 21:56:36 -05:00
const fs = require('fs');
const crypto = require('crypto');
const moment = require('moment');
const config = require('../config');
2017-02-09 23:36:47 -05:00
const utils = require('./utils');
2017-02-09 21:56:36 -05:00
2017-02-09 23:36:47 -05:00
const logDir = config.logDir || `${__dirname}/../logs`;
2017-02-09 21:56:36 -05:00
const logFileFormatRegex = /^([0-9\-]+?)__([0-9]+?)__([0-9a-f]+?)\.txt$/;
2017-02-09 23:36:47 -05:00
/**
* @typedef {Object} LogFileInfo
* @property {String} filename
* @property {String} date
* @property {String} userId
* @property {String} token
* @property {String=} url
*/
/**
* Returns information about the given logfile
* @param {String} logFilename
* @returns {LogFileInfo}
*/
2017-02-09 21:56:36 -05:00
function getLogFileInfo(logFilename) {
const match = logFilename.match(logFileFormatRegex);
if (! match) return null;
const date = moment.utc(match[1], 'YYYY-MM-DD-HH-mm-ss').format('YYYY-MM-DD HH:mm:ss');
return {
filename: logFilename,
date: date,
userId: match[2],
token: match[3],
};
}
2017-02-09 23:36:47 -05:00
/**
* Returns the filesystem path to the given logfile
* @param {String} logFilename
* @returns {String}
*/
2017-02-09 21:56:36 -05:00
function getLogFilePath(logFilename) {
return `${logDir}/${logFilename}`;
}
2017-02-09 23:36:47 -05:00
/**
* Returns the self-hosted URL to the given logfile
* @param {String} logFilename
* @returns {String}
*/
2017-02-09 21:56:36 -05:00
function getLogFileUrl(logFilename) {
const info = getLogFileInfo(logFilename);
return utils.getSelfUrl(`logs/${info.token}`);
}
2017-02-09 23:36:47 -05:00
/**
* Returns a new, unique log file name for the given userId
* @param {String} userId
* @returns {Promise<String>}
*/
2017-02-09 21:56:36 -05:00
function getNewLogFile(userId) {
return new Promise(resolve => {
crypto.randomBytes(16, (err, buf) => {
const token = buf.toString('hex');
const date = moment.utc().format('YYYY-MM-DD-HH-mm-ss');
resolve(`${date}__${userId}__${token}.txt`);
});
});
}
2017-02-09 23:36:47 -05:00
/**
* Finds a log file name by its token
* @param {String} token
* @returns {Promise<String>}
*/
2017-02-09 21:56:36 -05:00
function findLogFile(token) {
return new Promise(resolve => {
fs.readdir(logDir, (err, files) => {
for (const file of files) {
if (file.endsWith(`__${token}.txt`)) {
resolve(file);
return;
}
}
resolve(null);
});
});
}
2017-02-09 23:36:47 -05:00
/**
* Returns all log file infos for the given userId
* @param {String} userId
* @returns {Promise<LogFileInfo[]>}
*/
2017-02-09 21:56:36 -05:00
function getLogsByUserId(userId) {
2017-02-09 23:36:47 -05:00
return new Promise((resolve, reject) => {
2017-02-09 21:56:36 -05:00
fs.readdir(logDir, (err, files) => {
2017-02-09 23:36:47 -05:00
if (err) return reject(err);
2017-02-09 21:56:36 -05:00
const logfileInfos = files
.map(file => getLogFileInfo(file))
.filter(info => info && info.userId === userId);
resolve(logfileInfos);
});
});
}
2017-02-09 23:36:47 -05:00
/**
* Returns all log file infos with URLs for the given userId
* @param {String} userId
* @returns {Promise<LogFileInfo[]>}
*/
2017-02-09 21:56:36 -05:00
function getLogsWithUrlByUserId(userId) {
return getLogsByUserId(userId).then(infos => {
const urlPromises = infos.map(info => {
return getLogFileUrl(info.filename).then(url => {
info.url = url;
return info;
});
});
return Promise.all(urlPromises).then(infos => {
infos.sort((a, b) => {
if (a.date > b.date) return 1;
if (a.date < b.date) return -1;
return 0;
});
return infos;
});
});
}
2017-02-09 23:36:47 -05:00
/**
* @param {String} logFilename
* @param {String} content
* @returns {Promise}
*/
2017-02-09 21:56:36 -05:00
function saveLogFile(logFilename, content) {
return new Promise((resolve, reject) => {
fs.writeFile(getLogFilePath(logFilename), content, {encoding: 'utf8'}, err => {
if (err) return reject(err);
resolve();
});
});
}
module.exports = {
getLogFileInfo,
getLogFilePath,
getNewLogFile,
findLogFile,
getLogsByUserId,
getLogsWithUrlByUserId,
saveLogFile,
2017-02-09 23:36:47 -05:00
getLogFileUrl,
2017-02-09 21:56:36 -05:00
};