diff --git a/Services/backup.js b/Services/backup.js index 4fd5c2d..74fc5de 100644 --- a/Services/backup.js +++ b/Services/backup.js @@ -1,4 +1,4 @@ -function World_BAckup() { +module.exports.World_BAckup = () => { if (process.platform == "win32") { var dd = String(today.getDate()).padStart(2, '0'); @@ -35,6 +35,28 @@ function World_BAckup() { return 'Sucess' }; -module.exports = { - World_BAckup: World_BAckup -} \ No newline at end of file +module.exports.Drive_backup = () => { + const bds = require('../index'); + const path = require('path'); + const dir_zip = bds.world_dir; + const today = bds.date(); + const file_name = `bds_backup_World_${today}.zip` + const name = path.join(bds.tmp_dir, file_name) + /* Compress the folders */ + var AdmZip = require('adm-zip'); + var zip = new AdmZip(); + zip.addLocalFolder(dir_zip); /* var willSendthis = zip.toBuffer(); */ + zip.addZipComment(`Backup zip file in ${today}. \nBackup made to ${process.platform}, Free and open content for all\n\nSirherobrine23© By Bds Maneger.`) + var zipEntries = zip.getEntries(); + // zipEntries.forEach(function (zipEntry) { + // console.log(zipEntry.entryName.toString()); + // }); + zip.writeZip(name); /* Zip file destination */ + console.log('Backup Sucess') + /* Compress the folders */ +return JSON.parse(`{ + "file_dir": "${name.replaceAll('\\', '/')}", + "file_name": "${file_name}" +}`) +}; + diff --git a/Services/drive/.gitignore b/Services/drive/.gitignore new file mode 100644 index 0000000..eb47d3c --- /dev/null +++ b/Services/drive/.gitignore @@ -0,0 +1,2 @@ +token.json +Google Driver API bds.txt \ No newline at end of file diff --git a/Services/drive/auth.js b/Services/drive/auth.js new file mode 100644 index 0000000..c813f90 --- /dev/null +++ b/Services/drive/auth.js @@ -0,0 +1,83 @@ +module.exports.drive_backup = (parent_id) => { + const fs = require('fs'); + const readline = require('readline'); + const {google} = require('googleapis'); + + const SCOPES = ['https://www.googleapis.com/auth/drive']; + const TOKEN_PATH = __dirname+'/token.json'; + + fs.readFile(__dirname+'/credentials.json', (err, content) => { + if (err) return console.log('Error loading client secret file:', err); + authorize(JSON.parse(content), listFiles); + }); + + + function authorize(credentials, callback) { + const {client_secret, client_id, redirect_uris} = credentials.installed; + const oAuth2Client = new google.auth.OAuth2( + client_id, client_secret, redirect_uris[0]); + + fs.readFile(TOKEN_PATH, (err, token) => { + if (err) return getAccessToken(oAuth2Client, callback); + oAuth2Client.setCredentials(JSON.parse(token)); + callback(oAuth2Client); + }); + } + + function getAccessToken(oAuth2Client, callback) { + const authUrl = oAuth2Client.generateAuthUrl({ + access_type: 'offline', + scope: SCOPES, + }); + console.log('Authorize this app by visiting this url:', authUrl); + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + }); + rl.question('Enter the code from that page here: ', (code) => { + rl.close(); + oAuth2Client.getToken(code, (err, token) => { + if (err) return console.error('Error retrieving access token', err); + oAuth2Client.setCredentials(token); + fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => { + if (err) return console.error(err); + console.log('Token stored to', TOKEN_PATH); + }); + callback(oAuth2Client); + }); + }); + }; + function listFiles(auth) { + const bds_backup = require('../backup').Drive_backup() + const drive = google.drive({version: 'v3', auth}); + if (parent_id == undefined){ + var fileMetadata = { + 'name': bds_backup.file_name, + }; + console.log('Your backup will be saved to My Drive') + } else { + var fileMetadata = { + 'name': bds_backup.file_name, + parents: [parent_id] + }; + }; + var media = { + mimeType: 'application/octet-stream', + body: fs.createReadStream(bds_backup.file_dir) + }; + drive.files.create({ + resource: fileMetadata, + media: media, + fields: 'id', + }, function (err, file) { + if (err) { + // Handle error + console.error(err); + } else { + global.backup_id = file.data.id; + console.log('File: ', file.data.id); + } + }); + } + return 'Use backup_id para ter o id do ultimo arquivo' +}; /*End*/ \ No newline at end of file diff --git a/Services/drive/credentials.json b/Services/drive/credentials.json new file mode 100644 index 0000000..a49363f --- /dev/null +++ b/Services/drive/credentials.json @@ -0,0 +1 @@ +{"installed":{"client_id":"1000381612165-cukb31ib1ej3vnt9lbh8v0nvlrvvtp4f.apps.googleusercontent.com","project_id":"bds-maneger-api--1609019381362","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"PwfAhb7-mKqt7UozGUcETFWT","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}} \ No newline at end of file diff --git a/Services/drive/teste.zip b/Services/drive/teste.zip new file mode 100644 index 0000000..948ef49 Binary files /dev/null and b/Services/drive/teste.zip differ diff --git a/index.js b/index.js index 3876cad..a4fe63f 100644 --- a/index.js +++ b/index.js @@ -40,6 +40,7 @@ if (process.platform == 'win32') { }; var log_file = path.join(log_dir, `${date()}_Bds_log.log`) var log_date = `${date()}` + var tmp = process.env.TMP var system = `windows`; } else if (process.platform == 'linux') { var home = process.env.HOME; @@ -53,6 +54,7 @@ if (process.platform == 'win32') { }; var log_file = path.join(log_dir, `${date()}_Bds_log.log`) var log_date = `${date()}` + var tmp = `/tmp` var system = `linux`; } else if (process.platform == 'darwin') { require("open")("https://github.com/Bds-Maneger/Bds_Maneger/wiki/systems-support#a-message-for-mac-os-users") @@ -82,6 +84,8 @@ module.exports.token = telegram_tokenv1() module.exports.home = home module.exports.system = system module.exports.server_dir = server_dir +module.exports.world_dir = path.join(server_dir, 'worlds') +module.exports.tmp_dir = tmp module.exports.electron = electron_de module.exports.api_dir = cache_dir module.exports.log_file = log_file @@ -97,6 +101,7 @@ module.exports.stop = require('./Services/stop').Server_stop module.exports.date = date module.exports.command = require('./Services/command').command module.exports.backup = require("./Services/backup").World_BAckup +module.exports.drive_backup = require('./Services/drive/auth').drive_backup module.exports.kill = require("./Services/kill").bds_kill module.exports.version_Download = require("./Services/download").DownloadBDS module.exports.set_config = require("./Services/bds_settings").config diff --git a/package.json b/package.json index 02ec79f..67bd197 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "homepage": "https://github.com/Bds-Maneger/bds_maneger_api#readme", "dependencies": { "adm-zip": "^0.5.1", + "googleapis": "^39.2.0", "node-fetch": "^2.6.1", "node-localstorage": "^2.1.6", "open": "^7.3.0",