This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from rezof/master
gatsby-image support for media/image fields
- Loading branch information
Showing
4 changed files
with
91 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
"trailingComma": "es5", | ||
"semi": false, | ||
"tabWidth": 2, | ||
"single-quote": true | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const { createRemoteFileNode } = require(`gatsby-source-filesystem`) | ||
|
||
// Downloads media from image type fields | ||
exports.downloadMediaFiles = async ({ | ||
entities, | ||
apiURL, | ||
store, | ||
cache, | ||
createNode, | ||
touchNode, | ||
jwtToken: auth, | ||
}) => | ||
Promise.all( | ||
entities.map(async entity => { | ||
for (let item of entity) { | ||
// loop item over fields | ||
for (const key of Object.keys(item)) { | ||
const field = item[key] | ||
// image fields have a mime property among other | ||
// maybe should find a better test | ||
if (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) | ||
|
||
// If we have cached media data and it wasn't modified, reuse | ||
// previously created file node to not try to redownload | ||
if ( | ||
cacheMediaData && | ||
field.updatedAt === cacheMediaData.updatedAt | ||
) { | ||
fileNodeID = cacheMediaData.fileNodeID | ||
touchNode(cacheMediaData.fileNodeID) | ||
} | ||
|
||
// If we don't have cached data, download the file | ||
if (!fileNodeID) { | ||
try { | ||
// full media url | ||
const source_url = 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 (fileNode) { | ||
fileNodeID = fileNode.id | ||
|
||
await cache.set(mediaDataCacheKey, { | ||
fileNodeID, | ||
modified: field.updatedAt, | ||
}) | ||
} | ||
} catch (e) { | ||
// Ignore | ||
} | ||
} | ||
if (fileNodeID) { | ||
item[`${key}___NODE`] = fileNodeID | ||
} | ||
} | ||
} | ||
} | ||
return entity | ||
}) | ||
) |