test downloads for only non s3 files

This commit is contained in:
catarak 2016-09-02 20:40:18 -04:00
parent 69bb0d0077
commit e94ba201ce

View file

@ -116,26 +116,72 @@ export function createProject() {
}; };
} }
function buildZip(state) {
const zip = new JSZip();
const rootFile = state.files.find(file => file.name === 'root');
const numFiles = state.files.filter(file => file.fileType !== 'folder').length;
const files = state.files;
const projectName = state.project.name;
let numCompletedFiles = 0;
function addFileToZip(file, path) {
if (file.fileType === 'folder') {
const newPath = file.name === 'root' ? path : `${path}${file.name}/`;
file.children.forEach(fileId => {
const childFile = files.find(f => f.id === fileId);
(() => {
addFileToZip(childFile, newPath);
})();
});
} else {
if (file.url) {
JSZipUtils.getBinaryContent(file.url, (err, data) => {
zip.file(`${path}${file.name}`, data, { binary: true });
numCompletedFiles += 1;
if (numCompletedFiles === numFiles) {
zip.generateAsync({ type: 'blob' }).then((content) => {
saveAs(content, `${projectName}.zip`);
});
}
});
} else {
console.log('adding', `${path}${file.name}`);
zip.file(`${path}${file.name}`, file.content);
numCompletedFiles += 1;
console.log('numFiles', numFiles);
console.log('numCompletedFiles', numCompletedFiles);
if (numCompletedFiles === numFiles) {
zip.generateAsync({ type: 'blob' }).then((content) => {
saveAs(content, `${projectName}.zip`);
});
}
}
}
}
addFileToZip(rootFile, '/');
}
export function exportProjectAsZip() { export function exportProjectAsZip() {
return (dispatch, getState) => { return (dispatch, getState) => {
const state = getState(); const state = getState();
const zip = new JSZip(); buildZip(state);
async.each(state.files, (file, cb) => { // async.each(state.files, (file, cb) => {
if (file.url) { // if (file.url) {
JSZipUtils.getBinaryContent(file.url, (err, data) => { // JSZipUtils.getBinaryContent(file.url, (err, data) => {
zip.file(file.name, data, { binary: true }); // zip.file(file.name, data, { binary: true });
cb(); // cb();
}); // });
} else { // } else {
zip.file(file.name, file.content); // zip.file(file.name, file.content);
cb(); // cb();
} // }
}, err => { // }, err => {
if (err) console.log(err); // if (err) console.log(err);
zip.generateAsync({ type: 'blob' }).then((content) => { // zip.generateAsync({ type: 'blob' }).then((content) => {
saveAs(content, `${state.project.name}.zip`); // saveAs(content, `${state.project.name}.zip`);
}); // });
}); // });
// };
}; };
} }