Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#!/usr/bin/env node
'use strict';
// fix the cwd to project base dir for browserify and config loading
let path = require('path');
process.chdir( path.join(__dirname, '..') );
const config = require('config');
const http = require('http');
let port = (config.has('server.port') ? config.get('server.port') : null) || '127.0.0.1';
let host = (config.has('server.address') ? config.get('server.address') : null);
if (!host || host === '0.0.0.0' || host === '::') host = '127.0.0.1';
let urlPrefix = (config.has('server.urlPrefix') ? config.get('server.urlPrefix') : null) ||'';
http.get(`http://${host}:${port}${urlPrefix}/healthcheck`, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
if (data.trim() === 'ok') process.exit(0);
else {
console.log('got unexprected response from server: ' + data);
// Serve a resized image with scaling
server.get('/(:name)_(:width)_(:height)_(:scale)x.(:format)', serveResizedImage);
// Serve a resized image
server.get('/(:name)_(:width)_(:height).(:format)', serveResizedImage);
server.get('/(:name).(:format)', (req, res) => {
// Serve the original, with optionally filters applied
const params = urlParameters(req, false);
patchConnectionForTermination(req, params);
if (hasFiltersApplied(params)) {
imageResponse.magic(params, req.method, res, stats, metricFromParams(params));
} else {
imageResponse.original(params, req.method, res, metricFromParams(params));
}
});
const isSecretKeyConfigured = config.has('secret') && config.get('secret').length > 0;
// The upload stuff
server.post('/token', async (req, res) => {
// If we have a secret key configured, check whether it matches first
if (isSecretKeyConfigured && config.get('secret') !== req.body.secret) {
res.status(401).json({error: 'A secret key is required for this server, and it was not supplied or incorrect'});
return;
}
// Create a token
const image = req.body.id;
const metric = timingMetric(REQUEST_TOKEN, {fields: {name: image}});
if (image === null) {
res.status(400).end();
metric.addTag('status', 400);
metrics.write(metric);
// This is the array of Settings that will be created
const systemSettings = [ {
system: true,
type: 'string',
key: 'nodeEnv',
value: process.env.NODE_ENV || 'development',
}, {
system: true,
type: 'string',
key: 'appName',
value: 'Colmena CMS',
} ]
// Check if there are user configured Settings
if (config.has('settings')) {
// Get the defined settings
const settings = config.get('settings')
// Loop over the settings
Object.keys(settings).forEach(key => {
const value = settings[ key ]
const setting = {
key,
value,
system: true,
type: typeof value,
}
systemSettings.push(setting)
})
}
function sendNotifications(call, callback) {
logger.info('Sending notifications...');
var remaining=0;
if (config.has('httpget')) {
sendAllHTTPGet(call, config.get('httpget'), done);
remaining++;
}
if (config.has('freemobile')) {
sendAllSMS(call, done);
remaining++;
}
if (config.has('voice2freebox')) {
sendVoice2Freebox(call, config.get('voice2freebox'), done);
remaining++;
}
// No notifications at all configured
if (remaining==0) return callback();
export function configGet( key ) {
const target = this.getTargetType();
if ( target && config.has( target ) ) {
let targetConfig = config.get( target );
if ( targetConfig.has( key ) ) {
return targetConfig.get( key );
}
}
return config.get( key );
}
function get_hostname_with_case() {
var h = cfg.has('Hostname') ? cfg.get('Hostname') : os.hostname();
var hcase = cfg.has('HostnameCase') ? cfg.get('HostnameCase') : 'default';
switch(hcase) {
case 'upper': h = h.toUpperCase(); break;
case 'lower': h = h.toLowerCase(); break;
}
return(h);
}
export function getImageCacheFilepath(filepath: string, type: string) {
if (config.has('cacheDir')) {
const ext = path.extname(filepath);
const base = filepath.slice(0, filepath.length - ext.length);
const cacheFile = `${base}.${type}${ext}`;
const cacheDir: string = config.get('cacheDir');
return path.join(cacheDir, cacheFile);
} else {
throw new Error('cacheDir not set in configuration');
}
}
var processArchive = function(job, done, archiveLocalPath, requestArgs) {
debug("Processing job " + job.id)
var barc = config.has("barc_path") ? config.get("barc_path") : 'barc';
var cwd = process.cwd();
debug("Working from " + cwd);
debug(`job args: ` + JSON.stringify(requestArgs));
var archiveOutput = `${cwd}/${job.id}.mp4`;
var args = [];
args.push(`-i${archiveLocalPath}`);
args.push(`-o${archiveOutput}`);
if (requestArgs.width) {
args.push("-w" + parseInt(requestArgs.width));
}
if (requestArgs.height) {
args.push("-h" + parseInt(requestArgs.height));
}
if (requestArgs.cssPreset) {
args.push("-p" + requestArgs.cssPreset);
}
const patternBasePaths = Object.keys(config.get('nitro.patterns')).map((key) => {
const configKey = `nitro.patterns.${key}.path`;
const patternPath = config.has(configKey) ? config.get(configKey) : false;
return patternPath;
});
async function getTX(hash) {
if (config.has("urkel-enabled")) {
return _getTXUrkel(hash);
} else {
return _getTXDaemon(hash);
}
}