Newer
Older
const { rule } = require('graphql-shield')
const path = require('path')
const sharp = require('sharp')
const fs = require('fs-extra')
// const config = require('config')
// const get = require('lodash/get')
// const { ServiceCredential } = require('./models')
// const services = config.get('services')
const isAuthenticated = rule()(async (parent, args, ctx, info) => {
return !!ctx.user
})
const isAdmin = rule()(
async (parent, args, { user: userId, connectors: { User } }, info) => {
if (!userId) {
return false
}
const user = await User.model.findById(userId)
return user.admin
},
)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const convertFileStreamIntoBuffer = async fileStream => {
return new Promise((resolve, reject) => {
// Store file data chunks
const chunks = []
// Throw if error occurred
fileStream.on('error', err => {
reject(err)
})
// File is done being read
fileStream.on('end', () => {
// create the final data Buffer from data chunks;
resolve(Buffer.concat(chunks))
})
// Data is flushed from fileStream in chunks,
// this callback will be executed for each chunk
fileStream.on('data', chunk => {
chunks.push(chunk) // push data chunk to array
})
})
}
const getFileExtension = (filename, includingDot = false) => {
const { ext } = path.parse(filename)
if (!includingDot) {
return ext.split('.')[1]
}
return ext
}
const getImageFileMetadata = async fileBuffer => {
try {
const originalImage = sharp(fileBuffer, { limitInputPixels: false })
const imageMetadata = await originalImage.metadata()
return imageMetadata
} catch (e) {
throw new Error(e)
}
}
const writeFileFromStream = async (inputStream, filePath) => {
try {
return new Promise((resolve, reject) => {
const outputStream = fs.createWriteStream(filePath)
inputStream.pipe(outputStream)
outputStream.on('error', error => {
reject(error.message)
})
outputStream.on('finish', () => {
resolve()
})
})
} catch (e) {
throw new Error(e)
}
}
// const serviceHandshake = async (which, renew = false) => {
// if (!services) {
// throw new Error('services are undefined')
// }
// const service = get(services, `${which}`)
// if (!service) {
// throw new Error(`service ${which} configuration is undefined `)
// }
// const foundServiceCredential = await ServiceCredential.query().findOne({
// name: which,
// })
// const { clientId, clientSecret, port, protocol, host } = service
// const buff = Buffer.from(`${clientId}:${clientSecret}`, 'utf8')
// const base64data = buff.toString('base64')
// const serviceURL = `${protocol}://${host}${port ? `:${port}` : ''}`
// const serviceHealthCheck = await axios({
// method: 'get',
// url: `${serviceURL}/healthcheck`,
// })
// const { data: healthCheckData } = serviceHealthCheck
// const { message } = healthCheckData
// if (message !== 'Coolio') {
// throw new Error(`service ${which} is down`)
// }
// return new Promise((resolve, reject) => {
// axios({
// method: 'post',
// url: `${serviceURL}/api/auth`,
// headers: { authorization: `Basic ${base64data}` },
// })
// .then(async ({ data }) => {
// const { accessToken } = data
// if (!renew && !foundServiceCredential) {
// await ServiceCredential.query().insert({
// name: which,
// accessToken,
// })
// resolve()
// }
// await ServiceCredential.query().patchAndFetchById(
// foundServiceCredential.id,
// {
// accessToken,
// },
// )
// resolve()
// })
// .catch(async err => {
// const { response } = err
// if (foundServiceCredential) {
// await ServiceCredential.query().patchAndFetchById(
// foundServiceCredential.id,
// {
// accessToken: null,
// },
// )
// }
// if (!response) {
// return reject(new Error(`Request failed with message: ${err.code}`))
// }
// const { status, data } = response
// const { msg } = data
// return reject(
// new Error(`Request failed with status ${status} and message: ${msg}`),
// )
// })
// })
// }
module.exports = {
isAuthenticated,
isAdmin,
convertFileStreamIntoBuffer,
getFileExtension,
getImageFileMetadata,
writeFileFromStream,
// serviceHandshake,