Skip to content

Commit

Permalink
Merge pull request #387 from HubSpot/fix/filemanager-fetch-error
Browse files Browse the repository at this point in the history
Fix filemanger fetch error
  • Loading branch information
anthmatic authored Nov 13, 2020
2 parents ca46f38 + 12d2878 commit a6cb8f7
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions packages/cms-lib/fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,34 +104,14 @@ async function downloadFile(accountId, file, dest, options) {
return;
}

const logFsError = err => {
logFileSystemErrorInstance(
err,
new FileSystemErrorContext({
destPath,
accountId,
write: true,
})
);
};
let writeStream;

try {
await fs.ensureFile(destPath);
writeStream = fs.createWriteStream(destPath, { encoding: 'binary' });
} catch (err) {
logFsError(err);
throw err;
}

try {
await http.getOctetStream(
accountId,
{
baseUrl: file.url,
uri: '',
},
writeStream
destPath
);
logger.log(`Wrote file "${destPath}"`);
} catch (err) {
Expand Down Expand Up @@ -174,6 +154,18 @@ async function fetchAllPagedFiles(accountId, folderId, { includeArchived }) {
* @param {object} options
*/
async function fetchFolderContents(accountId, folder, dest, options) {
try {
await fs.ensureDir(dest);
} catch (err) {
logFileSystemErrorInstance(
err,
new FileSystemErrorContext({
dest,
accountId,
write: true,
})
);
}
const files = await fetchAllPagedFiles(accountId, folder.id, options);
for (const file of files) {
await downloadFile(accountId, file, dest, options);
Expand Down

0 comments on commit a6cb8f7

Please sign in to comment.