-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (53 loc) · 1.97 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// node server.js
// http://localhost:8080/card?studyUrl=https://raw.githubusercontent.com/pcrespov/osparc-sample-studies/master/Sleepers%20A%20lot/project.json
const express = require('express');
const app = express();
const https = require('https');
const svgTools = require('@iconify/tools');
const card_path = "C:/Develop/oSPARC_card/public/osparc_card.svg";
const new_card_path = "C:/Develop/oSPARC_card/public/osparc_card_custom.svg";
const sendCustomCard = (res, title, description, thumbnail) => {
svgTools.ImportSVG(card_path)
.then(svg => {
let newSVG = svg.toString();
const defTitle = "Study_Title";
const defDescription = "Study_Description";
const defThumbnail = "https://osparc.io/osparc/../resource/osparc/osparc-white.svg";
newSVG = newSVG.replace(defTitle, title || defTitle);
newSVG = newSVG.replace(defDescription, description || defDescription);
newSVG = newSVG.replace(defThumbnail, thumbnail || defThumbnail);
svgTools.ExportSVG(newSVG, new_card_path)
.then(() => {
res.sendFile(new_card_path);
})
.catch(err => {
console.error(err);
});
}).catch(err => {
console.error(err);
});
};
app.get('/card_custom', (req, res) => {
res.sendFile(new_card_path);
});
app.get('/card', (req, res) => {
const url = req.query.studyUrl;
const reqStudy = https.get(url, (resProject) => {
let data = '';
resProject.on('data', (stream) => {
data += stream;
});
resProject.on('end', () => {
const json_data = JSON.parse(data);
const title = json_data["name"];
console.log("Generating:", title);
const description = json_data["description"];
const thumbnail = json_data["thumbnail"];
sendCustomCard(res, title, description, thumbnail);
});
});
reqStudy.on('error', (e) => {
console.err('reqProject', e.message);
});
});
const server = app.listen(8080);