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

Commit

Permalink
Restore changes to parse arrays of images
Browse files Browse the repository at this point in the history
  • Loading branch information
denisgoryaynov committed Apr 2, 2020
1 parent ad078f3 commit f01eca1
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
139 changes: 85 additions & 54 deletions src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand All @@ -79,6 +108,7 @@ exports.downloadMediaFiles = async ({
store,
cache,
createNode,
createNodeId,
touchNode,
jwtToken: auth,
}) =>
Expand All @@ -91,6 +121,7 @@ exports.downloadMediaFiles = async ({
store,
cache,
createNode,
createNodeId,
touchNode,
auth,
item
Expand Down

0 comments on commit f01eca1

Please sign in to comment.