forked from TencentCloud/cos-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (163 loc) · 5.18 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const core = require('@actions/core');
const github = require('@actions/github');
const COS = require('cos-nodejs-sdk-v5');
const fs = require('fs');
const Path = require('path');
const walk = async (path, walkFn) => {
stats = await fs.promises.lstat(path);
if (!stats.isDirectory()) {
return await walkFn(path);
}
const dir = await fs.promises.opendir(path);
for await (const dirent of dir) {
await walk(Path.join(path, dirent.name), walkFn);
}
}
const uploadFileToCOS = (cos, path) => {
return new Promise((resolve, reject) => {
cos.cli.putObject({
Bucket: cos.bucket,
Region: cos.region,
Key: Path.join(cos.remotePath, path),
StorageClass: cos.storageClass,
Body: fs.createReadStream(Path.join(cos.localPath, path)),
}, function(err, data) {
if (err) {
return reject(err);
} else {
return resolve(data);
}
});
});
}
const deleteFileFromCOS = (cos, path) => {
return new Promise((resolve, reject) => {
cos.cli.deleteObject({
Bucket: cos.bucket,
Region: cos.region,
Key: Path.join(cos.remotePath, path)
}, function(err, data) {
if (err) {
return reject(err);
} else {
return resolve(data);
}
});
});
}
const listFilesOnCOS = (cos, nextMarker) => {
return new Promise((resolve, reject) => {
cos.cli.getBucket({
Bucket: cos.bucket,
Region: cos.region,
Prefix: cos.remotePath,
NextMarker: nextMarker
}, function(err, data) {
if (err) {
return reject(err);
} else {
return resolve(data);
}
});
});
}
const collectLocalFiles = async (cos) => {
const root = cos.localPath;
const files = new Set();
await walk(root, (path) => {
let p = path.substring(root.length);
for (;p[0] === '/';) {
p = p.substring(1);
}
files.add(p);
});
return files;
}
const uploadFiles = async (cos, localFiles) => {
const size = localFiles.size;
let index = 0;
let percent = 0;
for (const file of localFiles) {
await uploadFileToCOS(cos, file);
index++;
percent = parseInt(index / size * 100);
console.log(`>> [${index}/${size}, ${percent}%] uploaded ${Path.join(cos.localPath, file)}`);
}
}
const collectRemoteFiles = async (cos) => {
const files = new Set();
let data = {};
let nextMarker = null;
do {
data = await listFilesOnCOS(cos, nextMarker);
for (const e of data.Contents) {
let p = e.Key.substring(cos.remotePath.length);
for (;p[0] === '/';) {
p = p.substring(1);
}
files.add(p);
}
nextMarker = data.NextMarker;
} while (data.IsTruncated === 'true');
return files;
}
const findDeletedFiles = (localFiles, remoteFiles) => {
const deletedFiles = new Set();
for (const file of remoteFiles) {
if (!localFiles.has(file)) {
deletedFiles.add(file);
}
}
return deletedFiles;
}
const cleanDeleteFiles = async (cos, deleteFiles) => {
const size = deleteFiles.size;
let index = 0;
let percent = 0;
for (const file of deleteFiles) {
await deleteFileFromCOS(cos, file);
index++;
percent = parseInt(index / size * 100);
console.log(`>> [${index}/${size}, ${percent}%] cleaned ${Path.join(cos.remotePath, file)}`);
}
}
const process = async (cos) => {
const localFiles = await collectLocalFiles(cos);
console.log(localFiles.size, 'files to be uploaded');
await uploadFiles(cos, localFiles);
let cleanedFilesCount = 0;
if (cos.clean) {
const remoteFiles = await collectRemoteFiles(cos);
const deletedFiles = findDeletedFiles(localFiles, remoteFiles);
if (deletedFiles.size > 0) {
console.log(`${deletedFiles.size} files to be cleaned`);
}
await cleanDeleteFiles(cos, deletedFiles);
cleanedFilesCount = deletedFiles.size;
}
let cleanedFilesMessage = '';
if (cleanedFilesCount > 0) {
cleanedFilesMessage = `, cleaned ${cleanedFilesCount} files`;
}
console.log(`uploaded ${localFiles.size} files${cleanedFilesMessage}`);
}
try {
const cos = {
cli: new COS({
SecretId: core.getInput('secret_id'),
SecretKey: core.getInput('secret_key'),
Domain: core.getInput('accelerate') === 'true' ? '{Bucket}.cos.accelerate.myqcloud.com' : undefined,
}),
bucket: core.getInput('cos_bucket'),
region: core.getInput('cos_region'),
storageClass: core.getInput('cos_storage_class') || 'STANDARD',
localPath: core.getInput('local_path'),
remotePath: core.getInput('remote_path'),
clean: core.getInput('clean') === 'true'
};
process(cos).catch((reason) => {
core.setFailed(`fail to upload files to cos: ${JSON.stringify(reason.message)}`);
});
} catch (error) {
core.setFailed(error.message);
}