forked from coinchimp/kaspool-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from knackroot-technolabs-llp/cloud-backup
Cloud backup
- Loading branch information
Showing
4 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { createReadStream, access, constants } from 'fs'; | ||
import path from 'path'; | ||
import { google } from 'googleapis'; | ||
import config from '../config/config.json' | ||
import googleCredentials from './google-credentials.json'; | ||
|
||
const SCOPES = ["https://www.googleapis.com/auth/drive.file"]; | ||
const fileNameArgs = process.argv.slice(2); | ||
|
||
async function authorize() { | ||
const jwtClient = new google.auth.JWT( | ||
googleCredentials.client_email, | ||
undefined, | ||
googleCredentials.private_key, | ||
SCOPES | ||
); | ||
await jwtClient.authorize(); | ||
return jwtClient; | ||
} | ||
|
||
async function uploadFile(authClient: any) { | ||
const drive = google.drive({ version: "v3", auth: authClient }); | ||
|
||
for(let i = 0; i < fileNameArgs.length; i++) { | ||
access(fileNameArgs[i], constants.F_OK, async (err) => { | ||
if (err) { | ||
console.log(`The file ${fileNameArgs[i]} does not exist in the current directory.`); | ||
} else { | ||
const file = await drive.files.create({ | ||
media: { | ||
body: createReadStream(fileNameArgs[i]), | ||
}, | ||
fields: "id", | ||
requestBody: { | ||
name: path.basename(fileNameArgs[i]), | ||
}, | ||
}); | ||
console.log("File Uploaded :", file.data.id); | ||
const backupEmailAddress = config.backupEmailAddress | ||
await drive.permissions.create({ fileId: file.data.id!, requestBody: { type: 'user', role: 'writer', emailAddress: backupEmailAddress } }) | ||
} | ||
}); | ||
} | ||
} | ||
|
||
(async function main() { | ||
const authClient = await authorize(); | ||
await uploadFile(authClient); | ||
})() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,6 @@ | |
}, | ||
"treasury": { | ||
"fee": 5 | ||
} | ||
}, | ||
"backupEmailAddress": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters