1
1
const _ = require ( 'lodash' ) ;
2
+ const path = require ( 'path' ) ;
3
+ const fse = require ( 'fs-extra' ) ;
2
4
const request = require ( 'request-promise' ) ;
3
- const { promisify } = require ( 'util' ) ;
4
5
const cheerio = require ( 'cheerio' ) ;
5
6
const extract = require ( 'extract-zip' ) ;
6
7
const download = require ( 'download' ) ;
@@ -12,6 +13,7 @@ const {
12
13
DEFAULT_PROTOCOL ,
13
14
DEFAULT_LANG ,
14
15
APPLICATION ,
16
+ PREPACKAGED_APPS_FOLDER_NAME ,
15
17
} = require ( './config/config' ) ;
16
18
const {
17
19
getExtension,
@@ -49,6 +51,13 @@ const getDownloadUrl = async ({ url, lang }) => {
49
51
return false ;
50
52
} ;
51
53
54
+ const extractZip = async ( zipPath , destinationFolder ) => {
55
+ await extract ( zipPath , {
56
+ dir : destinationFolder ,
57
+ } ) ;
58
+ fse . unlinkSync ( zipPath ) ;
59
+ } ;
60
+
52
61
const downloadFile = async ( {
53
62
ext,
54
63
hash,
@@ -82,38 +91,64 @@ const downloadApplication = async ({
82
91
relativeSpacePath,
83
92
absoluteSpacePath,
84
93
app,
94
+ hash,
85
95
} ) => {
86
96
const { name, url, main } = app ;
87
97
// generate hash to save file
88
- const tmpZipName = `application.${ ext } ` ;
89
- const tmpZipPath = `${ absoluteSpacePath } /${ tmpZipName } ` ;
90
- const absoluteMainFilePath = `${ absoluteSpacePath } /${ name } /${ main } ` ;
98
+ const tmpZipName = `${ name } .zip` ;
99
+ const tmpZipPath = path . join ( absoluteSpacePath , tmpZipName ) ;
100
+ const destinationFolder = path . join ( absoluteSpacePath , name ) ;
101
+ const absoluteMainFilePath = path . join ( destinationFolder , main ) ;
102
+ const prepackagedPath = path . join (
103
+ __dirname ,
104
+ PREPACKAGED_APPS_FOLDER_NAME ,
105
+ tmpZipName
106
+ ) ;
107
+ let relativeFilePath = `${ relativeSpacePath } /${ name } /${ main } ` ;
91
108
92
- // eslint-disable-next-line no-await-in-loop
93
109
const fileAvailable = await isFileAvailable ( absoluteMainFilePath ) ;
94
110
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
111
try {
106
- await promisify ( extract ) ( tmpZipPath , {
107
- dir : `${ absoluteSpacePath } /${ name } ` ,
108
- } ) ;
112
+ // download app if the file is not available
113
+ // todo: update app if deprecated
114
+ if ( ! fileAvailable ) {
115
+ // copy and extract prepackaged application if exists
116
+ if ( fse . existsSync ( prepackagedPath ) ) {
117
+ // copy app in space
118
+ logger . debug ( `copying prepackaged application from ${ prepackagedPath } ` ) ;
119
+ fse . copySync ( prepackagedPath , tmpZipPath ) ;
120
+
121
+ await extractZip ( tmpZipPath , destinationFolder ) ;
122
+ }
123
+ // download application packaged as a zip file
124
+ else if ( ext === 'zip' ) {
125
+ logger . debug ( `downloading application ${ url } ` ) ;
126
+ // eslint-disable-next-line no-await-in-loop
127
+ await download ( url , absoluteSpacePath , {
128
+ filename : tmpZipName ,
129
+ } ) ;
130
+ logger . debug ( `downloaded application ${ url } to ${ tmpZipPath } ` ) ;
131
+
132
+ await extractZip ( tmpZipPath , destinationFolder ) ;
133
+ }
134
+ // download one-file application
135
+ else if ( ext === 'html' ) {
136
+ relativeFilePath = downloadFile ( {
137
+ ext,
138
+ relativeSpacePath,
139
+ absoluteSpacePath,
140
+ hash,
141
+ url : app . url ,
142
+ } ) ;
143
+ }
144
+ }
109
145
} catch ( e ) {
110
146
console . log ( e ) ;
111
147
return false ;
112
148
}
113
149
114
150
// returning this indicates that resource was downloaded successfully
115
- const relativeMainFilePath = `${ relativeSpacePath } /${ name } /${ main } ` ;
116
- return relativeMainFilePath ;
151
+ return relativeFilePath ;
117
152
} ;
118
153
119
154
const downloadResource = async ( {
@@ -155,12 +190,14 @@ const downloadResource = async ({
155
190
// generate hash to save file
156
191
const hash = generateHash ( resource ) ;
157
192
let asset = null ;
158
- if ( ext === 'zip' && resource . category === APPLICATION ) {
193
+ // follow a particular process to download an applicationp
194
+ if ( resource . category === APPLICATION ) {
159
195
asset = await downloadApplication ( {
160
196
ext,
161
197
relativeSpacePath,
162
198
absoluteSpacePath,
163
199
app : resourceObj ,
200
+ hash,
164
201
} ) ;
165
202
}
166
203
// only download if extension is present
0 commit comments