-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.js
137 lines (117 loc) · 3.12 KB
/
image.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const path = require( 'path' )
const fs = require( 'fs' )
const mkdirp = require( 'mkdirp' )
const request = require( 'request' )
const transform = require( './transform' )
module.exports = ( url ) => {
return new Promise( ( resolve, reject ) => {
let parsedUrl = parseUrl( url )
if ( false === parsedUrl ) {
reject()
}
let filePath = path.resolve( `./images/${parsedUrl.cloud.name}/${parsedUrl.cloud.folder}/${parsedUrl.file}` )
let fileUrl = `${parsedUrl.cloud.remote}${parsedUrl.file}`
downloadMissingImage( fileUrl, filePath ).then( () => {
transform( filePath, parsedUrl.transformations ).then( data => {
resolve( Object.assign( data, parsedUrl ) )
} ).catch( reject )
} ).catch( reject )
} )
}
parseUrl = ( url ) => {
let config = JSON.parse( fs.readFileSync( './config.json' ) )
let clouds = config.clouds
let match = false
clouds.some( ( cloud ) => {
match = matchUrl( url, cloud.name, cloud.folder )
if ( false !== match ) {
match.cloud = cloud
return true
}
return false
} )
return match
}
matchUrl = ( url, name, folder ) => {
let hasCustomFileName = false
let transformationIndex = -1
let fileIndex = -1
let match = null
let regExps = [
{
regex: `${name}\/images\/(.*)\/${folder}\/(.+)`,
customFileName: true,
transformations: 1,
file: 2,
},
{
regex: `${name}\/images\/${folder}\/(.+)`,
customFileName: true,
transformations: -1,
file: 1,
},
{
regex: `${name}\/(.*)\/${folder}\/(.+)`,
customFileName: false,
transformations: 1,
file: 2,
},
{
regex: `${name}\/${folder}\/(.+)`,
customFileName: false,
transformations: -1,
file: 1,
},
]
regExps.some( ( regExp ) => {
match = url.match( new RegExp( regExp.regex ) )
if ( null !== match ) {
hasCustomFileName = regExp.customFileName
transformationIndex = regExp.transformations
fileIndex = regExp.file
return true
}
return false
} )
if ( null === match ) {
return false
}
let transformationString = ''
let fileName = ''
let transformations = {}
if ( transformationIndex > 0 ) {
transformationString = match[ transformationIndex ]
let allTransformations = transformationString.split( ',' )
allTransformations.forEach( singleTransformation => {
let transformation = singleTransformation.split( '_', 2 )
transformations[ transformation[0] ] = transformation[1]
} )
}
if ( fileIndex > 0 ) {
fileName = match[ fileIndex ]
if ( hasCustomFileName ) {
fileName = path.dirname( fileName ) + path.extname( fileName )
}
}
return {
transformations: transformations,
file: fileName,
}
}
downloadMissingImage = ( url, filePath ) => {
return new Promise( ( resolve, reject ) => {
if ( ! fs.existsSync( filePath ) ) {
downloadImage( url, filePath ).then( resolve ).catch( reject )
} else {
resolve()
}
} )
}
downloadImage = ( url, filePath ) => {
return new Promise( resolve => {
mkdirp( path.dirname( filePath ) )
request.head( url, () => {
request( url ).pipe( fs.createWriteStream( filePath ) ).on( 'close', resolve )
} )
} )
}