Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
opts.suffix + '$';
var regex = new RegExp(regexp);
var i;
for (i = 0; i < indirFiles.length; i++) {
var file = regex.exec(indirFiles[i]);
if (file) {
etag = new Buffer(file[1], 'base64').toString();
break;
}
}
if (etag) {
try {
var headOpts = JSON.parse(JSON.stringify(options)); // clone options
headOpts.headers = { 'If-None-Match': etag };
request.head(headOpts, function(err, res, body) {
if (err) {
logger.error(err);
cb();
// not fatal; continue
} else if (res.statusCode === 304) {
var filename = path.join(opts.outdir, indirFiles[i]);
fs.copy(path.join(opts.indir, indirFiles[i]),
filename,
{ preserveTimestamps: true },
function(err) {
if (err) {
throw (err);
}
var body = decryptData(fs.readFileSync(filename));
logger.info('Using cached copy of %s', indirFiles[i]);
var result = {
exports.sendFileFromUrl = (url, path = "../media/temp.jpg", message = "", threadId, api = gapi) => {
request.head(url, (err, res, body) => {
// Download file and pass to chat API
const fullpath = `${__dirname}/${path}`;
if (!err) {
request(url).pipe(fs.createWriteStream(fullpath)).on('close', (err, data) => {
if (!err) {
// Use API's official sendMessage here for callback functionality
exports.sendMessage({
"body": message,
"attachment": fs.createReadStream(fullpath)
}, threadId, (err, data) => {
// Delete downloaded propic
fs.unlink(fullpath);
});
} else {
exports.sendMessage(message, threadId);
}
test('exists on http site', function(done) {
request.head(this.rawURL, function(err, res) {
assert(
res.statusCode > 199 && res.statusCode < 300,
'file exists on server http status: ' + res.statusCode
);
done(err);
});
});
});
var download = function (uri, filename, callback) {
request.head(uri, function (err, res, body) {
//console.log('content-type:', res.headers['content-type']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', function () {
callback();
var fileSize = formatBytes(res.headers['content-length']);
console.log("size: " + fileSize);
});
});
};
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
if (err || res.statusCode == 404) {
console.log("!! unable to download image : " + uri);
} else {
request(uri).pipe(fs.createWriteStream(filename)).on('close', function() {
fs.createReadStream(filename).pipe(new PNG({
filterType: 4
}))
.on('error', function() {
fs.unlinkSync(filename);
console.log("!! image : " + uri + " invalid");
})
.on('parsed', function() {
console.log("image : " + uri + " downloaded and valid");
});
});
var downloadAsync = function(uri, filename, callback){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
function download(uri, filename, callback){
if(uri.length>10 && !fs.existsSync(filename)) {
request.head(uri, function(err, res, body){
if(err){
Logger.log("ERROR",{
"Filename":filename,
"URL": uri,
"Data":Date.now()
});
}
if(res.headers["location"]){
addImageToDownloadQueue(res.headers["location"], filename);
}
if(res.statusCode===200){
var serverType = res.headers['content-type'].toLowerCase();
var fileNametype = mime.lookup(filename);
if(serverType===fileNametype){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
}else{
return new Promise(resolve => {
request.head({
url
}, (err, response) => {
if (err || response.statusCode !== 200 || !response.headers.hasOwnProperty('content-length')) {
resolve(-1);
}
resolve(parseInt(response.headers['content-length']));
});
});
}
redis.hget('mvw:contentLengthCache', url, (err, result) => {
if (result) {
callback(result);
} else {
request.head(url, (error, response) => {
let contentLength = -1;
if (!!response && !!response.headers['content-length']) {
contentLength = response.headers['content-length'];
}
callback(contentLength);
redis.hset('mvw:contentLengthCache', url, contentLength.toString());
});
}
});
});