spee.ch/controllers/serveController.js

137 lines
4.8 KiB
JavaScript
Raw Normal View History

const db = require('../models');
2017-06-20 01:15:28 +02:00
const logger = require('winston');
2017-10-06 00:14:50 +02:00
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
const NO_CHANNEL = 'NO_CHANNEL';
const NO_FILE = 'NO_FILE';
module.exports = {
2017-12-05 19:18:49 +01:00
getClaimId (channelName, channelClaimId, name, claimId) {
2017-11-21 21:53:43 +01:00
if (channelName) {
2017-12-05 19:18:49 +01:00
return module.exports.getClaimIdByChannel(channelName, channelClaimId, name);
2017-11-21 21:53:43 +01:00
} else {
return module.exports.getClaimIdByClaim(name, claimId);
}
},
getClaimIdByClaim (claimName, claimId) {
logger.debug(`getClaimIdByClaim(${claimName}, ${claimId})`);
return new Promise((resolve, reject) => {
2017-11-21 21:53:43 +01:00
db.Claim.getLongClaimId(claimName, claimId) // get the long claim id
.then(result => {
resolve(result); // resolves with NO_CLAIM or valid claim id
})
.catch(error => {
reject(error);
});
});
},
2017-12-05 19:18:49 +01:00
getClaimIdByChannel (channelName, channelClaimId, claimName) {
logger.debug(`getClaimIdByChannel(${channelName}, ${channelClaimId}, ${claimName})`);
return new Promise((resolve, reject) => {
2017-12-05 19:18:49 +01:00
db.Certificate.getLongChannelId(channelName, channelClaimId) // 1. get the long channel id
2017-11-21 21:53:43 +01:00
.then(result => {
2017-10-18 19:00:24 +02:00
if (result === NO_CHANNEL) {
2017-11-21 21:53:43 +01:00
resolve(result); // resolves NO_CHANNEL
return;
}
2017-11-21 21:53:43 +01:00
return db.Claim.getClaimIdByLongChannelId(result, claimName); // 2. get the long claim id
})
2017-11-21 21:53:43 +01:00
.then(result => {
resolve(result); // resolves with NO_CLAIM or valid claim id
})
.catch(error => {
reject(error);
});
});
},
2017-12-05 19:18:49 +01:00
getChannelContents (channelName, channelClaimId) {
return new Promise((resolve, reject) => {
2017-12-05 19:18:49 +01:00
let longChannelClaimId;
let shortChannelClaimId;
db.Certificate.getLongChannelId(channelName, channelClaimId) // 1. get the long channel Id
2017-10-13 02:56:23 +02:00
.then(result => { // 2. get all claims for that channel
if (result === NO_CHANNEL) {
return NO_CHANNEL;
}
2017-12-05 19:18:49 +01:00
longChannelClaimId = result;
return db.Certificate.getShortChannelIdFromLongChannelId(longChannelClaimId, channelName);
})
2017-10-13 02:56:23 +02:00
.then(result => { // 3. get all Claim records for this channel
if (result === NO_CHANNEL) {
return NO_CHANNEL;
}
2017-12-05 19:18:49 +01:00
shortChannelClaimId = result;
return db.Claim.getAllChannelClaims(longChannelClaimId);
})
.then(result => { // 4. add extra data not available from Claim table
if (result === NO_CHANNEL) {
2017-11-21 21:53:43 +01:00
resolve(result);
return;
}
if (result) {
result.forEach(element => {
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
2017-12-05 19:18:49 +01:00
element['showUrlLong'] = `/${channelName}:${longChannelClaimId}/${element.name}`;
element['directUrlLong'] = `/${channelName}:${longChannelClaimId}/${element.name}.${fileExtenstion}`;
element['showUrlShort'] = `/${channelName}:${shortChannelClaimId}/${element.name}`;
element['directUrlShort'] = `/${channelName}:${shortChannelClaimId}/${element.name}.${fileExtenstion}`;
2017-11-27 23:52:46 +01:00
element['thumbnail'] = module.exports.chooseThumbnail(element, DEFAULT_THUMBNAIL);
});
}
resolve({
2017-09-28 19:51:02 +02:00
channelName,
2017-12-05 19:18:49 +01:00
longChannelClaimId,
shortChannelClaimId,
claims: result,
2017-09-28 19:51:02 +02:00
});
})
.catch(error => {
reject(error);
});
});
},
2017-11-21 21:53:43 +01:00
getLocalFileRecord (claimId, name) {
return db.File.findOne({where: {claimId, name}})
.then(file => {
if (!file) {
return NO_FILE;
2017-11-21 21:53:43 +01:00
}
return file.dataValues;
});
},
getClaimRecord (claimId, name) {
return db.Claim.findOne({where: {claimId, name}})
.then(claim => {
if (!claim) {
throw new Error('no record found in Claim table');
}
claim.dataValues.thumbnail = module.exports.chooseThumbnail(claim.dataValues, DEFAULT_THUMBNAIL);
2017-11-27 23:52:46 +01:00
claim.dataValues.fileExt = module.exports.determineFileExtensionFromContentType(claim.dataValues.contentType);
2017-11-21 21:53:43 +01:00
return claim.dataValues;
});
},
2017-11-27 23:52:46 +01:00
determineFileExtensionFromContentType (contentType) {
switch (contentType) {
case 'image/jpeg':
return 'jpeg';
case 'image/jpg':
return 'jpg';
case 'image/png':
return 'png';
case 'image/gif':
return 'gif';
case 'video/mp4':
return 'mp4';
default:
logger.info('showing unknown file type as image/jpeg');
return 'jpeg';
}
},
chooseThumbnail (claimInfo, defaultThumbnail) {
if (!claimInfo.thumbnail || claimInfo.thumbnail.trim() === '') {
return defaultThumbnail;
}
return claimInfo.thumbnail;
},
};