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 pathgenerateSMImages.js
58 lines (50 loc) · 1.65 KB
/
generateSMImages.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
//
// This script imports testimonials from a gsheet
// and looks for photos locally at ./photos/
//
// usage
// cf_server node script/generateSMImages.js
//
require('dotenv').config()
const PgDb = require('../lib/pgdb')
const uploadExoscale = require('../lib/uploadExoscale')
const keyCDN = require('../lib/keyCDN')
const renderUrl = require('../lib/renderUrl')
const slugify = require('../lib/slugify')
const FOLDER = 'testimonials'
const { ASSETS_BASE_URL, FRONTEND_BASE_URL, S3BUCKET } = process.env
const WITH_USER_NAME = false
PgDb.connect().then(async (pgdb) => {
let counter = 0
const testimonials = await pgdb.public.testimonials.find({})
for (let testimonial of testimonials) {
let smImagePath = `/${FOLDER}/sm/${testimonial.id}_sm.png`
if (WITH_USER_NAME) {
const user = await pgdb.public.users.findOne({id: testimonial.userId})
smImagePath = `/${FOLDER}/export/${slugify(user.firstName + '_' + user.lastName)}.png`
}
const url = ASSETS_BASE_URL + smImagePath
await renderUrl(`${FRONTEND_BASE_URL}/community?share=${testimonial.id}`, 1200, 628)
.then(async (data) => {
await uploadExoscale({
stream: data,
path: smImagePath,
mimeType: 'image/png',
bucket: S3BUCKET
}).then(async () => {
await keyCDN.purgeUrls([smImagePath])
await pgdb.public.testimonials.updateAndGetOne({id: testimonial.id}, {
smImage: url
})
})
})
console.log(`magic done for: ${url}`)
counter += 1
}
console.log(`${counter} images generated`)
}).then(() => {
process.exit()
}).catch(e => {
console.error(e)
process.exit(1)
})