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')
|
|
|
|
|
const {readFileSync,createWriteStream}=require('node:fs')
|
|
|
|
|
const md5 = require("md5");
|
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'
|
|
|
|
|
});
|
|
|
|
|
static Bucket='note-1324909903'
|
|
|
|
|
static Region = 'ap-beijing'
|
|
|
|
|
|
2024-03-11 06:28:15 -04:00
|
|
|
|
constructor(store) {
|
|
|
|
|
this.store = store;
|
|
|
|
|
}
|
2024-03-13 04:21:03 -04:00
|
|
|
|
static selfUploadFile(activeFile){
|
|
|
|
|
console.log("cos.uploadFile")
|
|
|
|
|
UploadUtils.cos.uploadFile({
|
|
|
|
|
Bucket: UploadUtils.Bucket, /* 填入您自己的存储桶,必须字段 */
|
|
|
|
|
Region: UploadUtils.Region, /* 存储桶所在地域,例如 ap-beijing,必须字段 */
|
|
|
|
|
Key: activeFile, /* 存储在桶里的对象键(例如1.jpg,a/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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static selfDownLoadFile(activeFile){
|
|
|
|
|
console.log("cos.downloadFile",activeFile)
|
|
|
|
|
UploadUtils.cos.getObject({
|
|
|
|
|
Bucket: UploadUtils.Bucket, /* 填入您自己的存储桶,必须字段 */
|
|
|
|
|
Region: UploadUtils.Region, /* 存储桶所在地域,例如 ap-beijing,必须字段 */
|
|
|
|
|
Key: activeFile, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),必须字段 */
|
|
|
|
|
Output: activeFile
|
|
|
|
|
// 支持自定义headers 非必须
|
|
|
|
|
}, function (err, data) {
|
|
|
|
|
console.log(err || data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-11 06:28:15 -04:00
|
|
|
|
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) {
|
2024-03-13 04:21:03 -04:00
|
|
|
|
let dirMessage = JSON.parse(this.store.get("persist:dirMessage"));
|
|
|
|
|
UploadUtils.cos.headObject({
|
|
|
|
|
Bucket:UploadUtils.Bucket,
|
|
|
|
|
Region:UploadUtils.Region,
|
2024-03-11 06:28:15 -04:00
|
|
|
|
// 不能以 / 开头
|
|
|
|
|
Key: activeFile,
|
|
|
|
|
}, function(err, data) {
|
2024-03-13 04:21:03 -04:00
|
|
|
|
console.log("err || data.CommonPrefixes"+activeFile,err || data);
|
2024-03-11 06:28:15 -04:00
|
|
|
|
if (data&&data.ETag){
|
|
|
|
|
// 文件存在,比较MD5值
|
2024-03-13 04:21:03 -04:00
|
|
|
|
let onlineMd5= data.ETag.replaceAll('"',"")
|
|
|
|
|
let fileMd5
|
|
|
|
|
if (dirMessage&&dirMessage.data&&dirMessage.data.activeFile) {
|
|
|
|
|
fileMd5= dirMessage.data.activeFile.fileMd5;
|
|
|
|
|
}
|
|
|
|
|
if (!fileMd5){
|
|
|
|
|
fileMd5=md5(readFileSync(activeFile).toString())
|
|
|
|
|
}
|
|
|
|
|
if (onlineMd5===fileMd5){
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
console.log("fileList[0].ETag",onlineMd5)
|
|
|
|
|
let number = dialog.showMessageBoxSync({
|
|
|
|
|
"message":"云文件已修改是否同步到本地",
|
|
|
|
|
"type":"info",
|
|
|
|
|
"buttons":["是","否"],
|
|
|
|
|
"defaultId":0
|
2024-03-11 06:28:15 -04:00
|
|
|
|
});
|
2024-03-13 04:21:03 -04:00
|
|
|
|
if (number===0){
|
|
|
|
|
UploadUtils.selfDownLoadFile(activeFile)
|
|
|
|
|
}else if(number===1){
|
|
|
|
|
if (dialog.showMessageBoxSync({
|
|
|
|
|
"message":"是否使用本地文件覆盖远程文件",
|
|
|
|
|
"type":"info",
|
|
|
|
|
"buttons":["是","否"],
|
|
|
|
|
"defaultId":0
|
|
|
|
|
})===0){
|
|
|
|
|
UploadUtils.selfUploadFile(activeFile)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
UploadUtils.selfUploadFile(activeFile)
|
2024-03-11 06:28:15 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
syncFile() {
|
|
|
|
|
console.log("同步数据")
|
|
|
|
|
// 1. 先同步active文件数据
|
|
|
|
|
this.syncActiveFile()
|
|
|
|
|
// 2. 同步bar中数据
|
|
|
|
|
// 3. 同步树中的数据。
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = UploadUtils
|