-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgatsby-node.js
78 lines (68 loc) · 2.06 KB
/
gatsby-node.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import crypto from 'crypto';
import { listObjects } from './list-objects';
import { schema } from './plugin-options';
import { downloadImageFile } from './download-image-file';
const createContentDigest = obj =>
crypto
.createHash('md5')
.update(JSON.stringify(obj))
.digest('hex');
const isImage = object => /\.(jpe?g|png|webp|tiff?)$/i.test(object);
const getBucketName = () => {};
export async function sourceNodes(
{ boundActionCreators, store, cache },
pluginOptions
) {
const { createNode, touchNode } = boundActionCreators;
const { aws: awsConfig, buckets: bucketsConfig } = await schema.validate(
pluginOptions
);
const buckets = await listObjects(bucketsConfig, awsConfig);
const region = awsConfig.region;
await Promise.all(
buckets.map(({ Contents, ...rest }, index) => {
return Promise.all(
Contents.map(async content => {
const { Key } = content;
const node = {
...rest,
...content,
Url: `https://s3.${region ? `${region}.` : ''}amazonaws.com/${rest.Name}/${Key}`,
id: `s3-${Key}`,
children: [],
parent: '__SOURCE__',
internal: {
type: 'S3Object',
contentDigest: createContentDigest(content),
content: JSON.stringify(content),
},
};
createNode(node);
if (isImage(Key)) {
const Extension = Key.split('.').pop();
const imageNode = await downloadImageFile(
{
...node,
Extension,
id: `s3-image-${Key}`,
internal: {
type: 'S3Image',
contentDigest: createContentDigest(content),
content: JSON.stringify(content),
},
},
{
store,
cache,
createNode,
touchNode,
}
);
createNode(imageNode);
}
})
);
})
);
return buckets;
}