-
Notifications
You must be signed in to change notification settings - Fork 23
/
set-fields-on-graphql-node-type.ts
85 lines (77 loc) · 2.13 KB
/
set-fields-on-graphql-node-type.ts
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
79
80
81
82
83
84
85
import { DateTime } from 'luxon'
import { ExifParserFactory } from 'ts-exif-parser'
import {
GraphQLFloat,
GraphQLInt,
GraphQLObjectType,
GraphQLString,
} from 'gatsby/graphql'
import _ from 'lodash'
import fracty from 'fracty'
import fs from 'fs'
import ExifData from './types/ExifData'
import S3ImageAssetNode from './types/S3ImageAssetNode'
const resolveExifData = _.memoize((
image: S3ImageAssetNode // eslint-disable
): ExifData | undefined => {
const file = fs.readFileSync(image.absolutePath)
const tags = ExifParserFactory.create(file).parse().tags
const timestamp: number | undefined = _.get(tags, 'DateTimeOriginal')
if (!timestamp) {
return
}
const DateCreatedISO = DateTime.fromMillis(timestamp * 1000).toISODate()
const ExposureTime = _.get(tags, 'ExposureTime')
const ShutterSpeedFraction = fracty(ExposureTime)
return {
DateCreatedISO,
ShutterSpeedFraction,
..._.pick(tags, [
'DateTimeOriginal',
'Exposure',
'ExposureTime',
'FNumber',
'FocalLength',
'ISO',
'LensModel',
'Model',
'ShutterSpeedValue',
]),
}
})
interface ExtendNodeTypeOptions {
type: {
name: string
}
}
export default ({ type }: ExtendNodeTypeOptions) => {
if (type.name !== 'S3ImageAsset') {
return Promise.resolve()
}
return Promise.resolve({
ETag: { type: GraphQLString },
EXIF: {
resolve: (image: S3ImageAssetNode) => ({
...type,
...resolveExifData(image),
}),
type: new GraphQLObjectType({
fields: {
DateCreatedISO: { type: GraphQLString },
DateTimeOriginal: { type: GraphQLInt },
Exposure: { type: GraphQLString },
ExposureTime: { type: GraphQLFloat },
FNumber: { type: GraphQLFloat },
FocalLength: { type: GraphQLFloat },
ISO: { type: GraphQLInt },
LensModel: { type: GraphQLString },
Model: { type: GraphQLString },
ShutterSpeedFraction: { type: GraphQLString },
ShutterSpeedValue: { type: GraphQLFloat },
},
name: 'ExifData',
}),
},
Key: { type: GraphQLString },
})
}