2017-06-19 18:37:35 +02:00
|
|
|
const db = require('../models');
|
2017-06-20 01:15:28 +02:00
|
|
|
const logger = require('winston');
|
2017-09-08 00:31:32 +02:00
|
|
|
|
2017-10-06 00:14:50 +02:00
|
|
|
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
|
2017-10-18 00:09:43 +02:00
|
|
|
const NO_CHANNEL = 'NO_CHANNEL';
|
2017-08-02 22:16:39 +02:00
|
|
|
|
2017-09-12 02:57:51 +02:00
|
|
|
function chooseThumbnail (claimInfo, defaultThumbnail) {
|
2017-09-28 19:51:02 +02:00
|
|
|
if (!claimInfo.thumbnail || claimInfo.thumbnail.trim() === '') {
|
2017-09-12 02:57:51 +02:00
|
|
|
return defaultThumbnail;
|
|
|
|
}
|
|
|
|
return claimInfo.thumbnail;
|
|
|
|
}
|
|
|
|
|
2017-08-02 22:16:39 +02:00
|
|
|
module.exports = {
|
2017-11-21 21:53:43 +01:00
|
|
|
getClaimId (channelName, channelId, name, claimId) {
|
|
|
|
if (channelName) {
|
|
|
|
return module.exports.getClaimIdByChannel(channelName, channelId, name);
|
|
|
|
} else {
|
|
|
|
return module.exports.getClaimIdByClaim(name, claimId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getClaimIdByClaim (claimName, claimId) {
|
|
|
|
logger.debug(`getClaimIdByClaim(${claimName}, ${claimId})`);
|
2017-08-02 03:58:13 +02:00
|
|
|
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
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-11-21 21:53:43 +01:00
|
|
|
getClaimIdByChannel (channelName, channelId, claimName) {
|
|
|
|
logger.debug(`getClaimIdByChannel(${channelName}, ${channelId}, ${claimName})`);
|
2017-08-23 00:50:20 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-01 01:19:26 +01:00
|
|
|
db.Certificate.getLongChannelId(channelName, channelId) // 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
|
2017-10-18 00:50:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-11-21 21:53:43 +01:00
|
|
|
return db.Claim.getClaimIdByLongChannelId(result, claimName); // 2. get the long claim id
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
2017-11-21 21:53:43 +01:00
|
|
|
.then(result => {
|
|
|
|
resolve(result); // resolves with NO_CLAIM or valid claim id
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-08-24 01:01:28 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
getChannelContents (channelName, channelId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let longChannelId;
|
|
|
|
let shortChannelId;
|
2017-11-01 01:19:26 +01:00
|
|
|
db.Certificate.getLongChannelId(channelName, channelId) // 1. get the long channel Id
|
2017-10-13 02:56:23 +02:00
|
|
|
.then(result => { // 2. get all claims for that channel
|
2017-10-18 00:09:43 +02:00
|
|
|
if (result === NO_CHANNEL) {
|
|
|
|
return NO_CHANNEL;
|
|
|
|
}
|
2017-09-08 02:08:06 +02:00
|
|
|
longChannelId = result;
|
2017-10-31 18:05:15 +01:00
|
|
|
return db.Certificate.getShortChannelIdFromLongChannelId(longChannelId, channelName);
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
2017-10-13 02:56:23 +02:00
|
|
|
.then(result => { // 3. get all Claim records for this channel
|
2017-10-18 00:09:43 +02:00
|
|
|
if (result === NO_CHANNEL) {
|
|
|
|
return NO_CHANNEL;
|
|
|
|
}
|
2017-09-08 02:08:06 +02:00
|
|
|
shortChannelId = result;
|
2017-10-31 19:54:33 +01:00
|
|
|
return db.Claim.getAllChannelClaims(longChannelId);
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
2017-10-18 00:09:43 +02:00
|
|
|
.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);
|
2017-10-18 00:09:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
result.forEach(element => {
|
2017-09-11 20:09:38 +02:00
|
|
|
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
|
|
|
|
element['showUrlLong'] = `/${channelName}:${longChannelId}/${element.name}`;
|
|
|
|
element['directUrlLong'] = `/${channelName}:${longChannelId}/${element.name}.${fileExtenstion}`;
|
2017-10-18 18:49:19 +02:00
|
|
|
element['showUrlShort'] = `/${channelName}:${shortChannelId}/${element.name}`;
|
2017-09-11 20:09:38 +02:00
|
|
|
element['directUrlShort'] = `/${channelName}:${shortChannelId}/${element.name}.${fileExtenstion}`;
|
2017-09-12 02:57:51 +02:00
|
|
|
element['thumbnail'] = chooseThumbnail(element, DEFAULT_THUMBNAIL);
|
2017-09-08 02:08:06 +02:00
|
|
|
});
|
|
|
|
}
|
2017-10-18 00:09:43 +02:00
|
|
|
resolve({
|
2017-09-28 19:51:02 +02:00
|
|
|
channelName,
|
|
|
|
longChannelId,
|
|
|
|
shortChannelId,
|
2017-10-18 00:09:43 +02:00
|
|
|
claims: result,
|
2017-09-28 19:51:02 +02:00
|
|
|
});
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-08-23 00:50:20 +02:00
|
|
|
});
|
|
|
|
},
|
2017-11-21 21:53:43 +01:00
|
|
|
getLocalFileRecord (claimId, name) {
|
|
|
|
return db.File.findOne({where: {claimId, name}})
|
|
|
|
.then(file => {
|
|
|
|
if (!file) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
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 = chooseThumbnail(claim.dataValues.thumbnail, DEFAULT_THUMBNAIL);
|
|
|
|
return claim.dataValues;
|
|
|
|
});
|
2017-08-24 01:01:28 +02:00
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|