-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelpers.js
60 lines (53 loc) · 1.58 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
// Downloads media files and removes "sizes" data as useless in Gatsby context.
exports.downloadMediaFiles = async ({
entities,
store,
cache,
createNode,
createNodeId,
touchNode,
_auth,
}) =>
Promise.all(
entities.map(async e => {
let fileNodeID
if (e.__type === `wordpress__wp_media`) {
const mediaDataCacheKey = `wordpress-media-${e.wordpress_id}`
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 && e.modified === cacheMediaData.modified) {
fileNodeID = cacheMediaData.fileNodeID
touchNode({ nodeId: cacheMediaData.fileNodeID })
}
// If we don't have cached data, download the file
if (!fileNodeID) {
try {
const fileNode = await createRemoteFileNode({
url: e.source_url,
store,
cache,
createNode,
createNodeId,
auth: _auth,
})
if (fileNode) {
fileNodeID = fileNode.id
await cache.set(mediaDataCacheKey, {
fileNodeID,
modified: e.modified,
})
}
} catch (e) {
// Ignore
}
}
}
if (fileNodeID) {
e.localFile___NODE = fileNodeID
delete e.media_details.sizes
}
return e
})
)