-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
collective-image-check.js
59 lines (54 loc) · 1.51 KB
/
collective-image-check.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import '../server/env';
import fetch from 'node-fetch';
import models, { Op } from '../server/models';
const searchPattern = process.argv[2];
if (!searchPattern) {
console.log('Please pass a search pattern to filter collective images to process.');
console.log('Ie: %logo.clearbit.com% or %pbs.twimg.com% or %opencollective-production.s3.us-west-1.amazonaws.com%');
process.exit();
}
async function main() {
const collectives = await models.Collective.findAll({
where: {
image: { [Op.iLike]: searchPattern },
},
});
for (const collective of collectives) {
let response;
try {
response = await fetch(collective.image);
} catch (e) {
console.log(e);
continue;
}
if (response.status === 404) {
console.log(`Image for ${collective.slug} not found: ${collective.image}`);
try {
await collective.update({ image: null });
} catch (e) {
console.log(e);
}
continue;
}
if (response.status === 403) {
console.log(`Image for ${collective.slug} access denied: ${collective.image}`);
try {
await collective.update({ image: null });
} catch (e) {
console.log(e);
}
continue;
}
const body = await response.text();
if (body.length === 0) {
console.log(`Image for ${collective.slug} invalid: ${collective.image}`);
try {
await collective.update({ image: null });
} catch (e) {
console.log(e);
}
}
}
console.log('Done.');
}
main();