|
| 1 | +const fs = require('fs'); |
| 2 | +const https = require('https'); |
| 3 | + |
| 4 | +const FILE_LOCATION = './db/data.db'; |
| 5 | +const DOWNLOAD_URL = |
| 6 | + 'https://github.com/aditya-mitra/dsckiit-website-2.0/releases/download/v2.2/data.db'; |
| 7 | + |
| 8 | +function showProgress(current, totalFileSize, totalFileSizeInMB) { |
| 9 | + process.stdout.clearLine(); |
| 10 | + process.stdout.cursorTo(0); |
| 11 | + const writeLine = |
| 12 | + '\x1b[33m\x1b[1m Downloading the file -- \x1b[43m\x1b[37m' + |
| 13 | + ((100.0 * current) / totalFileSize).toFixed(2) + |
| 14 | + '% \x1b[0m\x1b[33m\x1b[1m (' + |
| 15 | + (current / 1048576).toFixed(2) + |
| 16 | + ' MB) of total file size: ' + |
| 17 | + totalFileSizeInMB.toFixed(2) + |
| 18 | + ' MB \x1b[0m'; |
| 19 | + |
| 20 | + process.stdout.write(writeLine); |
| 21 | +} |
| 22 | + |
| 23 | +function createDBFolder() { |
| 24 | + const folderExists = fs.existsSync('./db'); |
| 25 | + if (!folderExists) { |
| 26 | + console.log('\x1b[0m\x1b[1m\x1b[35m Creating db/ folder\x1b[0m'); |
| 27 | + fs.mkdirSync('./db'); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function showError(error) { |
| 32 | + console.log('\x1b[0m\x1b[1m\x1b[31m Failed to Download! \x1b[0m', error); |
| 33 | +} |
| 34 | + |
| 35 | +/* discuss whether to take the versino from package.json |
| 36 | +function getVersionFromPackageJSON(){ |
| 37 | + const fileContent = fs.readFileSync('./package.json',{encoding:'utf-8'}) |
| 38 | + const packageJson = JSON.parse(fileContent); |
| 39 | + return packageJson.version; |
| 40 | +} |
| 41 | +*/ |
| 42 | + |
| 43 | +function getFile(url) { |
| 44 | + https.get(url, (response) => { |
| 45 | + if (response.statusCode === 301 || response.statusCode === 302) { |
| 46 | + return getFile(response.headers.location); |
| 47 | + } else if (response.statusCode >= 400 && response.statusCode < 600) { |
| 48 | + return showError(`StatusCode : ${response.statusCode}`); |
| 49 | + } |
| 50 | + const fileStream = fs.createWriteStream(FILE_LOCATION); |
| 51 | + const totalFileSize = parseInt(response.headers['content-length'], 10); |
| 52 | + let current = 0; |
| 53 | + const totalFileSizeInMB = totalFileSize / 1048576; |
| 54 | + |
| 55 | + response.on('data', (chunk) => { |
| 56 | + current += chunk.length; |
| 57 | + showProgress(current, totalFileSize, totalFileSizeInMB); |
| 58 | + }); |
| 59 | + |
| 60 | + response.on('end', () => { |
| 61 | + console.log('\n\x1b[1m\x1b[32m Download complete!\x1b[0m'); |
| 62 | + console.log(' \x1b[0m\x1b[1m\x1b[34mDatabase File saved to \x1b[4m./db/data.db\x1b[0m'); |
| 63 | + }); |
| 64 | + |
| 65 | + response.on('error', (err) => { |
| 66 | + return showError(err); |
| 67 | + }); |
| 68 | + |
| 69 | + response.pipe(fileStream); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function run() { |
| 74 | + createDBFolder(); |
| 75 | + console.log('\x1b[1m\x1b[36m Starting Download ...\x1b[0m'); |
| 76 | + getFile(DOWNLOAD_URL); |
| 77 | +} |
| 78 | + |
| 79 | +run(); |
0 commit comments