Skip to content

Download all sketches of a user as a single zip #2707

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions client/modules/IDE/actions/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ export function exportProjectAsZip(projectId) {
win.focus();
}

export function exportAllProjectsAsZip(username) {
const win = window.open(`${ROOT_URL}/${username}/downloadall`, '_blank');
win.focus();
}

export function resetProject() {
return {
type: ActionTypes.RESET_PROJECT
Expand Down
6 changes: 6 additions & 0 deletions client/modules/IDE/hooks/useSketchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useParams } from 'react-router';
import {
autosaveProject,
exportProjectAsZip,
exportAllProjectsAsZip,
newProject,
saveProject,
setProjectName
Expand Down Expand Up @@ -44,6 +45,10 @@ const useSketchActions = () => {
exportProjectAsZip(project.id);
}

function downloadAllSketches() {
exportAllProjectsAsZip(params.username);
}

function shareSketch() {
const { username } = params;
dispatch(showShareModal(project.id, project.name, username));
Expand All @@ -61,6 +66,7 @@ const useSketchActions = () => {
newSketch,
saveSketch,
downloadSketch,
downloadAllSketches,
shareSketch,
changeSketchName,
canEditProjectName
Expand Down
10 changes: 10 additions & 0 deletions client/modules/User/pages/DashboardView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import CollectionList from '../../IDE/components/CollectionList';
import SketchList from '../../IDE/components/SketchList';
import RootPage from '../../../components/RootPage';
import { newProject } from '../../IDE/actions/project';
import { useSketchActions } from '../../IDE/hooks';
import {
CollectionSearchbar,
SketchSearchbar
Expand All @@ -35,10 +36,16 @@ const DashboardView = () => {

const [collectionCreateVisible, setCollectionCreateVisible] = useState(false);

const { downloadAllSketches } = useSketchActions();

const createNewSketch = () => {
dispatch(newProject());
};

const downloadAll = () => {
downloadAllSketches(params.username);
};

const selectedTabKey = useCallback(() => {
const path = location.pathname;

Expand Down Expand Up @@ -89,6 +96,9 @@ const DashboardView = () => {
{t('DashboardView.NewSketch')}
</Button>
)}
<Button onClick={downloadAll}>
{t('DashboardView.DownloadAll')}
</Button>
<SketchSearchbar />
</>
);
Expand Down
73 changes: 71 additions & 2 deletions server/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Project from '../models/project';
import User from '../models/user';
import { resolvePathToFile } from '../utils/filePath';
import generateFileSystemSafeName from '../utils/generateFileSystemSafeName';
import { getProjectsForUserName } from './project.controller/getProjectsForUser';

export {
default as createProject,
Expand Down Expand Up @@ -215,6 +216,41 @@ function bundleExternalLibs(project) {
});
}

function bundleExternalLibsForCompiledProject(compilation) {
const { files } = compilation;
const htmlFiles = [];
const projectFolders = [];
files.forEach((file) => {
if (file.name === 'index.html') htmlFiles.push(file);
else if (file.fileType === 'folder' && file.name !== 'root')
projectFolders.push(file);
});
htmlFiles.forEach((indexHtml) => {
const { window } = new JSDOM(indexHtml.content);
const scriptTags = window.document.getElementsByTagName('script');

const parentFolder = projectFolders.filter((folder) =>
folder.children.includes(indexHtml.id)
);

Object.values(scriptTags).forEach(async ({ src }, i) => {
if (!isUrl(src)) return;

const path = src.split('/');
const filename = path[path.length - 1];
const libId = `${filename}-${parentFolder[0].name}`;

compilation.files.push({
name: filename,
url: src,
id: libId
});

parentFolder[0].children.push(libId);
});
});
}

function addFileToZip(file, files, zip, path = '') {
return new Promise((resolve, reject) => {
if (file.fileType === 'folder') {
Expand Down Expand Up @@ -259,7 +295,7 @@ function addFileToZip(file, files, zip, path = '') {
});
}

async function buildZip(project, req, res) {
async function buildZip(project, req, res, isSingleProject = true) {
try {
const zip = new JSZip();
const currentTime = format(new Date(), 'yyyy_MM_dd_HH_mm_ss');
Expand All @@ -270,7 +306,11 @@ async function buildZip(project, req, res) {
const { files } = project;
const root = files.find((file) => file.name === 'root');

bundleExternalLibs(project);
if (isSingleProject) {
bundleExternalLibs(project);
} else {
bundleExternalLibsForCompiledProject(project);
}
await addFileToZip(root, files, zip);

const base64 = await zip.generateAsync({ type: 'base64' });
Expand All @@ -292,3 +332,32 @@ export function downloadProjectAsZip(req, res) {
buildZip(project, req, res);
});
}

function compileProjects(projects, compiledName) {
const compiledFolder = {
name: 'root',
fileType: 'folder',
children: []
};

const compiledProject = {
name: compiledName,
files: [compiledFolder]
};

projects.forEach((project) => {
const rootFile = project.files.find((file) => file.name === 'root');
rootFile.name = project.name;
compiledProject.files.push(...project.files);
compiledFolder.children.push(rootFile.id);
});

return compiledProject;
}

export function downloadAllProjectsAsZip(req, res) {
getProjectsForUserName(req.params.username).then((projects) => {
const compilation = compileProjects(projects, req.params.username);
buildZip(compilation, req, res, false);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import createApplicationErrorClass from '../../utils/createApplicationErrorClass

const UserNotFoundError = createApplicationErrorClass('UserNotFoundError');

function getProjectsForUserName(username) {
export function getProjectsForUserName(username) {
return new Promise((resolve, reject) => {
User.findByUsername(username, (err, user) => {
if (err) {
Expand Down
5 changes: 5 additions & 0 deletions server/routes/project.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ router.get('/:username/projects', ProjectController.getProjectsForUser);

router.get('/projects/:project_id/zip', ProjectController.downloadProjectAsZip);

router.get(
'/:username/downloadall',
ProjectController.downloadAllProjectsAsZip
);

export default router;
3 changes: 2 additions & 1 deletion translations/locales/en-US/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@
"DashboardView": {
"CreateCollection": "Create collection",
"NewSketch": "New sketch",
"CreateCollectionOverlay": "Create collection"
"CreateCollectionOverlay": "Create collection",
"DownloadAll": "Download all Sketches"
},
"DashboardTabSwitcher": {
"Sketches": "Sketches",
Expand Down