Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #99 from zarathustra323/video-embed-src
Browse files Browse the repository at this point in the history
Add `ContentVideo.embedSrc` field
  • Loading branch information
zarathustra323 authored Jun 9, 2021
2 parents 97ec1a2 + 484d6a0 commit b9747fc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type ContentVideo implements Content & Authorable & Media @applyInterfaceFields
embedCode: String @projection
sourceId: String @projection
sourceThumbnail: String @projection
# graphql only fields
embedSrc: String @projection(localField: "embedCode")
}
type ContentVideoConnection {
Expand Down
34 changes: 34 additions & 0 deletions services/graphql-server/src/graphql/resolvers/platform/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { createSrcFor, createCaptionFor } = require('@parameter1/base-cms-image')
const { getAsArray } = require('@parameter1/base-cms-object-path');
const moment = require('moment');
const momentTZ = require('moment-timezone');
const cheerio = require('cheerio');

const defaults = require('../../defaults');
const validateRest = require('../../utils/validate-rest');
Expand Down Expand Up @@ -596,6 +597,39 @@ module.exports = {
},
},

/**
*
*/
ContentVideo: {
embedSrc: ({ embedCode }) => {
if (!embedCode) return null;
const $ = cheerio.load(`${embedCode}`);

const loadFromIframe = () => {
const $iframe = $('iframe:first-of-type');
if (!$iframe) return null;
return $iframe.attr('src') || null;
};

const loadFromBrightcove = () => {
const $video = $('video:first-of-type');
if (!$video) return null;
const data = $video.data();
if (!data) return null;
if (!['videoId', 'account', 'player', 'embed'].every(key => data[key])) return null;
return `https://players.brightcove.net/${data.account}/${data.player}_${data.embed}/index.html?videoId=${data.videoId}`;
};

const iframeSrc = loadFromIframe();
if (iframeSrc) return iframeSrc;

const brightcoveSrc = loadFromBrightcove();
if (brightcoveSrc) return brightcoveSrc;

return null;
},
},

ContentCompanyYoutube: {
videos: ({ videos = [] } = {}) => videos.filter(v => v),
url: (youtube) => {
Expand Down

0 comments on commit b9747fc

Please sign in to comment.