trtc-electron-sdk
Version:
trtc electron sdk
92 lines (82 loc) • 2.58 kB
JavaScript
const fs = require('fs');
const path = require('path');
const COS = require('cos-nodejs-sdk-v5');
const cosConfig = {
SecretId: '',
SecretKey: ''
};
let enableWrite = false;
const myArgs = process.argv.slice(2);
myArgs.forEach(arg => {
if (arg.includes('=')) {
const [key, value] = arg.split('=');
if (key === 'SecretId' && value) {
// 注意: 确保在蓝盾运行时才打开, 真正上传文档到 cos
enableWrite = true;
cosConfig.SecretId = value;
}
if (key === 'SecretKey' && value) {
cosConfig.SecretKey = value;
}
}
});
if (!cosConfig.SecretId || !cosConfig.SecretKey) {
console.log('unable to upload doc');
}
const cos = new COS({
SecretId: cosConfig.SecretId,
SecretKey: cosConfig.SecretKey
});
const bucket = 'sdk-web-1252463788';
const region = 'ap-hongkong';
const keyPrefix = 'trtc/electron/doc/';
const docPath = path.join(__dirname, '../_doc');
function walkSync(dir, filelist) {
const files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = walkSync(path.join(dir, file), filelist);
} else {
filelist.push({
path: path.join(dir, file),
name: path.relative(docPath, path.join(dir, file)).replace(/\\/g, '/')
});
}
});
return filelist;
};
function getUploadFiles(docFileList) {
return docFileList.map(file => {
return {
Bucket: bucket,
Region: region,
Key: keyPrefix + file.name,
FilePath: file.path,
};
});
}
function uploadFiles() {
const docFileList = [];
walkSync(docPath, docFileList);
const fileList = getUploadFiles(docFileList);
if (enableWrite) {
cos.uploadFiles({
files: fileList,
SliceSize: 1024 * 1024,
onProgress: function (info) {
const percent = parseInt(info.percent * 10000) / 100;
const speed = parseInt((info.speed / 1024 / 1024) * 100) / 100;
console.log(`进度:${percent}%; 速度:${speed}Mb/s;`);
},
onFileFinish: function (err, data, options) {
console.log(`${options.Key}上传${err ? '失败' : '完成'}`);
},
}, function (err, data) {
if (err) {
console.log(JSON.stringify(err));
}
});
}
}
uploadFiles();