Commit be2b74c3 authored by yPhil's avatar yPhil
Browse files

Promises and rejections

parent 4596a512
Pipeline #418156 failed with stage
in 49 seconds
const express = require('express'), const express = require('express'),
router = express.Router(), router = express.Router(),
favrat = require('favrat'),
feeder = require('./feeder'), feeder = require('./feeder'),
fetch = require('node-fetch'), fetch = require('node-fetch'),
// favrat = require('favrat'),
favrat = require(__dirname + '/../../favrat/'),
feedrat = require('feedrat'), feedrat = require('feedrat'),
// feedrat = require(__dirname + '/../../feedrat/'), // feedrat = require(__dirname + '/../../feedrat/'),
fs = require('fs'), fs = require('fs'),
...@@ -24,29 +25,36 @@ router.get('/favicon', function(req, res) { ...@@ -24,29 +25,36 @@ router.get('/favicon', function(req, res) {
if (url) { if (url) {
console.error('######### url: %s (%s)',url);
if (!url.startsWith('http') || !url.startsWith('//')) url = 'http://' + url.substring(url.indexOf("/") + 1); if (!url.startsWith('http') || !url.startsWith('//')) url = 'http://' + url.substring(url.indexOf("/") + 1);
const hash = crypto.createHash('md5').update(url).digest('hex'), const hash = crypto.createHash('md5').update(url).digest('hex'),
fileName = hash + '.favicon', fileName = hash + '.favicon',
filePath = path.join(pjson.FAVICONS_CACHE_DIR, fileName); filePath = path.join(pjson.FAVICONS_CACHE_DIR, fileName);
// res.send(hash);
fetch(url) fetch(url)
.then(res => { .then(
const dest = fs.createWriteStream(filePath, { 'Content-Type': 'image/x-icon' }); res =>
res.body.pipe(dest); new Promise((resolve, reject) => {
res.body.on("end", () => { const dest = fs.createWriteStream(filePath, {'Content-Type': 'image/x-icon'});
resolve(); res.body.pipe(dest);
res.send(hash); res.body.on("end", () => {
}); resolve({fileName, url});
dest.on("error", () => { });
res.status(500).send(false); dest.on("error", () => {
reject('No favicon found'); reject('No favicon found');
}); res.status(500).send(false);
});
})
).catch(error => {
res.status(500).send(error);
}); });
} else { } else {
reject('Not a valid URL'); res.status(500).send(false);
res.send(false);
} }
}); });
}); });
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment