Server: Fix response in buildAll. Fix file name after minify.

This commit is contained in:
Konstantinos Triantafyllou 2022-07-25 11:52:56 +03:00
parent 27f20e1918
commit a8c28d91f4
3 changed files with 57 additions and 45 deletions

View File

@ -12,6 +12,7 @@ import {REQUEST, RESPONSE} from "./src/app/openaireLibrary/utils/tokens";
import {isArray} from "util";
import {properties} from "./src/environments/environment";
import {Layout} from "./src/app/openaireLibrary/connect/community/CustomizationOptions";
import {Response} from "express";
const fs = require('fs');
const less = require('less');
@ -25,9 +26,9 @@ var bodyParser = require('body-parser')
var jsonParser = bodyParser.json()
function buildCss(portal: string, suffix =null, variables: {} = null) {
function buildCss(portal: string, suffix = null, variables: {} = null) {
let lessFile = 'community.less'
if(portal === 'connect') {
if (portal === 'connect') {
lessFile = 'connect.less'
}
let input = fs.readFileSync(browser + '/assets/' + lessFile, 'utf8');
@ -37,7 +38,7 @@ function buildCss(portal: string, suffix =null, variables: {} = null) {
'@font-media-icon-url': 'e("assets/openaire-theme/media/fonts/material-icons/")'
}
/* Change variables (optional)*/
if(variables) {
if (variables) {
Object.entries(variables).forEach(([key, value]) => {
modifyVars[key] = value;
});
@ -48,20 +49,21 @@ function buildCss(portal: string, suffix =null, variables: {} = null) {
modifyVars: modifyVars
};
less.render(input, options, function (error, result) {
if(error) {
if (error) {
console.log(error);
} else {
fs.writeFile(browser + portal + (suffix?("-" + suffix):"") + '.css', result.css, function (error) {
if(error) {
let file = browser + portal + (suffix ? ("-" + suffix) : "") + '.css';
fs.writeFile(file, result.css, function (error) {
if (error) {
console.error(error);
} else {
minify({
compressor: cleanCSS,
replaceInPlace: true,
input: browser + portal + '.css',
output: browser + portal + '.css',
callback: function(err) {
if(err) {
input: file,
output: file,
callback: function (err) {
if (err) {
console.log(err);
}
}
@ -71,22 +73,29 @@ function buildCss(portal: string, suffix =null, variables: {} = null) {
}
});
}
function buildAll(){
function buildAll(res: Response = null) {
let layoutsURL = properties.adminToolsAPIURL + '/community/layouts';
axios.get(layoutsURL).then(response => {
if(response.data && Array.isArray(response.data) && response.data.length > 0) {
if (response.data && Array.isArray(response.data) && response.data.length > 0) {
response.data.forEach((layout: Layout) => {
let variables = Layout.getVariables(layout.layoutOptions);
if(variables) {
buildCss(layout.portalPid,layout.date?layout.date:null, variables);
if (variables) {
buildCss(layout.portalPid, layout.date ? layout.date : null, variables);
}
});
return ('CSS build for all available layouts was successful');
if (res) {
res.status(200).send({
code: 200,
message: 'CSS build for all available layouts was successful'
});
}
} else {
return ('No available layouts found');
res.status(500).send({code: 500, message: 'No available layouts found'});
}
});
}
buildCss('connect');
buildCss('default');
buildAll();
@ -115,15 +124,15 @@ export function app() {
});
server.use(function (req, res, next) {
var XFRAME_WHITELIST = [ 'http://spitoo.di.uoa.gr:5000/','http://scoobydoo.di.uoa.gr:5000/', 'https://beta.admin.connect.openaire.eu/','https://admin.connect.openaire.eu/' ];
var XFRAME_WHITELIST = ['http://spitoo.di.uoa.gr:5000/', 'http://scoobydoo.di.uoa.gr:5000/', 'https://beta.admin.connect.openaire.eu/', 'https://admin.connect.openaire.eu/'];
let referer: string;
if(req.headers.referer){
referer = isArray(req.headers.referer)?req.headers.referer[0]:(<string>req.headers.referer);
if (req.headers.referer) {
referer = isArray(req.headers.referer) ? req.headers.referer[0] : (<string>req.headers.referer);
referer = referer.split("?")[0];
}
if ( referer && (XFRAME_WHITELIST.indexOf(referer) != -1 || referer.indexOf("/customize-layout") !=-1 || referer.indexOf(".d4science.org") != -1)) {
if (referer && (XFRAME_WHITELIST.indexOf(referer) != -1 || referer.indexOf("/customize-layout") != -1 || referer.indexOf(".d4science.org") != -1)) {
// res.header('X-FRAME-OPTIONS', 'allow from ' +req.headers.referer);
}else {
} else {
res.header('X-FRAME-OPTIONS', 'SAMEORIGIN');
}
next();
@ -132,20 +141,20 @@ export function app() {
// - Serve sitemap based on the host -
server.get('/sitemap.xml', (req, res) => {
let host = req.get("host").split(":")[0];
let connectLinks= ["/","/about/learn-how","/about/learn-in-depth", "/about/faq","/search/find/communities"];
let communityLinks= ["/","/search/find/research-outcomes?size=20","/search/advanced/research-outcomes?size=20","/participate/deposit/learn-how","/organizations", "/content"];
let connectLinks = ["/", "/about/learn-how", "/about/learn-in-depth", "/about/faq", "/search/find/communities"];
let communityLinks = ["/", "/search/find/research-outcomes?size=20", "/search/advanced/research-outcomes?size=20", "/participate/deposit/learn-how", "/organizations", "/content"];
let sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">";
let urlPre = "<url>\n" +
" <loc>";
let urlSuf = "</loc>\n" +
" </url>";
for(let link of (host.indexOf("connect.openaire.eu") == -1 ?communityLinks:connectLinks)){
for (let link of (host.indexOf("connect.openaire.eu") == -1 ? communityLinks : connectLinks)) {
sitemap += urlPre + "https://" + host + link + urlSuf;
}
sitemap += "\n</urlset>";
res.setHeader('content-type', 'application/xml');
res.send( sitemap);
res.send(sitemap);
});
// - Serve robots based on the host -
@ -174,34 +183,37 @@ export function app() {
});
server.post('/build-css/all', (req, res) => {
res.send(buildAll())
buildAll(res);
});
server.post('/build-css/:id/:suffix', (req, res) => {
let layoutURL = properties.adminToolsAPIURL + '/community/' + req.params.id + '/layout';
axios.get(layoutURL).then(response => {
if(response.data) {
if (response.data) {
let variables = Layout.getVariables(response.data);
if(variables) {
buildCss(response.data.portalPid,req.params.suffix, variables);
if (variables) {
buildCss(response.data.portalPid, req.params.suffix, variables);
}
res.status(200).send({code:200, message: 'CSS build for ' + response.data.portalPid + ' layouts was successful'});
} else {
res.status(500).send({code:500, message:'No layout found'});
}
}).catch(function (error) {
res.status(500).send({code:500, message:'No layout found'});
});
});
server.post('/build-css/preview/:id/:suffix', jsonParser, (req, res) => {
let layoutURL = properties.adminToolsAPIURL + '/community/' + req.params.id + '/layout';
let variables = Layout.getVariables(req.body);
if(variables) {
buildCss(req.params.id,req.params.suffix, variables);
res.status(200).send({code:200, message: 'CSS build for ' + req.params.id + ' layouts was successful'});
res.status(200).send({
code: 200,
message: 'CSS build for ' + response.data.portalPid + ' layout was successful'
});
} else {
res.status(500).send({code: 500, message: 'No layout found'});
}
}).catch(function (error) {
res.status(500).send({code: 500, message: 'No layout found'});
});
});
server.post('/build-css/preview/:id/:suffix', jsonParser, (req, res) => {
let variables = Layout.getVariables(req.body);
if (variables) {
buildCss(req.params.id, req.params.suffix, variables);
res.status(200).send({code: 200, message: 'CSS build for ' + req.params.id + ' layout was successful'});
} else {
res.status(500).send({code: 500, message: 'No layout found'});
}
});
// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });

@ -1 +1 @@
Subproject commit 8e09a2c68de2f248fd38f370a067073280f30165
Subproject commit 71437f208599735b6eb9ee9fe769111c9f26e3ee

@ -1 +1 @@
Subproject commit 5f6ce90db8213df9d88cff0231b0913543d2ed36
Subproject commit f10d1dd1350f2da69eecb3bb43880c0de4705399