From f01eca114d40e6688e6282f3e188620d7312de67 Mon Sep 17 00:00:00 2001 From: denisgoryaynov Date: Thu, 2 Apr 2020 22:38:41 +0200 Subject: [PATCH] Restore changes to parse arrays of images --- package.json | 2 +- src/normalize.js | 139 +++++++++++++++++++++++++++++------------------ 2 files changed, 86 insertions(+), 55 deletions(-) diff --git a/package.json b/package.json index aa74830..8c38732 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gatsby-source-strapi", - "version": "0.0.11", + "version": "0.0.12", "description": "Gatsby source plugin for building websites using Strapi as a data source", "author": { "email": "hi@strapi.io", diff --git a/src/normalize.js b/src/normalize.js index 54f1acd..d9c6f17 100644 --- a/src/normalize.js +++ b/src/normalize.js @@ -5,68 +5,97 @@ const extractFields = async ( store, cache, createNode, + createNodeId, touchNode, auth, - item + item, + key = 'localFile' ) => { - for (const key of Object.keys(item)) { - const field = item[key] - if (Array.isArray(field)) { - // add recursion to fetch nested strapi references - await Promise.all( - field.map(async f => - extractFields(apiURL, store, cache, createNode, touchNode, auth, f) - ) - ) - } else { - // image fields have a mime property among other - // maybe should find a better test - if (field !== null && field.hasOwnProperty('mime')) { - let fileNodeID - // using field on the cache key for multiple image field - const mediaDataCacheKey = `strapi-media-${item.id}-${key}` - const cacheMediaData = await cache.get(mediaDataCacheKey) + // image fields have a mime property among other + // maybe should find a better test + if (item && item.hasOwnProperty('mime')) { + let fileNodeID + // using field on the cache key for multiple image field + const mediaDataCacheKey = `strapi-media-${item.id}-${key}` + const cacheMediaData = await cache.get(mediaDataCacheKey) - // If we have cached media data and it wasn't modified, reuse - // previously created file node to not try to redownload - if (cacheMediaData && field.updated_at === cacheMediaData.updated_at) { - fileNodeID = cacheMediaData.fileNodeID - touchNode({ nodeId: cacheMediaData.fileNodeID }) - } + // If we have cached media data and it wasn't modified, reuse + // previously created file node to not try to redownload + if (cacheMediaData && item.updatedAt === cacheMediaData.updatedAt) { + fileNodeID = cacheMediaData.fileNodeID + touchNode({ nodeId: cacheMediaData.fileNodeID }) + } - // If we don't have cached data, download the file - if (!fileNodeID) { - try { - // full media url - const source_url = `${field.url.startsWith('http') ? '' : apiURL}${ - field.url - }` - const fileNode = await createRemoteFileNode({ - url: source_url, - store, - cache, - createNode, - auth, - }) + // If we don't have cached data, download the file + if (!fileNodeID) { + try { + // full media url + const source_url = `${item.url.startsWith('http') ? '' : apiURL}${ + item.url + }` + const fileNode = await createRemoteFileNode({ + url: source_url, + store, + cache, + createNode, + createNodeId, + auth, + }) - // If we don't have cached data, download the file - if (fileNode) { - fileNodeID = fileNode.id + // If we don't have cached data, download the file + if (fileNode) { + fileNodeID = fileNode.id - await cache.set(mediaDataCacheKey, { - fileNodeID, - updated_at: field.updated_at, - }) - } - } catch (e) { - // Ignore - } - } - if (fileNodeID) { - item[`${key}___NODE`] = fileNodeID + await cache.set(mediaDataCacheKey, { + fileNodeID, + updatedAt: item.updatedAt, + }) } - } else if (field !== null && typeof field === 'object') { - extractFields(apiURL, store, cache, createNode, touchNode, auth, field); + } catch (e) { + // Ignore + } + } + + if (fileNodeID) { + if (key !== 'localFile') { + return fileNodeID + } + + item.localFile___NODE = fileNodeID + } + } else if (Array.isArray(item)) { + await Promise.all( + item.map(async f => + extractFields( + apiURL, + store, + cache, + createNode, + createNodeId, + touchNode, + auth, + f + ) + ) + ) + } else if (item && typeof item === 'object') { + for (const key of Object.keys(item)) { + const field = item[key] + + const fileNodeID = await extractFields( + apiURL, + store, + cache, + createNode, + createNodeId, + touchNode, + auth, + field, + key + ) + + if (fileNodeID) { + item[`${key}___NODE`] = fileNodeID } } } @@ -79,6 +108,7 @@ exports.downloadMediaFiles = async ({ store, cache, createNode, + createNodeId, touchNode, jwtToken: auth, }) => @@ -91,6 +121,7 @@ exports.downloadMediaFiles = async ({ store, cache, createNode, + createNodeId, touchNode, auth, item