-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadDestinations.js
69 lines (55 loc) · 1.71 KB
/
uploadDestinations.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
import admin from 'firebase-admin'
import dotenv from 'dotenv'
import path from 'path'
import fs from 'fs'
import { v4 as uuidv4 } from 'uuid'
import csv from 'csv-parser'
import { Destination } from './models/Destination'
dotenv.config()
admin.initializeApp({
credential: admin.credential.applicationDefault(),
storageBucket: 'gs://apec-2021-nz.appspot.com',
})
const saveImage = async (imagePath) => {
const stream = fs.createReadStream(imagePath)
const file = admin
.storage()
.bucket()
.file(uuidv4() + path.extname(imagePath))
// Upload the file to gcs
await new Promise((resolve, reject) => {
const writeStream = file.createWriteStream()
writeStream.on('finish', resolve)
writeStream.on('error', reject)
stream.pipe(writeStream)
})
return file.publicUrl()
}
let destinations = []
const main = async () => {
await new Promise(function (resolve, reject) {
fs.createReadStream('destinations.csv')
.pipe(csv())
.on('data', async (object) => {
destinations.push(object)
})
.on('end', () => {
resolve()
})
})
let count = 1
for (const { destination, city } of destinations) {
const imagepath = `./image/destination/${destination
.split(' ')
.join('_')}.png`
const imageUrl = await saveImage(imagepath)
const destinationModel = Destination.init()
destinationModel.id = `${count}`
destinationModel.name = destination
destinationModel.image = imageUrl
destinationModel.city = `/City/${city}`
await destinationModel.save()
count++
}
}
main()