Skip to content

Commit

Permalink
feat: download space image for offline use
Browse files Browse the repository at this point in the history
closes #47
  • Loading branch information
juancarlosfarah committed May 1, 2019
1 parent 4209a41 commit 7a09413
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
52 changes: 36 additions & 16 deletions app/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,40 @@ app.on('ready', async () => {
);
}

// only download if connection is available
const isConnected = await isOnline();
if (!isConnected) {
return mainWindow.webContents.send(
SAVE_SPACE_CHANNEL,
ERROR_DOWNLOADING_FILE
);
}

// create directory where resources will be stored
createSpaceDirectory({ id });

const { phases } = spaceToSave;
const { phases, image } = spaceToSave;

const spacePath = `${VAR_FOLDER}/${id}`;

// todo: follow new format
// if there is a background/thumbnail image, save it
if (image) {
const ext = getExtension({ url: image });
const hash = generateHash({ url: image });
const imageFileName = `${hash}.${ext}`;
const imagePath = `${spacePath}/${imageFileName}`;
const imageAvailable = await isFileAvailable(imagePath);
if (imageAvailable) {
spaceToSave.image = `file://${imagePath}`;
} else {
const imageDl = await download(mainWindow, image, {
directory: spacePath,
filename: imageFileName,
});
spaceToSave.image = `file://${imageDl.getSavePath()}`;
}
}
// eslint-disable-next-line no-restricted-syntax
for (const phase of phases) {
const { items = [] } = phase;
Expand All @@ -255,7 +285,6 @@ app.on('ready', async () => {
const hash = generateHash(resource);
const ext = getExtension(resource);
const fileName = `${hash}.${ext}`;
const spacePath = `${VAR_FOLDER}/${id}`;
const filePath = `${spacePath}/${fileName}`;
phase.items[i].hash = hash;

Expand All @@ -267,20 +296,11 @@ app.on('ready', async () => {
phase.items[i].asset = filePath;
} else {
// eslint-disable-next-line no-await-in-loop
const isConnected = await isOnline();
if (isConnected) {
// eslint-disable-next-line no-await-in-loop
const dl = await download(mainWindow, url, {
directory: spacePath,
filename: fileName,
});
phase.items[i].asset = dl.getSavePath();
} else {
return mainWindow.webContents.send(
SAVE_SPACE_CHANNEL,
ERROR_DOWNLOADING_FILE
);
}
const dl = await download(mainWindow, url, {
directory: spacePath,
filename: fileName,
});
phase.items[i].asset = dl.getSavePath();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/MediaCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const styles = theme => ({
maxWidth: 345,
},
media: {
height: 160,
height: 300,
},
leftIcon: {
marginRight: theme.spacing.unit,
Expand Down

0 comments on commit 7a09413

Please sign in to comment.