This repository was archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathimportTestimonials.js
152 lines (136 loc) · 4.53 KB
/
importTestimonials.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
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
//
// This script imports testimonials from a gsheet
// and looks for photos locally at ./local/photos/
//
// usage
// cf_server node script/importTestimonials.js
//
require('dotenv').config()
const PgDb = require('../lib/pgdb')
const gsheets = require('gsheets')
const uploadExoscale = require('../lib/uploadExoscale')
const keyCDN = require('../lib/keyCDN')
const fs = require('fs')
const path = require('path')
const uuid = require('uuid/v4')
const convertImage = require('../lib/convertImage')
const GKEY = '1IoNowWMs6dK3OAK_uyWaZMQKrWU0H6LCTYedLcbHPXk'
const FOLDER = 'testimonials'
const { ASSETS_BASE_URL } = process.env
const {IMAGE_SIZE_SMALL, IMAGE_SIZE_SHARE} = convertImage
function randomString (len) {
var text = ''
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for (var i = 0; i < len; i++) { text += possible.charAt(Math.floor(Math.random() * possible.length)) }
return text
}
PgDb.connect().then(async (pgdb) => {
const {S3BUCKET} = process.env
let counter = 0
const sheet = await gsheets.getWorksheet(GKEY, 'live')
for (let person of sheet.data) {
if (person.Filename && fs.existsSync(path.join(__dirname, '/local/photos/', person.Filename))) {
const names = person.Name.split(' ')
const filename = person.Filename
const firstName = names[0]
const lastName = names.slice(1).join(' ')
const email = person['E-Mailadresse']
const quote = person.Statement
const role = person.Bezeichnung
let video
if (person.hls && person.mp4) {
video = {
hls: person.hls,
mp4: person.mp4,
subtitles: person.subtitles,
youtube: person.youtube
}
}
console.log(firstName + ' ' + lastName)
let user = await pgdb.public.users.findOne({email})
let testimonial
if (user) {
testimonial = await pgdb.public.testimonials.findOne({userId: user.id})
}
const id = testimonial ? testimonial.id : uuid()
const pathOriginal = `/${FOLDER}/${id}_original.jpeg`
const pathSmall = `/${FOLDER}/${id}_${IMAGE_SIZE_SMALL}x${IMAGE_SIZE_SMALL}.jpeg`
const pathShare = `/${FOLDER}/${id}_${IMAGE_SIZE_SHARE}x${IMAGE_SIZE_SHARE}.jpeg`
const image = fs.readFileSync(path.join(__dirname, '/local/photos/', filename), 'binary')
const inputBuffer = Buffer.from(image, 'binary')
await Promise.all([
convertImage.toJPEG(inputBuffer)
.then((data) => {
return uploadExoscale({
stream: data,
path: pathOriginal,
mimeType: 'image/jpeg',
bucket: S3BUCKET
})
}),
convertImage.toSmallBW(inputBuffer)
.then((data) => {
return uploadExoscale({
stream: data,
path: pathSmall,
mimeType: 'image/jpeg',
bucket: S3BUCKET
})
}),
convertImage.toShare(inputBuffer)
.then((data) => {
return uploadExoscale({
stream: data,
path: pathShare,
mimeType: 'image/jpeg',
bucket: S3BUCKET
})
})
])
if (!user) {
user = await pgdb.public.users.insertAndGet({
firstName,
lastName,
email: email || `${randomString(10)}@anonymous.project-r.construction`,
verified: true
})
}
// load existing memberships
const firstMembership = await pgdb.public.memberships.findFirst({
userId: user.id
}, {orderBy: ['sequenceNumber asc']})
let sequenceNumber
if (firstMembership) { sequenceNumber = firstMembership.sequenceNumber }
if (!testimonial) {
await pgdb.public.testimonials.insert({
id,
userId: user.id,
role,
quote,
image: ASSETS_BASE_URL + pathSmall,
video,
sequenceNumber
}, {skipUndefined: true})
} else {
keyCDN.purgeUrls([pathOriginal, pathSmall, pathShare])
await pgdb.public.testimonials.updateAndGetOne({id: testimonial.id}, {
role,
quote,
image: ASSETS_BASE_URL + pathSmall,
video,
sequenceNumber
}, {skipUndefined: true})
}
counter += 1
console.log('finished: ' + firstName + ' ' + lastName)
} else {
console.log('photo not found: ' + person.Filename)
}
}
console.log(`${counter} people imported`)
}).then(() => {
process.exit()
}).catch(e => {
console.error(e)
process.exit(1)
})