|
| 1 | +/* |
| 2 | +This file was "borrowed" from a module called drive-upload (https://www.npmjs.com/package/drive-upload) and modified a little bit, because it didn't work on my system |
| 3 | +*/ |
| 4 | +const fs = require('fs'); |
| 5 | +const readline = require('readline'); |
| 6 | +const {google} = require('googleapis'); |
| 7 | +const FileType = require('file-type'); |
| 8 | + |
| 9 | +const SCOPES = ['https://www.googleapis.com/auth/drive']; |
| 10 | + |
| 11 | +let options = { |
| 12 | + credentials: './credentials.json', |
| 13 | + token: './token.json', |
| 14 | + permissions: { |
| 15 | + 'type': 'anyone', |
| 16 | + 'role': 'reader' |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +exports.setOptions = opts => { |
| 21 | + if (opts.driveFolder) { |
| 22 | + options['driveFolder'] = opts.driveFolder; |
| 23 | + } |
| 24 | + if (opts.permissions && Object.keys(opts.permissions).length > 0) { |
| 25 | + options['permissions'] = opts.permissions; |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +exports.store = (srcFile, destFile, callback) => { |
| 30 | + fs.readFile(options.credentials, (err, content) => { |
| 31 | + if (err) return console.log('Error loading client secret file:', err); |
| 32 | + authorize(JSON.parse(content), auth => { |
| 33 | + FileType.fromFile(srcFile) |
| 34 | + .then(fileData => { |
| 35 | + fileData = {ext: 'zip', mime: 'application/zip'}; |
| 36 | + const drive = google.drive({version: 'v3', auth}); |
| 37 | + const fileMetadata = { |
| 38 | + 'name': destFile ? destFile.split('/').pop() : srcFile.split('/').pop(), |
| 39 | + parents: options.driveFolder ? [options.driveFolder] : null |
| 40 | + }; |
| 41 | + const media = { |
| 42 | + mimeType: fileData.mime, |
| 43 | + body: fs.createReadStream(srcFile) |
| 44 | + }; |
| 45 | + drive.files.create({ |
| 46 | + resource: fileMetadata, |
| 47 | + media: media, |
| 48 | + fields: 'id' |
| 49 | + }, function (err, file) { |
| 50 | + if (err) { |
| 51 | + console.error(err); |
| 52 | + } else { |
| 53 | + const trans = options.permissions.role === 'owner'; |
| 54 | + drive.permissions.create({ |
| 55 | + fileId: file.data.id, |
| 56 | + requestBody: options.permissions, |
| 57 | + transferOwnership: trans |
| 58 | + }); |
| 59 | + drive.files.get({ |
| 60 | + fileId: file.data.id, |
| 61 | + fields: 'id,name,mimeType,parents,webContentLink,webViewLink,thumbnailLink,createdTime,size,imageMediaMetadata' |
| 62 | + }).then(response => { |
| 63 | + response.data['webLink'] = response.data.webContentLink.replace('&export=download', ''); |
| 64 | + if (callback && typeof callback === 'function') { |
| 65 | + callback(response.data); |
| 66 | + } else { |
| 67 | + console.log('fileData:', response.data); |
| 68 | + } |
| 69 | + }).catch(err => console.log('Drive error:', err)); |
| 70 | + } |
| 71 | + }); |
| 72 | + }) |
| 73 | + .catch(err => { |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}; |
| 78 | + |
| 79 | +function authorize(credentials, callback) { |
| 80 | + const {client_secret, client_id, redirect_uris} = credentials.installed; |
| 81 | + const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]); |
| 82 | + fs.readFile(options.token, (err, token) => { |
| 83 | + if (err) { |
| 84 | + return getAccessToken(oAuth2Client, callback); |
| 85 | + } |
| 86 | + oAuth2Client.setCredentials(JSON.parse(token)); |
| 87 | + callback(oAuth2Client); |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +function getAccessToken(oAuth2Client, callback) { |
| 92 | + const authUrl = oAuth2Client.generateAuthUrl({ |
| 93 | + access_type: 'offline', |
| 94 | + scope: SCOPES |
| 95 | + }); |
| 96 | + console.log('Authorize this app by visiting this url:', authUrl); |
| 97 | + const rl = readline.createInterface({ |
| 98 | + input: process.stdin, |
| 99 | + output: process.stdout |
| 100 | + }); |
| 101 | + rl.question('Enter the code from that page here: ', (code) => { |
| 102 | + rl.close(); |
| 103 | + oAuth2Client.getToken(code, (err, token) => { |
| 104 | + if (err) return console.error('Error retrieving access token', err); |
| 105 | + oAuth2Client.setCredentials(token); |
| 106 | + |
| 107 | + fs.writeFile(options.token, JSON.stringify(token), (err) => { |
| 108 | + if (err) return console.error(err); |
| 109 | + }); |
| 110 | + callback(oAuth2Client); |
| 111 | + }); |
| 112 | + }); |
| 113 | +} |
0 commit comments