-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
32 lines (30 loc) · 1012 Bytes
/
index.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
'use strict';
const request = require('sync-request');
const urlParse = require('url').parse;
const isImage = require('is-image');
const isUrl = require('is-url');
module.exports = (url, accurate, timeout) => {
if (!url) return false;
const http = url.lastIndexOf('http');
if (http != -1) url = url.substring(http);
if (!isUrl(url)) return isImage(url);
let pathname = urlParse(url).pathname;
if (!pathname) return false;
const last = pathname.search(/[:?&]/);
if (last != -1) pathname = pathname.substring(0, last);
if (isImage(pathname)) return true;
if (/styles/i.test(pathname)) return false;
try {
if (!accurate) return false;
if (!timeout) timeout = 60000;
const res = request('GET', url, { timeout });
if (!res) return false;
const headers = res.headers;
if (!headers) return false;
const contentType = headers['content-type'];
if (!contentType) return false;
return contentType.search(/^image\//) != -1;
} catch (e) {
return false;
}
};