1
1
const _ = require ( 'lodash' ) ;
2
2
const request = require ( 'request-promise' ) ;
3
+ const { promisify } = require ( 'util' ) ;
3
4
const cheerio = require ( 'cheerio' ) ;
5
+ const extract = require ( 'extract-zip' ) ;
4
6
const download = require ( 'download' ) ;
5
7
const providers = require ( './config/providers' ) ;
6
8
const logger = require ( './logger' ) ;
7
9
const mapping = require ( './config/mapping' ) ;
8
- const { DEFAULT_PROTOCOL , DEFAULT_LANG } = require ( './config/config' ) ;
10
+
11
+ const {
12
+ DEFAULT_PROTOCOL ,
13
+ DEFAULT_LANG ,
14
+ APPLICATION ,
15
+ } = require ( './config/config' ) ;
9
16
const {
10
17
getExtension,
11
18
isDownloadable,
@@ -15,7 +22,7 @@ const {
15
22
16
23
const getDownloadUrl = async ( { url, lang } ) => {
17
24
let proxied = false ;
18
- providers . forEach ( provider => {
25
+ providers . forEach ( ( provider ) => {
19
26
if ( url . includes ( provider ) ) {
20
27
proxied = true ;
21
28
}
@@ -42,63 +49,131 @@ const getDownloadUrl = async ({ url, lang }) => {
42
49
return false ;
43
50
} ;
44
51
52
+ const downloadFile = async ( {
53
+ ext,
54
+ hash,
55
+ relativeSpacePath,
56
+ absoluteSpacePath,
57
+ url,
58
+ } ) => {
59
+ const fileName = `${ hash } .${ ext } ` ;
60
+ const relativeFilePath = `${ relativeSpacePath } /${ fileName } ` ;
61
+ const absoluteFilePath = `${ absoluteSpacePath } /${ fileName } ` ;
62
+
63
+ // eslint-disable-next-line no-await-in-loop
64
+ const fileAvailable = await isFileAvailable ( absoluteFilePath ) ;
65
+
66
+ // if the file is available, point this resource to its path
67
+ if ( ! fileAvailable ) {
68
+ logger . debug ( `downloading ${ url } ` ) ;
69
+ // eslint-disable-next-line no-await-in-loop
70
+ await download ( url , absoluteSpacePath , {
71
+ filename : fileName ,
72
+ } ) ;
73
+ logger . debug ( `downloaded ${ url } to ${ absoluteFilePath } ` ) ;
74
+ }
75
+
76
+ // returning this indicates that resource was downloaded successfully
77
+ return relativeFilePath ;
78
+ } ;
79
+
80
+ const downloadApplication = async ( {
81
+ ext,
82
+ relativeSpacePath,
83
+ absoluteSpacePath,
84
+ app,
85
+ } ) => {
86
+ const { name, url, main } = app ;
87
+ // generate hash to save file
88
+ const tmpZipName = `application.${ ext } ` ;
89
+ const tmpZipPath = `${ absoluteSpacePath } /${ tmpZipName } ` ;
90
+ const absoluteMainFilePath = `${ absoluteSpacePath } /${ name } /${ main } ` ;
91
+
92
+ // eslint-disable-next-line no-await-in-loop
93
+ const fileAvailable = await isFileAvailable ( absoluteMainFilePath ) ;
94
+
95
+ // if the file is available, point this resource to its path
96
+ if ( ! fileAvailable ) {
97
+ logger . debug ( `downloading application ${ url } ` ) ;
98
+ // eslint-disable-next-line no-await-in-loop
99
+ await download ( url , absoluteSpacePath , {
100
+ filename : tmpZipName ,
101
+ } ) ;
102
+ logger . debug ( `downloaded application ${ url } to ${ tmpZipPath } ` ) ;
103
+ }
104
+ // unzip application files
105
+ try {
106
+ await promisify ( extract ) ( tmpZipPath , {
107
+ dir : `${ absoluteSpacePath } /${ name } ` ,
108
+ } ) ;
109
+ } catch ( e ) {
110
+ console . log ( e ) ;
111
+ return false ;
112
+ }
113
+
114
+ // returning this indicates that resource was downloaded successfully
115
+ const relativeMainFilePath = `${ relativeSpacePath } /${ name } /${ main } ` ;
116
+ return relativeMainFilePath ;
117
+ } ;
118
+
45
119
const downloadResource = async ( {
46
120
resource,
47
121
absoluteSpacePath,
48
122
lang,
49
123
relativeSpacePath,
50
124
} ) => {
51
125
if ( resource && isDownloadable ( resource ) ) {
52
- let { url } = resource ;
53
-
126
+ const { url } = resource ;
127
+ let resourceObj = { url } ;
54
128
// check mappings for files
55
129
if ( mapping [ url ] ) {
56
- url = mapping [ url ] ;
130
+ resourceObj = mapping [ url ] ;
57
131
}
58
132
59
133
// download from proxy url if available
60
134
// eslint-disable-next-line no-await-in-loop
61
- const downloadUrl = await getDownloadUrl ( { url, lang } ) ;
135
+ const downloadUrl = await getDownloadUrl ( { url : resourceObj . url , lang } ) ;
62
136
if ( downloadUrl ) {
63
- url = downloadUrl ;
137
+ resourceObj . url = downloadUrl ;
64
138
}
65
139
66
140
// default to https
67
- if ( url . startsWith ( '//' ) ) {
68
- url = `https:${ url } ` ;
141
+ if ( resourceObj . url . startsWith ( '//' ) ) {
142
+ resourceObj . url = `https:${ resourceObj . url } ` ;
69
143
}
70
144
71
145
// get extension to save file
72
- const ext = getExtension ( resource ) ;
146
+ const ext = getExtension ( {
147
+ url : resourceObj . url ,
148
+ mimeType : resource . mimeType ,
149
+ } ) ;
73
150
74
- // only download if extension is present
75
- if ( ext ) {
76
- // generate hash to save file
77
- const hash = generateHash ( resource ) ;
78
- const fileName = `${ hash } .${ ext } ` ;
79
- const relativeFilePath = `${ relativeSpacePath } /${ fileName } ` ;
80
- const absoluteFilePath = `${ absoluteSpacePath } /${ fileName } ` ;
81
-
82
- // eslint-disable-next-line no-await-in-loop
83
- const fileAvailable = await isFileAvailable ( absoluteFilePath ) ;
84
-
85
- // if the file is available, point this resource to its path
86
- if ( ! fileAvailable ) {
87
- logger . debug ( `downloading ${ url } ` ) ;
88
- // eslint-disable-next-line no-await-in-loop
89
- await download ( url , absoluteSpacePath , {
90
- filename : fileName ,
91
- } ) ;
92
- logger . debug ( `downloaded ${ url } to ${ absoluteSpacePath } /${ fileName } ` ) ;
93
- }
151
+ if ( _ . isNil ( ext ) ) {
152
+ return false ;
153
+ }
94
154
95
- // returning this indicates that resource was downloaded successfully
96
- return {
97
- asset : relativeFilePath ,
155
+ // generate hash to save file
156
+ const hash = generateHash ( resource ) ;
157
+ let asset = null ;
158
+ if ( ext === 'zip' && resource . category === APPLICATION ) {
159
+ asset = await downloadApplication ( {
160
+ ext,
161
+ relativeSpacePath,
162
+ absoluteSpacePath,
163
+ app : resourceObj ,
164
+ } ) ;
165
+ }
166
+ // only download if extension is present
167
+ else if ( ext ) {
168
+ asset = await downloadFile ( {
169
+ ext,
98
170
hash,
99
- } ;
171
+ relativeSpacePath,
172
+ absoluteSpacePath,
173
+ url : resourceObj . url ,
174
+ } ) ;
100
175
}
101
- return false ;
176
+ return { asset , hash } ;
102
177
}
103
178
return false ;
104
179
} ;
0 commit comments