assistant-note/elsrc/sync/tencent/UploadUtils.js

72 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const COS = require('cos-nodejs-sdk-v5');
const pathOpt = require("path");
const cos = new COS({
SecretId: 'AKIDvjKhqrfEaliRq11nMcrGZmsATiyNl1BA',
// 推荐使用环境变量获取;用户的 SecretId建议使用子账号密钥授权遵循最小权限指引降低使用风险。
// 子账号密钥获取可参考https://cloud.tencent.com/document/product/598/37140
SecretKey: 'xpZCjCTVJzZG2wyy8mFVwLWTVVIqAKct',
Domain: 'https://note-1324909903.cos.ap-beijing.myqcloud.com'
});
const Bucket='note-1324909903'
const Region = 'ap-beijing'
class UploadUtils {
constructor(store) {
this.store = store;
}
syncActiveFile() {
let tableBarItem = JSON.parse(this.store.get("persist:tableBarItem"));
if (!tableBarItem) {
return;
}
let activeFile = tableBarItem.activeKey?tableBarItem.activeKey.replaceAll('"',""):undefined;
console.log("activeFile:", activeFile)
if (activeFile) {
cos.headObject({
Bucket:Bucket,
Region:Region,
// 不能以 / 开头
Key: activeFile,
}, function(err, data) {
console.log("err || data.CommonPrefixes",err || data);
if (data&&data.ETag){
// 文件存在比较MD5值
let md5= data.ETag.replaceAll('"',"")
console.log("fileList[0].ETag",md5)
}else {
console.log("cos.uploadFile")
cos.uploadFile({
Bucket: Bucket, /* 填入您自己的存储桶,必须字段 */
Region: Region, /* 存储桶所在地域,例如 ap-beijing必须字段 */
Key: activeFile, /* 存储在桶里的对象键例如1.jpga/b/test.txt必须字段 */
FilePath: activeFile, /* 必须 */
SliceSize: 1024 * 1024 * 5, /* 触发分块上传的阈值超过5MB使用分块上传非必须 */
onTaskReady: function (taskId) { /* 非必须 */
console.log(taskId);
},
onProgress: function (progressData) { /* 非必须 */
console.log(JSON.stringify(progressData));
},
onFileFinish: function (err, data, options) { /* 非必须 */
console.log(options.Key + '上传' + (err ? '失败' : '完成'));
},
// 支持自定义headers 非必须
}, function (err, data) {
console.log(err || data);
});
}
});
}
}
syncFile() {
console.log("同步数据")
// 1. 先同步active文件数据
this.syncActiveFile()
// 2. 同步bar中数据
// 3. 同步树中的数据。
}
}
module.exports = UploadUtils