Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when fetching filemanger to ./ #392

Merged
merged 8 commits into from
Dec 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cms-lib/api/fileManager.js
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ async function fetchFiles(accountId, folderId, { offset, archived }) {
qs: {
hidden: 0,
offset: offset,
folder_id: folderId || 'None',
folder_id: folderId,
...(!archived && { archived: 0 }),
},
});
@@ -48,7 +48,7 @@ async function fetchFolders(accountId, folderId) {
uri: `${FILE_MANAGER_API_PATH}/folders/`,
qs: {
hidden: 0,
parent_folder_id: folderId || 'None',
parent_folder_id: folderId,
},
});
}
53 changes: 29 additions & 24 deletions packages/cms-lib/fileManager.js
Original file line number Diff line number Diff line change
@@ -103,7 +103,6 @@ async function downloadFile(accountId, file, dest, options) {
if (await skipExisting(options.overwrite, destPath)) {
return;
}

try {
await http.getOctetStream(
accountId,
@@ -113,7 +112,6 @@ async function downloadFile(accountId, file, dest, options) {
},
destPath
);
logger.log(`Wrote file "${destPath}"`);
} catch (err) {
logErrorInstance(err);
}
@@ -184,22 +182,28 @@ async function fetchFolderContents(accountId, folder, dest, options) {
* @param {number} accountId
* @param {string} src
* @param {string} dest
* @param {object} file
* @param {object} folder
* @param {object} options
*/
async function downloadFolder(accountId, src, dest, folder, options) {
try {
const rootPath =
dest === getCwd()
? convertToLocalFileSystemPath(path.resolve(dest, folder.name))
: dest;
let absolutePath;

if (folder.name) {
absolutePath = convertToLocalFileSystemPath(
path.resolve(getCwd(), dest, folder.name)
);
} else {
absolutePath = convertToLocalFileSystemPath(path.resolve(getCwd(), dest));
}

logger.log(
'Fetching folder from "%s" to "%s" in the File Manager of account %s',
src,
rootPath,
absolutePath,
accountId
);
await fetchFolderContents(accountId, folder, rootPath, options);
await fetchFolderContents(accountId, folder, absolutePath, options);
logger.success(
'Completed fetch of folder "%s" to "%s" from the File Manager',
src,
@@ -259,26 +263,27 @@ async function downloadSingleFile(accountId, src, dest, file, options) {
* @param {object} options
*/
async function downloadFileOrFolder(accountId, src, dest, options) {
if (src === '/') {
await downloadFolder(accountId, src, dest, '', options);
} else {
try {
try {
if (src == '/') {
// Filemanager API treats 'None' as the root
const rootFolder = { id: 'None' };
await downloadFolder(accountId, src, dest, rootFolder, options);
} else {
const { file, folder } = await fetchStat(accountId, src);

if (file) {
downloadSingleFile(accountId, src, dest, file, options);
await downloadSingleFile(accountId, src, dest, file, options);
} else if (folder) {
downloadFolder(accountId, src, dest, folder, options);
await downloadFolder(accountId, src, dest, folder, options);
}
} catch (err) {
logApiErrorInstance(
err,
new ApiErrorContext({
request: src,
accountId,
})
);
}
} catch (err) {
logApiErrorInstance(
err,
new ApiErrorContext({
request: src,
accountId,
})
);
}
}

5 changes: 5 additions & 0 deletions packages/cms-lib/http.js
Original file line number Diff line number Diff line change
@@ -150,6 +150,11 @@ const createGetRequestStream = ({ contentType }) => async (
req.on('error', reject);
req.on('response', res => {
if (res.statusCode >= 200 && res.statusCode < 300) {
try {
fs.ensureFileSync(filepath);
} catch (err) {
reject(err);
}
const writeStream = fs.createWriteStream(filepath, {
encoding: 'binary',
});