Skip to content

Commit

Permalink
fix: do not fail if extension is missing
Browse files Browse the repository at this point in the history
closes #155
  • Loading branch information
juancarlosfarah committed Aug 6, 2019
1 parent 6d36e99 commit b3bf56a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
76 changes: 45 additions & 31 deletions public/app/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,31 @@ const downloadSpaceResources = async ({ lang, space, absoluteSpacePath }) => {
if (url.startsWith('//')) {
url = `https:${url}`;
}

// get extension to save file
const ext = getExtension({ url });
const hash = generateHash({ url });
const imageFileName = `${hash}.${ext}`;
const relativeImagePath = `${relativeSpacePath}/${imageFileName}`;
const absoluteImagePath = `${absoluteSpacePath}/${imageFileName}`;
// eslint-disable-next-line no-await-in-loop
const imageAvailable = await isFileAvailable(absoluteImagePath);
if (!imageAvailable) {
logger.debug(`downloading ${url}`);

// only download if extension is present
if (ext) {
// generate hash to save file
const hash = generateHash({ url });
const imageFileName = `${hash}.${ext}`;
const relativeImagePath = `${relativeSpacePath}/${imageFileName}`;
const absoluteImagePath = `${absoluteSpacePath}/${imageFileName}`;
// eslint-disable-next-line no-await-in-loop
await download(url, absoluteSpacePath, {
filename: imageFileName,
});
logger.debug(
`downloaded ${url} to ${absoluteSpacePath}/${imageFileName}`
);
const imageAvailable = await isFileAvailable(absoluteImagePath);
if (!imageAvailable) {
logger.debug(`downloading ${url}`);
// eslint-disable-next-line no-await-in-loop
await download(url, absoluteSpacePath, {
filename: imageFileName,
});
logger.debug(
`downloaded ${url} to ${absoluteSpacePath}/${imageFileName}`
);
}
spaceToSave.image[key] = relativeImagePath;
}
spaceToSave.image[key] = relativeImagePath;
}
}
}
Expand Down Expand Up @@ -101,27 +108,34 @@ const downloadSpaceResources = async ({ lang, space, absoluteSpacePath }) => {
url = `https:${url}`;
}

// generate hash and get extension to save file
const hash = generateHash(resource);
// get extension to save file
const ext = getExtension(resource);
const fileName = `${hash}.${ext}`;
const relativeFilePath = `${relativeSpacePath}/${fileName}`;
const absoluteFilePath = `${absoluteSpacePath}/${fileName}`;
phase.items[i].hash = hash;

// eslint-disable-next-line no-await-in-loop
const fileAvailable = await isFileAvailable(absoluteFilePath);
// only download if extension is present
if (ext) {
// generate hash to save file
const hash = generateHash(resource);
const fileName = `${hash}.${ext}`;
const relativeFilePath = `${relativeSpacePath}/${fileName}`;
const absoluteFilePath = `${absoluteSpacePath}/${fileName}`;
phase.items[i].hash = hash;

// if the file is available, point this resource to its path
if (!fileAvailable) {
logger.debug(`downloading ${url}`);
// eslint-disable-next-line no-await-in-loop
await download(url, absoluteSpacePath, {
filename: fileName,
});
logger.debug(`downloaded ${url} to ${absoluteSpacePath}/${fileName}`);
const fileAvailable = await isFileAvailable(absoluteFilePath);

// if the file is available, point this resource to its path
if (!fileAvailable) {
logger.debug(`downloading ${url}`);
// eslint-disable-next-line no-await-in-loop
await download(url, absoluteSpacePath, {
filename: fileName,
});
logger.debug(
`downloaded ${url} to ${absoluteSpacePath}/${fileName}`
);
}
phase.items[i].asset = relativeFilePath;
}
phase.items[i].asset = relativeFilePath;
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion public/app/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ const getExtension = ({ url, mimeType }) => {
if (mimeType) {
return mime.extension(mimeType);
}
return url.match(/[^\\]*\.(\w+)$/)[1];
logger.debug(url, mimeType);
const matchExtension = url.match(/[^\\]*\.(\w+)$/);
logger.debug(matchExtension);
if (matchExtension && matchExtension.length) {
// extension is in index 1
return matchExtension[1];
}
return null;
};

const isDownloadable = resource => {
Expand Down

0 comments on commit b3bf56a

Please sign in to comment.