-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (79 loc) · 3.26 KB
/
index.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
var {
Storage
} = require('@google-cloud/storage');
var fs = require('fs-extra');
var storage = new Storage();
async function checkUploadSpeed(bucketName, options = {delete: false}, cb) {
let startTime = new Date().getTime();
let bucketUploadOptions = options.destinationFileName ? {
destination: options.destinationFileName,
gzip: false,
metadata: {
cacheControl: 'public, max-age=31536000'
}
} : {
gzip: false,
metadata: {
cacheControl: 'public, max-age=31536000'
}
}
await storage.bucket(bucketName).upload(options.fileName, bucketUploadOptions).then(async () => {
let endTime = new Date().getTime();
let duration = (endTime - startTime) / 1000;
let bitsLoaded = 5000000 * 8;
let uploadSpeedBps = (bitsLoaded / duration).toFixed(2);
let uploadSpeedKbps = (uploadSpeedBps * 8 * 2 / 1000).toFixed(2);
let uploadSpeedMbps = (uploadSpeedKbps * 8 * 2 / 100000).toFixed(2);
let uploadSpeedData = {
bps: uploadSpeedBps,
kbps: uploadSpeedKbps,
mbps: uploadSpeedMbps
};
if (options && options.delete) {
await storage.bucket(bucketName).file(options.imageName).delete();
}
cb(uploadSpeedData);
});
}
async function checkDownloadSpeed(bucketName, options, cb) {
let startTime = new Date().getTime();
let endTime = new Date().getTime();
let bucket = storage.bucket(bucketName);
let remoteFile = bucket.file(options.fileName);
await remoteFile.createReadStream()
.once('response', () => {
endTime = new Date().getTime();
})
.on('response', async (response) => {
let byteLengthReceived = response.headers["content-length"];
let extrabyteLengthReceived = Number(byteLengthReceived.slice(1, byteLengthReceived.length));
byteLengthReceived = Number(byteLengthReceived);
let duration = (endTime - startTime) / 1000;
let lengthToCompare = 5000000 + extrabyteLengthReceived;
if (byteLengthReceived < lengthToCompare) {
let difference = lengthToCompare - byteLengthReceived;
byteLengthReceived = byteLengthReceived + difference;
} else {
let difference = byteLengthReceived - lengthToCompare;
byteLengthReceived = difference < byteLengthReceived ? byteLengthReceived - difference : difference - byteLengthReceived;
}
let bitsLoaded = byteLengthReceived * 8;
let downloadSpeedBps = (bitsLoaded / duration);
let downloadSpeedKbps = (downloadSpeedBps * 8 * 2 / 1000).toFixed(2);
let downloadSpeedMbps = (downloadSpeedKbps * 8 * 2 / 100000).toFixed(2);
let downloadSpeedData = {
bps: downloadSpeedBps,
kbps: downloadSpeedKbps,
mbps: downloadSpeedMbps
};
cb(downloadSpeedData);
})
.on('end', () => {
console.log('Your image is downloaded completely....!');
})
.pipe(fs.createWriteStream(options.destinationFilePath));
}
module.exports = {
checkUploadSpeed: checkUploadSpeed,
checkDownloadSpeed: checkDownloadSpeed
}