@@ -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
288302router.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) {
0 commit comments