Skip to content

Commit 44676ae

Browse files
committed
remove unsuded files
1 parent afb207c commit 44676ae

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function slugify(text: string) {
2+
const a = 'àáäâèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;';
3+
const b = 'aaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------';
4+
const p = new RegExp(a.split('').join('|'), 'g');
5+
6+
/* eslint-disable */
7+
return text
8+
.toString()
9+
.toLowerCase()
10+
.replace(/\s+/g, '-') // Replace spaces with -
11+
.replace(p, c => b.charAt(a.indexOf(c))) // Replace special chars
12+
.replace(/&/g, '-and-') // Replace & with 'and'
13+
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
14+
.replace(/\-\-+/g, '-') // Replace multiple - with single -
15+
.replace(/^-+/, '') // Trim - from start of text
16+
.replace(/-+$/, ''); // Trim - from end of text
17+
/* eslint-enable */
18+
}
19+
20+
export function fileDownload(data: any, filename: string) {
21+
const blob = new Blob(data, { type: 'application/zip' });
22+
const blobURL = window.URL.createObjectURL(blob);
23+
const tempLink = document.createElement('a');
24+
tempLink.style.display = 'none';
25+
tempLink.href = blobURL;
26+
tempLink.setAttribute('download', `${slugify(filename)}.zip`);
27+
28+
// Safari thinks _blank anchor are pop ups. We only want to set _blank
29+
// target if the browser does not support the HTML5 download attribute.
30+
// This allows you to download files in desktop safari if pop up blocking
31+
// is enabled.
32+
if (typeof tempLink.download === 'undefined') {
33+
tempLink.setAttribute('target', '_blank');
34+
}
35+
36+
document.body.appendChild(tempLink);
37+
tempLink.click();
38+
39+
// Fixes "webkit blob resource error 1"
40+
setTimeout(() => {
41+
document.body.removeChild(tempLink);
42+
window.URL.revokeObjectURL(blobURL);
43+
}, 0);
44+
}
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
import { Sandbox } from '@codesandbox/common/lib/types';
1+
import axios from 'axios';
2+
import store from 'store/dist/store.modern';
3+
import { fileDownload } from './fileDownload';
4+
5+
const getFile = async (id: string) => {
6+
const jwt =
7+
store.get('jwt') || (document.cookie.match(/[; ]?jwt=([^\s;]*)/) || [])[1];
8+
const file = await axios.get(`http://localhost:8000/${id}`, {
9+
headers: {
10+
Authorization: `Bearer ${jwt}`,
11+
},
12+
});
13+
return file.data;
14+
};
215

316
export default {
4-
create(sandbox: Sandbox) {
5-
return import(
6-
/* webpackChunkName: 'create-zip' */ './create-zip'
7-
).then(module =>
8-
module
9-
.getZip(sandbox, sandbox.modules, sandbox.directories)
10-
.then(result => ({ file: result.file }))
11-
);
17+
create({ id }, { id: string }) {
18+
return getFile(id);
1219
},
13-
download(sandbox: Sandbox) {
14-
return import(
15-
/* webpackChunkName: 'create-zip' */ './create-zip'
16-
).then(module =>
17-
module
18-
.default(sandbox, sandbox.modules, sandbox.directories)
19-
.then(file => ({ file }))
20-
);
20+
async download({ title, id }: { title: string; id: string }) {
21+
const file = await getFile(id);
22+
fileDownload(file.data, title || id);
2123
},
2224
};

0 commit comments

Comments
 (0)