forked from skymins04/r2-dir-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (83 loc) · 3.49 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
var aws = require('aws-sdk');
const core = require('@actions/core');
var path = require("path");
var fs = require('fs');
var mime = require('mime');
var crypto = require('crypto');
try {
const accountid = core.getInput('accountid');
const accesskeyid = core.getInput('accesskeyid');
const secretaccesskey = core.getInput('secretaccesskey');
const bucket = core.getInput('bucket');
const source = core.getInput('source');
const destination = core.getInput('destination');
const filenamePattern = core.getInput('filenamePattern');
const uploadDir = function(s3Path, bucketName) {
var s3 = new aws.S3({
accessKeyId: accesskeyid,
secretAccessKey: secretaccesskey,
endpoint: accountid + ".r2.cloudflarestorage.com"
});
function walkSync(currentDirPath, callback) {
fs.readdirSync(currentDirPath).forEach(function (name) {
var filePath = path.join(currentDirPath, name);
var stat = fs.statSync(filePath);
if (stat.isFile()) {
console.log(filePath);
callback(filePath, stat);
} else if (stat.isDirectory()) {
walkSync(filePath, callback);
}
});
}
function getLocalFileMD5(filePath) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash('md5');
const stream = fs.createReadStream(filePath);
stream.on('data', (data) => hash.update(data));
stream.on('end', () => resolve(hash.digest('hex')));
stream.on('error', (err) => reject(err));
});
}
function getS3ETag(bucketName, key) {
return s3.headObject({ Bucket: bucketName, Key: key }).promise()
.then(data => data.ETag.replace(/"/g, ''))
.catch(err => {
if (err.code === 'NotFound') {
return null;
}
throw err;
});
}
walkSync(s3Path, async function(filePath, stat) {
let fileName = path.basename(filePath);
if (!new RegExp(filenamePattern).test(fileName)) {
console.log(`Skipping upload for ${filePath}: Filename does not match pattern.`);
return;
}
let bucketPath = path.join(destination, filePath.substring(s3Path.length + 1));
let localFileMD5 = await getLocalFileMD5(filePath);
let s3ETag = await getS3ETag(bucketName, bucketPath);
if (s3ETag === localFileMD5) {
console.log(`Skipping upload for ${bucketPath}: File is unchanged.`);
} else {
let params = {
Bucket: bucketName,
Key: bucketPath,
Body: fs.readFileSync(filePath),
ContentType: mime.getType(filePath)
};
s3.putObject(params, function(err, data) {
if (err) {
console.log(err);
} else {
console.log(`Successfully uploaded ${bucketPath} to ${bucketName}`);
}
});
}
});
};
uploadDir(source, bucket);
} catch (error) {
core.setFailed(error.message);
}