forked from Jodebu/upload-to-drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (99 loc) · 3.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const actions = require('@actions/core');
const { google } = require('googleapis');
const fs = require('fs');
const archiver = require('archiver');
/** Google Service Account credentials encoded in base64 */
const credentials = actions.getInput('credentials', { required: true });
/** Google Drive Folder ID to upload the file/folder to */
const folder = actions.getInput('folder', { required: true });
/** Local path to the file/folder to upload */
const target = actions.getInput('target', { required: true });
/** Optional name for the zipped file */
const name = actions.getInput('name', { required: true });
/** Link to the Drive folder */
const link = 'link';
const credentialsJSON = JSON.parse(Buffer.from(credentials, 'base64').toString());
const scopes = ['https://www.googleapis.com/auth/drive'];
const auth = new google.auth.JWT(credentialsJSON.client_email, null, credentialsJSON.private_key, scopes);
const drive = google.drive({ version: 'v3', auth });
const driveLink = `https://drive.google.com/drive/folders/${folder}`
// const targetPath = target.split('/').pop();
async function main() {
actions.setOutput(link, driveLink);
const nowMs=Date.now();
const dateNow=new Date(nowMs);
const zipFileName=`${name}-${dateNow.toISOString().split('T')[0]}-${nowMs}.zip`
actions.info(`Folder detected in ${target}`)
actions.info(`Zipping ${target}...`)
let zipPromise=null
if(fs.lstatSync(target).isDirectory()){
zipPromise=zipDirectory(target,zipFileName)
}else{
zipPromise=zipFile(target,zipFileName)
}
if(zipPromise){
zipPromise
.then(()=>uploadToDrive(zipFileName))
.catch((e)=>{
actions.error('Zip failed');
throw(e)
})
}
}
/**
* Zips a directory and stores it in memory
* @param {string} source File or folder to be zipped
* @param {string} out Name of the resulting zipped file
*/
function zipDirectory(source, out) {
const archive = archiver('zip', { zlib: { level: 9 }});
const stream = fs.createWriteStream(out);
return new Promise((resolve, reject) => {
archive
.directory(source, false)
.on('error', err => reject(err))
.pipe(stream);
stream.on('close',
() => {
actions.info(`Folder successfully zipped: ${archive.pointer()} total bytes written`);
return resolve();
});
archive.finalize();
});
}
function zipFile(file,out){
const archive = archiver('zip', { zlib: { level: 9 }});
const stream = fs.createWriteStream(out);
return new Promise((resolve,reject)=>{
const fileName = file.split('/').pop();
archive
.file(file,{name:fileName})
.on("error",(e)=>reject(e))
.pipe(stream)
stream.on('close',()=>{
actions.info(`Folder successfully zipped: ${archive.pointer()} total bytes written`);
return resolve();
})
archive.finalize()
})
}
/**
* Uploads the file to Google Drive
*/
function uploadToDrive(zipFile) {
actions.info('Uploading file to Goole Drive...');
drive.files.create({
requestBody: {
name: zipFile,
parents: [folder]
},
media: {
body: fs.createReadStream(zipFile)
}
}).then(() => actions.info('File uploaded successfully'))
.catch(e => {
actions.error('Upload failed');
throw e;
});
}
main().catch(e => actions.setFailed(e));