Skip to content

Commit 7e26fb0

Browse files
committed
avoid creation of plugin with spaces in name
1 parent 22ea6a7 commit 7e26fb0

File tree

2 files changed

+77
-40
lines changed

2 files changed

+77
-40
lines changed

server/routers/plugins.js

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,44 @@ router.get('/', (req, res) => {
5353
})
5454
});
5555

56-
router.get('/:id', (req, res) => {
56+
function getSources(pluginId) {
5757
const { s3, Bucket } = S3.state()
5858

59-
const filename = req.params.id;
60-
6159
const params = {
6260
Bucket,
63-
Key: `${filename}.zip`
61+
Key: `${pluginId}.zip`
6462
}
6563

66-
s3.send(new GetObjectCommand(params))
64+
return s3.send(new GetObjectCommand(params))
6765
.then(data => new fetch.Response(data.Body).buffer())
6866
.then(data => {
69-
res.attachment('plugin.zip');
70-
res.send(data);
67+
return {
68+
status: 200,
69+
body: data
70+
}
7171
})
7272
.catch((err) => {
73-
res
74-
.status(err.$metadata.httpStatusCode)
75-
.json({
73+
return {
74+
status: err.$metadata.httpStatusCode,
75+
body: {
7676
error: err.Code,
7777
status: err.$metadata.httpStatusCode
78-
})
78+
}
79+
}
80+
})
81+
}
82+
83+
router.get('/:id', (req, res) => {
84+
getSources(req.params.id)
85+
.then(out => {
86+
if (out.status === 200) {
87+
res.attachment('plugin.zip');
88+
res.send(out.body);
89+
} else {
90+
res
91+
.status(out.status)
92+
.json(out.body)
93+
}
7994
})
8095
})
8196

@@ -112,7 +127,6 @@ router.get('/:id/configurations', (req, res) => {
112127
])
113128
})
114129
.catch(err => {
115-
console.log(err)
116130
res.json(files)
117131
})
118132
})
@@ -215,7 +229,7 @@ function createEmptyPlugin(req, metadata) {
215229
const plugins = [
216230
...(data.plugins || []),
217231
{
218-
filename: metadata.name,
232+
filename: metadata.name.replace(/ /g, '-'),
219233
type: metadata.type,
220234
pluginId: pluginId
221235
}
@@ -286,7 +300,6 @@ function updatePluginContent(id, body) {
286300
}
287301

288302
router.post('/', async (req, res) => {
289-
290303
if (!req.body ||
291304
Object.keys(req.body).length === 0 ||
292305
(!req.body.metadata && !(req.body.plugin && req.body.type))) {
@@ -301,11 +314,20 @@ router.post('/', async (req, res) => {
301314
if (out.status !== 201 || !req.body.files) {
302315
return res.status(out.status).json(out.body);
303316
} else {
304-
const templatesFiles = await FileSystem.templatesFilesToJSON(req.body.metadata.type);
317+
const templatesFiles = await FileSystem.templatesFilesToJSON(req.body.metadata.type, req.body.metadata.name.replace(/ /g, '-'));
305318
const zip = await FileSystem.createZipFromJSONFiles(req.body.files, templatesFiles);
306-
updatePluginContent(out.body.plugins[out.body.plugins.length - 1].pluginId, zip)
319+
const pluginId = out.body.plugins[out.body.plugins.length - 1].pluginId;
320+
updatePluginContent(pluginId, zip)
307321
.then(out => {
308-
res.status(out.status).json(out.body);
322+
if (out.status === 204) {
323+
res.status(201)
324+
.json({
325+
plugin_id: pluginId
326+
})
327+
} else {
328+
res.status(out.status)
329+
.json(out.body);
330+
}
309331
});
310332
}
311333
})
@@ -396,7 +418,7 @@ router.post('/build', async (req, res) => {
396418
Buffer.from(zip),
397419
folder,
398420
[
399-
{ key: '@@PLUGIN_NAME@@', value: metadata.name },
421+
{ key: '@@PLUGIN_NAME@@', value: metadata.name.replace(/ /g, '-') },
400422
{ key: '@@PLUGIN_VERSION@@', value: metadata.version || '1.0.0' }
401423
])
402424

@@ -409,7 +431,7 @@ router.post('/build', async (req, res) => {
409431
addPluginToBuildQueue(
410432
folder,
411433
{
412-
filename: metadata.name,
434+
filename: metadata.name.replace(/ /g, '-'),
413435
type: kind,
414436
pluginId,
415437
last_hash: " ",
@@ -515,12 +537,28 @@ router.post('/:id/build', async (req, res) => {
515537
res.json({ queue_id: pluginId, alreadyExists: true });
516538
} else {
517539
const folder = await FileSystem.createBuildFolder(plugin.type, pluginId);
518-
await unzip(isRustBuild, req.body, folder)
540+
541+
let sources = req.body;
542+
if (!sources || Object.keys(sources).length === 0) {
543+
await getSources(pluginId)
544+
.then(out => {
545+
if (out.status === 200) {
546+
sources = out.body;
547+
} else {
548+
res
549+
.status(out.status)
550+
.json(out.body)
551+
}
552+
})
553+
}
554+
555+
await unzip(isRustBuild, sources, folder)
519556
.catch(() => res.status(400).json({ error: 'failed to unzip file' }));
557+
520558
try {
521559
const zipHash = crypto
522560
.createHash('md5')
523-
.update(req.body.toString())
561+
.update(sources.toString())
524562
.digest('hex');
525563

526564
if (release || plugin['last_hash'] !== zipHash) {

server/services/file-system.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,30 @@ const getLocalWasm = (id, res) => {
101101

102102
const createZipFromJSONFiles = (jsonFiles, templatesFiles) => {
103103
const zip = new AdmZip();
104-
[...templatesFiles, ...jsonFiles].forEach(({ name, content }) => {
105-
zip.addFile(name, content)
106-
})
104+
105+
console.log(templatesFiles)
106+
console.log(jsonFiles);
107+
[
108+
...templatesFiles.filter(t => !jsonFiles.find(f => f.name === t.name)),
109+
...jsonFiles].forEach(({ name, content }) => {
110+
zip.addFile(name, content)
111+
})
107112

108113
return zip.toBuffer()
109114
}
110115

111-
const templatesFilesToJSON = type => {
112-
const folder = path.join(process.cwd(), 'templates', 'builds', type);
116+
const templatesFilesToJSON = (type, name) => {
117+
const folder = path.join(process.cwd(), 'templates', `${type}.zip`);
113118

114-
return new Promise(resolve => fs.readdir(folder, (err, filenames) => {
115-
if (err) {
116-
return [];
117-
}
119+
const zip = new AdmZip(folder);
118120

119-
Promise.all(filenames.map(name => new Promise(resolve => {
120-
fs.readFile(path.join(folder, name), 'utf-8', (err, content) => {
121-
if (err) {
122-
resolve({ name, content: "" })
123-
}
124-
resolve({ name, content })
125-
});
126-
})))
127-
.then(resolve)
128-
}))
121+
return zip.getEntries()
122+
.map(entry => ({
123+
name: entry.entryName.replace(`${type}/`, ''),
124+
content: entry.getData().toString('utf-8')
125+
.replace('@@PLUGIN_NAME@@', name)
126+
.replace('@@PLUGIN_VERSION@@', '1.0.0')
127+
}))
129128

130129
}
131130

0 commit comments

Comments
 (0)