.
This commit is contained in:
parent
9d717cd7c5
commit
86ff89e2b5
87
helpers/providers.helper.js
Normal file
87
helpers/providers.helper.js
Normal file
@ -0,0 +1,87 @@
|
||||
const config = require('../config');
|
||||
const Vimeo = require('vimeo').Vimeo;
|
||||
const client = new Vimeo(config.vimeo.CLIENT_ID, config.vimeo.CLIENT_SECRET, config.vimeo.ACCESS_TOKEN);
|
||||
|
||||
function parseVideo(url) {
|
||||
// - Supported YouTube URL formats:
|
||||
// - http://www.youtube.com/watch?v=My2FRPA3Gf8
|
||||
// - http://youtu.be/My2FRPA3Gf8
|
||||
// - https://youtube.googleapis.com/v/My2FRPA3Gf8
|
||||
// - Supported Vimeo URL formats:
|
||||
// - http://vimeo.com/25451551
|
||||
// - http://player.vimeo.com/video/25451551
|
||||
// - Also supports relative URLs:
|
||||
// - //player.vimeo.com/video/25451551
|
||||
|
||||
var type = undefined;
|
||||
url.match(/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/);
|
||||
|
||||
if (RegExp.$3.indexOf('youtu') > -1) {
|
||||
type = 'youtube';
|
||||
} else if (RegExp.$3.indexOf('vimeo') > -1) {
|
||||
type = 'vimeo';
|
||||
}
|
||||
|
||||
return {
|
||||
type: type,
|
||||
id: RegExp.$6,
|
||||
class: type ? 'video' : 'unknown',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function getIframeSource(iframeHtml) {
|
||||
const groups = iframeHtml.match(/\<iframe.+src\=(?:\"|\')(.+?)(?:\"|\')/);
|
||||
return groups[1];
|
||||
}
|
||||
|
||||
function extractVimeoInformation(vimeoResponse) {
|
||||
return {
|
||||
duration: vimeoResponse.duration,
|
||||
name: vimeoResponse.name,
|
||||
description: vimeoResponse.description,
|
||||
link: vimeoResponse.link,
|
||||
type: vimeoResponse.type,
|
||||
stats: vimeoResponse.stats,
|
||||
|
||||
download: vimeoResponse.download,
|
||||
pictures: vimeoResponse.pictures,
|
||||
embed: getIframeSource(vimeoResponse.embed.html)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function extractProviderInfo(videoId) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
client.request({
|
||||
method: 'GET',
|
||||
path: '/videos/' + videoId
|
||||
}, function (error, body, status_code, headers) {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
resolve({})
|
||||
} else {
|
||||
if (body.status !== 'available') {
|
||||
resolve({})
|
||||
} else {
|
||||
resolve(extractVimeoInformation(body));
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
function determineProviderInfo(url) {
|
||||
var info = parseVideo(url);
|
||||
return {
|
||||
provider: info.type,
|
||||
code: info.code,
|
||||
class: info.class,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
determineProviderInfo,
|
||||
extractProviderInfo
|
||||
}
|
||||
@ -203,7 +203,11 @@ const generateService = (model, extraMethods = {}, options = defaultOptions) =>
|
||||
if (extraMethods.beforeCreate) {
|
||||
values = extraMethods.beforeCreate(values, context);
|
||||
}
|
||||
return await model.scope(context.scopes).create(values);
|
||||
var result = await model.scope(context.scopes).create(values);
|
||||
if (extraMethods.afterCreate) {
|
||||
result = extraMethods.beforeCreate(result, context);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
update: async (params, values, context) => {
|
||||
@ -217,7 +221,13 @@ const generateService = (model, extraMethods = {}, options = defaultOptions) =>
|
||||
delete: async (params, context) => {
|
||||
const findOptions = parseParamsToFindOptions(params);
|
||||
const numAffectedRows = await model.scope(context.scopes).destroy(findOptions);
|
||||
return (numAffectedRows > 0);
|
||||
|
||||
var result = (numAffectedRows > 0);
|
||||
|
||||
if (extraMethods.afterDelete) {
|
||||
extraMethods.afterDelete(values, findOptions, context);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
search: async (params, context) => {
|
||||
|
||||
@ -1,47 +0,0 @@
|
||||
const config = require('../config');
|
||||
const Vimeo = require('vimeo').Vimeo;
|
||||
const client = new Vimeo(config.vimeo.CLIENT_ID, config.vimeo.CLIENT_SECRET, config.vimeo.ACCESS_TOKEN);
|
||||
|
||||
function getIframeSource(iframeHtml) {
|
||||
const groups = iframeHtml.match(/\<iframe.+src\=(?:\"|\')(.+?)(?:\"|\')/);
|
||||
return groups[1];
|
||||
}
|
||||
|
||||
function extractInformation(vimeoResponse) {
|
||||
return {
|
||||
duration: vimeoResponse.duration,
|
||||
name: vimeoResponse.name,
|
||||
description: vimeoResponse.description,
|
||||
link: vimeoResponse.link,
|
||||
type: vimeoResponse.type,
|
||||
stats: vimeoResponse.stats,
|
||||
|
||||
download: vimeoResponse.download,
|
||||
pictures: vimeoResponse.pictures,
|
||||
embed: getIframeSource(vimeoResponse.embed.html)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getVimeoVideoInfo(videoId) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
client.request({
|
||||
method: 'GET',
|
||||
path: '/videos/' + videoId
|
||||
}, function (error, body, status_code, headers) {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
resolve({})
|
||||
} else {
|
||||
if (body.status !== 'available') {
|
||||
resolve({})
|
||||
} else {
|
||||
resolve(extractInformation(body));
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
module.exports = getVimeoVideoInfo;
|
||||
@ -18,20 +18,14 @@ const extraControllers = {
|
||||
try {
|
||||
var file = req.file;
|
||||
var data = req.body;
|
||||
console.log(file);
|
||||
console.log(data);
|
||||
|
||||
// ¿Hay fichero?
|
||||
if (!file || !file.buffer) {
|
||||
return handleResultResponse('Multimediafile is missing', null, null, res, httpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
var CDNFilePath = cdnHelper.getCDNFilenameWithPath(file.originalname, 'speaker');
|
||||
console.log('CDNFilePath', CDNFilePath);
|
||||
//var CDNFilePath = cdnHelper.getCDNFilenameWithPath(file.originalname, 'speaker');
|
||||
//console.log('CDNFilePath', CDNFilePath);
|
||||
|
||||
const context = buildContext(req, config);
|
||||
console.log(context);
|
||||
|
||||
await multimediaFileService.uploadFile(file, CDNFilePath, data, context);
|
||||
//await multimediaFileService.uploadFile(file, CDNFilePath, data, context);
|
||||
|
||||
const result = await multimediaService.create(data, context);
|
||||
return handleResultResponse(result, null, null, res, httpStatus.CREATED)
|
||||
|
||||
@ -5,12 +5,12 @@ const _ = require('lodash');
|
||||
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
||||
const models = require('../../core/models');
|
||||
const cdnHelper = require('../../helpers/cdn.helper');
|
||||
const getVimeoVideoInfo = require('../../helpers/vimeo.helper');
|
||||
const { extractProviderInfo } = require('../../helpers/providers.helper');
|
||||
|
||||
const providerComposer = (multimedia) => {
|
||||
|
||||
if (multimedia.provider === 'vimeo') {
|
||||
multimedia.providerInfo = getVimeoVideoInfo(multimedia.code)
|
||||
multimedia.providerInfo = extractProviderInfo(multimedia.code)
|
||||
} else if (multimedia.provider === 'cdn') {
|
||||
multimedia.url = cdnHelper.getCDNMediaUrl(multimedia.url);
|
||||
}
|
||||
|
||||
@ -49,9 +49,9 @@ const extraControllers = {
|
||||
try {
|
||||
if (req.file) {
|
||||
data.url = req.file.path.replace(configuration.uploads.path, '');
|
||||
data.provider = 'cdn';
|
||||
data.class = 'picture';
|
||||
}
|
||||
var data = req.body;
|
||||
console.log(data);
|
||||
|
||||
const context = buildContext(req, config);
|
||||
const result = await multimediaFileService.create(data, context);
|
||||
|
||||
@ -58,5 +58,13 @@ routes.put('/admin/multimediafiles/:id',
|
||||
multimediaFileController.update()
|
||||
);
|
||||
|
||||
// Borrado
|
||||
routes.delete('/admin/multimediafiles/:id',
|
||||
isAdministratorUser,
|
||||
SchemaValidator(multimediaFilesInputType, true),
|
||||
cdnUpload().single('file'),
|
||||
multimediaFileController.delete()
|
||||
);
|
||||
|
||||
|
||||
module.exports = routes;
|
||||
@ -13,13 +13,13 @@ const sharp = require('sharp');
|
||||
const config = require('../../config');
|
||||
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
||||
const models = require('../../core/models');
|
||||
const getVimeoVideoInfo = require('../../helpers/vimeo.helper');
|
||||
const { determineProviderInfo, extractProvciderInfo } = require('../../helpers/providers.helper');
|
||||
|
||||
const providerComposer = (multimedia) => {
|
||||
return getVimeoVideoInfo(multimedia.code)
|
||||
}
|
||||
|
||||
const _generateThumbFileName = function (fileName, prefix) {
|
||||
/*const _generateThumbFileName = function (fileName, prefix) {
|
||||
prefix = prefix || config.uploads.thumb_prefix;
|
||||
return Path.join(
|
||||
Path.dirname(fileName),
|
||||
@ -72,7 +72,7 @@ const _uploadFile = function (file, filename, options) {
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}*/
|
||||
|
||||
const extraMethods = {
|
||||
removeFile: function (filename) {
|
||||
@ -102,8 +102,16 @@ const extraMethods = {
|
||||
})
|
||||
},
|
||||
|
||||
create: async function(values, context) {
|
||||
beforeCreate: function(values, context) {
|
||||
if (values.provider != 'cdn') {
|
||||
var info = determineProviderInfo(values.url);
|
||||
|
||||
values.provider = info.provider;
|
||||
values.code = info.code;
|
||||
values.class = info.class;
|
||||
}
|
||||
|
||||
return values;
|
||||
},
|
||||
|
||||
afterFetchOne: async (result, params, context) => {
|
||||
@ -122,4 +130,4 @@ const extraMethods = {
|
||||
|
||||
}
|
||||
|
||||
module.exports = generateService(models.MultimediaFile, extraMethods);
|
||||
module.exports = generateService(models.MultimediaFile, extraMethods);
|
||||
Loading…
Reference in New Issue
Block a user