-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
87 lines (74 loc) · 2.54 KB
/
database.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
'use strict'
let https = require('https')
let fs = require('fs');
let azure = require('azure-storage')
let crypto = require('crypto');
let fileService = azure.createFileService(process.env.AZURE_FILE_STORAGE_NAME, process.env.AZURE_FILE_STORAGE_KEY)
let db = null
module.exports = {
init: (firebase) => db = firebase.database(),
addRequest: (req) => {
let name = (new Date()).toISOString().replace(".", ":").replace(/:/g, "-")
persistFile(req.imageUri, name, url => {
req.imageUri = url
console.log("uploaded url", url)
db.ref("requests/" + name).set(req).then(
ok => console.log("firebase set succesfull"),
err => console.error("firebase set failed", err)
)
})
},
getRequests: (limit) => {
return db.ref("requests").limitToLast(limit || 8).once("value")
},
getUsers: () => {
return new Promise((resolve, reject) => {
console.log(1)
db.ref("requests").once("value").then(data => {
let users = []
let requests = Object.values(data.val())
requests = requests.map(x => x.user)
requests = requests.map(x => Number.isInteger(x) ? { id: x } : x) //convert users that have not given any name
requests = requests.map(x => {
if (users.find(y => y.id == x.id)) return
x.calls = requests.reduce((acc, item) => item.id == x.id ? acc + 1 : acc, 0) //count all requests of this user
users.push(x)
})
resolve(users)
})
})
},
}
function persistFile(uri, name, callback) {
downloadFile(uri, (filePath) => {
uploadToPermanentStorage(filePath, name, (url) => callback(url))
})
}
function uploadToPermanentStorage(filePath, name, callback) {
let shareName = "uploads"
let dir = "images"
name += ".jpg"
fileService.createFileFromLocalFile(shareName, dir, name, filePath, (err, res, resp) => {
let sasToken = fileService.generateSharedAccessSignature(shareName, dir, name, {
AccessPolicy: {
Permissions: azure.FileUtilities.SharedAccessPermissions.READ,
Start: "2015-12-24T18:21Z",
Expiry: "2099-12-24T18:21Z"
}
})
let url = fileService.getUrl(shareName, dir, name, sasToken, true)
callback(url)
})
}
function downloadFile(uri, callback) {
let localFilePath = "/tmp/" + crypto.randomBytes(4).readUInt32LE(0)
let localFile = fs.createWriteStream(localFilePath)
https.get(uri, response => {
response.pipe(localFile)
localFile.on('finish', () => {
localFile.close(() => {
callback(localFilePath)
})
})
})
}