Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions coce.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,10 @@ CoceFetcher.prototype.addurl = function addurl(provider, id, url) {
if (config[provider].cache) {
storedUrl = `${config.cache.url}/${provider}/${id}.jpg`;
const dest = `${config.cache.path}/${provider}/${id}.jpg`;
const file = fs.createWriteStream(dest);
https.get(url, (response) => {
response.pipe(file);
file.on('finish', () => file.close());
}).on('error', () => fs.unlink(dest));
saveresult = this.saveurl(url, dest);
if (saveresult != 200) {
storedUrl = url;
}
} else {
storedUrl = url;
}
Expand All @@ -189,6 +188,40 @@ CoceFetcher.prototype.addurl = function addurl(provider, id, url) {
this.url[id][provider] = storedUrl;
};

/**
*
* Get a copy of the file, following redirects as necessary.
* Returns HTTP code 200 if successful.
*/
CoceFetcher.prototype.saveurl = function saveurl(url, dest) {
var redirs = [url],
fetch = function (url, dest) {
https.get(url, (response) => {
var body = [];
if ([503].indexOf(response.statusCode) >= 0) {
return 503;
} else if ([301, 302].indexOf(response.statusCode) >= 0) {
if (redirs.length > 10) {
return 302; // Fail silently
} else {
if (redirs.indexOf(response.headers.location) < 0) {
redirs.push(response.headers.location);
return fetch(response.headers.location, dest);
} else {
return 500; // Redirect loop detected. Fail silently
}
}
} else {
const file = fs.createWriteStream(dest);
response.pipe(file);
file.on('finish', () => file.close());
return 200;
}
});
};
return fetch(url, dest);
}

/**
* Increment the count of found URLs.
* Stop the timer if all URLs have been found.
Expand Down