Google Drive Backup upload #3

Merged
Sirherobrine23 merged 4 commits from google-drive-backup into main 2020-12-28 22:11:44 +00:00
3 changed files with 105 additions and 89 deletions
Showing only changes of commit 21414f9887 - Show all commits

View File

@ -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
}
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}"
}`)
};

View File

@ -1,94 +1,83 @@
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
module.exports.drive_backup = (parent_id) => {
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/drive'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';
const SCOPES = ['https://www.googleapis.com/auth/drive'];
const TOKEN_PATH = __dirname+'/token.json';
// Load client secrets from a local file.
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Drive API.
authorize(JSON.parse(content), listFiles);
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
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]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
fs.readFile(__dirname+'/credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
authorize(JSON.parse(content), listFiles);
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
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);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
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);
});
});
}
}
/**
* Lists the names and IDs of up to 10 files.
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listFiles(auth) {
const drive = google.drive({version: 'v3', auth});
var fileMetadata = {
'name': 'teste.zip'
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);
});
});
};
var media = {
mimeType: 'application/octet-stream',
body: fs.createReadStream('teste.zip')
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, file) {
if (err) {
// Handle error
console.error(err);
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 {
console.log('File: ', file.data.id);
}
});
}
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*/

View File

@ -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