56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
const Path = require("path");
|
|
const config = require('../config');
|
|
const sanitize = require("sanitize-filename");
|
|
const safename = require("safename");
|
|
|
|
const assetsUrl = config.cdn.hostname;
|
|
|
|
const CDN_PATHS = {
|
|
BLOG: "blog/",
|
|
CITIES: "cities/",
|
|
EVENT: "events/",
|
|
PROFILE: "profile/",
|
|
SPEAKERS: "speakers/",
|
|
WALLPAPERS: "wallpapers/"
|
|
};
|
|
|
|
const sanitizeFilename = (filename) => safename(sanitize(filename, { replacement: "_" }));
|
|
|
|
const getCDNMediaUrl = (mediaUri) => {
|
|
if (mediaUri) {
|
|
const pathParsed = mediaUri.indexOf("media") == 0 ? mediaUri.substr("media/".length, mediaUri.length) : mediaUri;
|
|
return encodeURI(encodeURI(`${assetsUrl}/${pathParsed}`));
|
|
}
|
|
else {
|
|
return encodeURI(dummyMedia);
|
|
}
|
|
}
|
|
|
|
const getCDNCityMediaUrl = (cityName) => encodeURI(`${assetsUrl}/${CDN_PATHS.CITIES}/${cityName}.jpg`);
|
|
|
|
const getCDNFilenameWithPath = (filename, type) => {
|
|
var cdnPath = '';
|
|
switch (type) {
|
|
case 'speaker':
|
|
cdnPath = CDN_PATHS.SPEAKERS;
|
|
break;
|
|
case 'event':
|
|
cdnPath = CDN_PATHS.EVENT;
|
|
break;
|
|
|
|
case 'post':
|
|
cdnPath = CDN_PATHS.BLOG;
|
|
break;
|
|
|
|
default:
|
|
throw new Error('Multimedia type unknown');
|
|
}
|
|
return Path.join(cdnPath, sanitizeFilename(filename));
|
|
}
|
|
|
|
module.exports = {
|
|
CDN_PATHS,
|
|
getCDNCityMediaUrl,
|
|
getCDNMediaUrl,
|
|
getCDNFilenameWithPath
|
|
} |