-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.ts
44 lines (41 loc) · 1017 Bytes
/
utils.ts
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
import sizeOf from 'image-size'
import { IImgSize } from 'picgo'
interface IImgSizeInfo extends IImgSize {
type?: string
}
export const getImageSize = (buffer: Buffer): IImgSizeInfo => {
try {
const size = sizeOf(buffer)
return {
real: true,
width: size.width,
height: size.height,
type: size.type
}
} catch (e) {
// fallback to default size
return {
real: false,
width: 200,
height: 200,
type: '.png'
}
}
}
export const isUrl = (url: string): boolean => (url.startsWith('http://') || url.startsWith('https://'))
export const isUrlEncode = (url: string): boolean => {
url = url || ''
try {
// the whole url encode or decode shold not use encodeURIComponent or decodeURIComponent
return url !== decodeURI(url)
} catch (e) {
// if some error caught, try to let it go
return true
}
}
export const handleUrlEncode = (url: string): string => {
if (!isUrlEncode(url)) {
url = encodeURI(url)
}
return url
}