Skip to content

Commit c2f3338

Browse files
committed
feat: handle prepackaged apps
1 parent 44b3c6c commit c2f3338

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

public/app/config/config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const DOWNLOADABLE_MIME_TYPES = [
3434
];
3535

3636
// resolve path for windows '\'
37-
const escapeEscapeCharacter = str => {
37+
const escapeEscapeCharacter = (str) => {
3838
return isWindows() ? str.replace(/\\/g, '\\\\') : str;
3939
};
4040

@@ -43,6 +43,7 @@ const RESOURCE = 'Resource';
4343
const APPLICATION = 'Application';
4444

4545
const VAR_FOLDER = `${escapeEscapeCharacter(app.getPath('userData'))}/var`;
46+
const PREPACKAGED_APPS_FOLDER_NAME = 'prepackagedApps';
4647
const DATABASE_PATH = `${VAR_FOLDER}/db.json`;
4748
const ICON_PATH = 'app/assets/icon.png';
4849
const PRODUCT_NAME = 'Graasp';
@@ -127,4 +128,5 @@ module.exports = {
127128
DEFAULT_FORMAT,
128129
ACTIONS_VERBS,
129130
buildFilePath,
131+
PREPACKAGED_APPS_FOLDER_NAME,
130132
};

public/app/config/mapping.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module.exports = {
6262
'https://apps.graasp.eu/5acb589d0d5d9464081c2d46/5d84ba5978f7f92b3d219b60/latest/index.html': {
6363
main: 'index.html',
6464
name: 'file-drop',
65-
url: 'http://localhost/build/build.zip',
65+
url:
66+
'https://apps.graasp.eu/5acb589d0d5d9464081c2d46/5d84ba5978f7f92b3d219b60/latest/index.html',
6667
},
6768
};

public/app/download.js

+58-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const _ = require('lodash');
2+
const path = require('path');
3+
const fse = require('fs-extra');
24
const request = require('request-promise');
3-
const { promisify } = require('util');
45
const cheerio = require('cheerio');
56
const extract = require('extract-zip');
67
const download = require('download');
@@ -12,6 +13,7 @@ const {
1213
DEFAULT_PROTOCOL,
1314
DEFAULT_LANG,
1415
APPLICATION,
16+
PREPACKAGED_APPS_FOLDER_NAME,
1517
} = require('./config/config');
1618
const {
1719
getExtension,
@@ -49,6 +51,13 @@ const getDownloadUrl = async ({ url, lang }) => {
4951
return false;
5052
};
5153

54+
const extractZip = async (zipPath, destinationFolder) => {
55+
await extract(zipPath, {
56+
dir: destinationFolder,
57+
});
58+
fse.unlinkSync(zipPath);
59+
};
60+
5261
const downloadFile = async ({
5362
ext,
5463
hash,
@@ -82,38 +91,64 @@ const downloadApplication = async ({
8291
relativeSpacePath,
8392
absoluteSpacePath,
8493
app,
94+
hash,
8595
}) => {
8696
const { name, url, main } = app;
8797
// 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}`;
91108

92-
// eslint-disable-next-line no-await-in-loop
93109
const fileAvailable = await isFileAvailable(absoluteMainFilePath);
94110

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
105111
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+
}
109145
} catch (e) {
110146
console.log(e);
111147
return false;
112148
}
113149

114150
// returning this indicates that resource was downloaded successfully
115-
const relativeMainFilePath = `${relativeSpacePath}/${name}/${main}`;
116-
return relativeMainFilePath;
151+
return relativeFilePath;
117152
};
118153

119154
const downloadResource = async ({
@@ -155,12 +190,14 @@ const downloadResource = async ({
155190
// generate hash to save file
156191
const hash = generateHash(resource);
157192
let asset = null;
158-
if (ext === 'zip' && resource.category === APPLICATION) {
193+
// follow a particular process to download an applicationp
194+
if (resource.category === APPLICATION) {
159195
asset = await downloadApplication({
160196
ext,
161197
relativeSpacePath,
162198
absoluteSpacePath,
163199
app: resourceObj,
200+
hash,
164201
});
165202
}
166203
// only download if extension is present
1010 KB
Binary file not shown.

0 commit comments

Comments
 (0)