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

248 lines
9.9 KiB
JavaScript
Raw Permalink Normal View History

2024-03-11 06:28:15 -04:00
const COS = require('cos-nodejs-sdk-v5');
2024-03-13 04:21:03 -04:00
const {dialog} = require('electron')
2024-03-13 21:11:11 -04:00
const {readFileSync} = require('node:fs')
2024-03-13 04:21:03 -04:00
const md5 = require("md5");
2024-03-13 21:11:11 -04:00
2024-03-11 06:28:15 -04:00
class UploadUtils {
2024-03-13 04:21:03 -04:00
static 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'
});
2024-03-13 21:11:11 -04:00
static Bucket = 'note-1324909903'
2024-03-13 04:21:03 -04:00
static Region = 'ap-beijing'
2024-03-14 02:27:51 -04:00
static staticStore
2024-03-11 06:28:15 -04:00
constructor(store) {
this.store = store;
2024-03-14 02:27:51 -04:00
UploadUtils.staticStore = store;
2024-03-11 06:28:15 -04:00
}
2024-03-13 21:11:11 -04:00
getActiveFile() {
2024-09-28 10:21:21 -04:00
let persist = this.store.get("persist:tableBarItem");
if (persist === undefined){
return undefined
}
2024-03-13 21:11:11 -04:00
let tableBarItem = JSON.parse(this.store.get("persist:tableBarItem"));
if (!tableBarItem) {
return;
}
2024-03-14 02:27:51 -04:00
return tableBarItem.activeKey ? JSON.parse(tableBarItem.activeKey) : undefined;
2024-03-13 21:11:11 -04:00
}
2024-03-13 20:44:56 -04:00
2024-03-13 21:11:11 -04:00
/*
* 检测本地和远程文件是否相同
*/
getLocalFileMd5(activeFile) {
let dirMessage = JSON.parse(this.store.get("persist:dirMessage"));
let fileMd5
if (activeFile && dirMessage && dirMessage.data) {
let find = JSON.parse(dirMessage.data, []).find(file => file.fileId === activeFile || file.filePath === activeFile);
if (find) {
fileMd5 = find.fileMd5;
}
}
if (!fileMd5) {
fileMd5 = md5(readFileSync(activeFile).toString())
}
return fileMd5
}
2024-03-13 20:44:56 -04:00
2024-03-13 21:11:11 -04:00
upLoadFileUtil(activeFile) {
// 判断文件是否相同
let fileMd5 = this.getLocalFileMd5(activeFile);
UploadUtils.cos.headObject({
Bucket: UploadUtils.Bucket,
Region: UploadUtils.Region,
// 不能以 / 开头
2024-03-14 02:27:51 -04:00
Key: process.platform==='win32'?activeFile.replaceAll("\\","/"):activeFile
2024-03-13 21:11:11 -04:00
}, function (err, data) {
2024-03-14 02:27:51 -04:00
console.log('upLoadFileUtil:',err || data);
2024-03-13 21:11:11 -04:00
if (data && data.ETag) {
// 文件存在比较MD5值
let onlineMd5 = data.ETag.replaceAll('"', "")
if (onlineMd5 === fileMd5) {
dialog.showMessageBoxSync({
"message": "云文件和本地相同无需同步",
"type": "info",
"buttons": ["确认"],
"defaultId": 0
});
2024-03-14 02:27:51 -04:00
return
2024-03-13 21:11:11 -04:00
}
}
2024-03-14 02:27:51 -04:00
UploadUtils.selfUploadFile(activeFile,fileMd5)
2024-03-13 21:11:11 -04:00
})
}
downLoadFileUtil(activeFile) {
// 判断文件是否相同
let fileMd5 = this.getLocalFileMd5(activeFile);
UploadUtils.cos.headObject({
Bucket: UploadUtils.Bucket,
Region: UploadUtils.Region,
// 不能以 / 开头
2024-03-14 02:27:51 -04:00
Key: process.platform==='win32'?activeFile.replaceAll("\\","/"):activeFile,
2024-03-13 21:11:11 -04:00
}, function (err, data) {
2024-03-14 02:27:51 -04:00
console.log('downLoadFileUtil:',err || data);
2024-03-13 21:11:11 -04:00
if (data && data.ETag) {
// 文件存在比较MD5值
let onlineMd5 = data.ETag.replaceAll('"', "")
if (onlineMd5 === fileMd5) {
dialog.showMessageBoxSync({
"message": "云文件和本地相同无需同步",
"type": "info",
"buttons": ["确认"],
"defaultId": 0
});
2024-03-14 02:27:51 -04:00
return
2024-03-13 21:11:11 -04:00
}
}
2024-03-14 02:27:51 -04:00
UploadUtils.selfDownLoadFile(activeFile)
2024-03-13 21:11:11 -04:00
})
}
2024-03-14 02:27:51 -04:00
static selfUploadFile(activeFile,fileMd5) {
console.log("cos.uploadFile",activeFile,fileMd5)
2024-03-13 04:21:03 -04:00
UploadUtils.cos.uploadFile({
Bucket: UploadUtils.Bucket, /* 填入您自己的存储桶,必须字段 */
Region: UploadUtils.Region, /* 存储桶所在地域,例如 ap-beijing必须字段 */
2024-03-14 02:27:51 -04:00
Key: process.platform==='win32'?activeFile.replaceAll("\\","/"):activeFile, /* 存储在桶里的对象键例如1.jpga/b/test.txt必须字段 */
2024-03-13 04:21:03 -04:00
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 ? '失败' : '完成'));
2024-03-13 21:11:11 -04:00
if (err){
dialog.showMessageBoxSync({
"message": err,
"type": "error",
"buttons": ["确认"],
"defaultId": 0
})
}else {
dialog.showMessageBoxSync({
"message": "同步完成",
"type": "info",
"buttons": ["确认"],
"defaultId": 0
})
}
2024-03-13 04:21:03 -04:00
},
// 支持自定义headers 非必须
}, function (err, data) {
2024-03-14 02:27:51 -04:00
console.log("uploadFile:callback:",err || data);
2024-03-13 21:11:11 -04:00
2024-03-13 04:21:03 -04:00
});
}
2024-03-13 21:11:11 -04:00
static selfDownLoadFile(activeFile) {
console.log("cos.downloadFile", activeFile)
2024-03-13 04:21:03 -04:00
UploadUtils.cos.getObject({
Bucket: UploadUtils.Bucket, /* 填入您自己的存储桶,必须字段 */
Region: UploadUtils.Region, /* 存储桶所在地域,例如 ap-beijing必须字段 */
2024-03-14 02:27:51 -04:00
Key: process.platform==='win32'?activeFile.replaceAll("\\","/"):activeFile, /* 存储在桶里的对象键例如1.jpga/b/test.txt必须字段 */
2024-03-13 04:21:03 -04:00
Output: activeFile
// 支持自定义headers 非必须
}, function (err, data) {
console.log(err || data);
2024-03-13 21:11:11 -04:00
if (err){
dialog.showMessageBoxSync({
"message": err,
"type": "error",
"buttons": ["确认"],
"defaultId": 0
})
}else {
dialog.showMessageBoxSync({
"message": "同步完成",
"type": "info",
"buttons": ["确认"],
"defaultId": 0
})
2024-03-14 02:27:51 -04:00
// 修改store中Md5值
let persist = UploadUtils.staticStore.get("persist:dirMessage");
if (persist){
let persistObj = JSON.parse(persist)
let data1 = persistObj.data;
console.log("data1",data1,Array.isArray(data1))
let parseArray = JSON.parse(data1, []);
let find =parseArray.find(file => file.fileId === activeFile || file.filePath === activeFile);
if (find) {
find.fileMd5=data.ETag.replaceAll('"',"");
}
persistObj.data=JSON.stringify(parseArray)
UploadUtils.staticStore.set("persist:dirMessage",JSON.stringify(persistObj))
}
2024-03-13 21:11:11 -04:00
}
2024-03-13 04:21:03 -04:00
});
}
2024-03-13 20:44:56 -04:00
syncActiveFile() {
let activeFile = this.getActiveFile();
2024-03-11 06:28:15 -04:00
console.log("activeFile:", activeFile)
if (activeFile) {
2024-03-13 21:11:11 -04:00
let fileMd5 = this.getLocalFileMd5(activeFile);
2024-03-13 04:21:03 -04:00
UploadUtils.cos.headObject({
2024-03-13 21:11:11 -04:00
Bucket: UploadUtils.Bucket,
Region: UploadUtils.Region,
2024-03-11 06:28:15 -04:00
// 不能以 / 开头
2024-03-14 02:27:51 -04:00
Key: process.platform==='win32'?activeFile.replaceAll("\\","/"):activeFile,
2024-03-13 21:11:11 -04:00
}, function (err, data) {
console.log("err || data.CommonPrefixes" + activeFile, err || data);
if (data && data.ETag) {
2024-03-11 06:28:15 -04:00
// 文件存在比较MD5值
2024-03-13 21:11:11 -04:00
let onlineMd5 = data.ETag.replaceAll('"', "")
if (onlineMd5 === fileMd5) {
2024-03-13 04:21:03 -04:00
return
}
2024-03-13 21:11:11 -04:00
console.log("fileList[0].ETag", onlineMd5)
2024-04-09 21:21:50 -04:00
dialog.showMessageBox({
2024-03-13 21:11:11 -04:00
"message": "云文件已修改是否同步到本地",
"type": "info",
"buttons": ["是", "否"],
"defaultId": 0
2024-04-09 21:21:50 -04:00
}).then(result=>{
let number = result.response
if (number === 0) {
UploadUtils.selfDownLoadFile(activeFile)
} else if (number === 1) {
dialog.showMessageBox({
"message": "是否使用本地文件覆盖远程文件",
"type": "info",
"buttons": ["是", "否"],
"defaultId": 0
}).then(result=>{
if (result.response===0){
UploadUtils.selfUploadFile(activeFile,fileMd5)
}
})
2024-03-13 04:21:03 -04:00
}
2024-04-09 21:21:50 -04:00
});
2024-03-13 21:11:11 -04:00
} else {
2024-03-14 02:27:51 -04:00
UploadUtils.selfUploadFile(activeFile,fileMd5)
2024-03-11 06:28:15 -04:00
}
});
}
}
syncFile() {
console.log("同步数据")
// 1. 先同步active文件数据
this.syncActiveFile()
// 2. 同步bar中数据
// 3. 同步树中的数据。
}
}
module.exports = UploadUtils