From 49e26a0d3612a98db707f84cafd3777d76c7ebd4 Mon Sep 17 00:00:00 2001 From: Joel Rainwater Date: Tue, 12 Feb 2019 12:12:04 +0100 Subject: [PATCH 1/2] Mapped video_content properties in media_gallery --- src/adapters/magento/product.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/adapters/magento/product.js b/src/adapters/magento/product.js index bc10a44..dcd5eff 100644 --- a/src/adapters/magento/product.js +++ b/src/adapters/magento/product.js @@ -264,12 +264,24 @@ class ProductAdapter extends AbstractMagentoAdapter { let media_gallery = [] for (let mediaItem of result){ if (!mediaItem.disabled) { - media_gallery.push({ + let newMediaObj = { image: mediaItem.file, pos: mediaItem.position, typ: mediaItem.media_type, lab: mediaItem.label - }) + }; + // Separation of ifs for future extendability + if (mediaItem.extension_attributes) { + if (mediaItem.extension_attributes.video_content) { + newMediaObj.vid = { + url: mediaItem.extension_attributes.video_content.video_url, + title: mediaItem.extension_attributes.video_content.video_title, + desc: mediaItem.extension_attributes.video_content.video_description, + meta: mediaItem.extension_attributes.video_content.video_metadata + } + } + } + media_gallery.push(newMediaObj) } } item.media_gallery = media_gallery From 88aa453525d96faa9e28a9ae9a4dbf9c206fb8c4 Mon Sep 17 00:00:00 2001 From: Joel Rainwater Date: Thu, 14 Feb 2019 17:28:04 +0100 Subject: [PATCH 2/2] Parse video url to grab type and id during import --- src/adapters/magento/product.js | 58 ++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/src/adapters/magento/product.js b/src/adapters/magento/product.js index dcd5eff..ee66ddf 100644 --- a/src/adapters/magento/product.js +++ b/src/adapters/magento/product.js @@ -264,24 +264,13 @@ class ProductAdapter extends AbstractMagentoAdapter { let media_gallery = [] for (let mediaItem of result){ if (!mediaItem.disabled) { - let newMediaObj = { + media_gallery.push({ image: mediaItem.file, pos: mediaItem.position, typ: mediaItem.media_type, - lab: mediaItem.label - }; - // Separation of ifs for future extendability - if (mediaItem.extension_attributes) { - if (mediaItem.extension_attributes.video_content) { - newMediaObj.vid = { - url: mediaItem.extension_attributes.video_content.video_url, - title: mediaItem.extension_attributes.video_content.video_title, - desc: mediaItem.extension_attributes.video_content.video_description, - meta: mediaItem.extension_attributes.video_content.video_metadata - } - } - } - media_gallery.push(newMediaObj) + lab: mediaItem.label, + vid: this.computeVideoData(mediaItem) + }) } } item.media_gallery = media_gallery @@ -561,6 +550,45 @@ class ProductAdapter extends AbstractMagentoAdapter { }); } + /** + * Process video data to provide the proper + * provider and attributes. + * Currently supports YouTube and Vimeo + * + * @param {Object} mediaItem + */ + computeVideoData(mediaItem) { + let videoData = null; + + if (mediaItem.extension_attributes && mediaItem.extension_attributes.video_content) { + let videoId = null, + type = null, + youtubeRegex = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/, + vimeoRegex = new RegExp(['https?:\\/\\/(?:www\\.|player\\.)?vimeo.com\\/(?:channels\\/(?:\\w+\\/)', + '?|groups\\/([^\\/]*)\\/videos\\/|album\\/(\\d+)\\/video\\/|video\\/|)(\\d+)(?:$|\\/|\\?)' + ].join('')); + + if (mediaItem.extension_attributes.video_content.video_url.match(youtubeRegex)) { + videoId = RegExp.$1 + type = 'youtube' + } else if (mediaItem.extension_attributes.video_content.video_url.match(vimeoRegex)) { + videoId = RegExp.$3 + type = 'vimeo' + } + + videoData = { + url: mediaItem.extension_attributes.video_content.video_url, + title: mediaItem.extension_attributes.video_content.video_title, + desc: mediaItem.extension_attributes.video_content.video_description, + meta: mediaItem.extension_attributes.video_content.video_metadata, + id: videoId, + type: type + } + } + + return videoData; + } + /** * We're transorming the data structure of item to be compliant with Smile.fr Elastic Search Suite * @param {object} item document to be updated in elastic search