picgo-plugin-gitee 2.1.2 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/src/index.js +111 -48
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "picgo-plugin-gitee",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "picgo uploader for gitee",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -31,7 +31,8 @@
31
31
  "release:patch": "npm run bump:patch && npm publish",
32
32
  "release:minor": "npm run bump:minor && npm publish",
33
33
  "release:major": "npm run bump:major && npm publish",
34
- "prepublishOnly": "node --check src/index.js"
34
+ "prepublishOnly": "node --check src/index.js",
35
+ "publish": "npm publish"
35
36
  },
36
37
  "keywords": [
37
38
  "picgo",
package/src/index.js CHANGED
@@ -41,7 +41,18 @@ module.exports = (ctx) => {
41
41
  const stack = err && err.stack ? err.stack : "";
42
42
  log("error", "[同步]失败:" + msg);
43
43
  if (stack) log("error", stack);
44
- // 弹窗 + 系统通知,三重保险
44
+ // 通道 1:ctx.emit('notification') — PicGo 主窗口消息中心(最可靠)
45
+ try {
46
+ if (ctx && ctx.emit) {
47
+ ctx.emit("notification", {
48
+ title: "Gitee 同步失败",
49
+ body: msg,
50
+ });
51
+ }
52
+ } catch (e) {
53
+ log("error", "[同步]ctx.emit 失败:" + e.message);
54
+ }
55
+ // 通道 2:showMessageBox — 系统弹窗,最醒目
45
56
  if (guiApi && guiApi.showMessageBox) {
46
57
  try {
47
58
  guiApi.showMessageBox({
@@ -54,6 +65,7 @@ module.exports = (ctx) => {
54
65
  log("error", "[同步]showMessageBox 失败:" + e.message);
55
66
  }
56
67
  }
68
+ // 通道 3:showNotification — 系统通知(依赖 OS 权限)
57
69
  if (guiApi && guiApi.showNotification) {
58
70
  try {
59
71
  guiApi.showNotification({
@@ -171,12 +183,7 @@ module.exports = (ctx) => {
171
183
  `[同步]配置 OK: owner=${userConfig.owner} repo=${userConfig.repo} path=${userConfig.path || "(root)"}`
172
184
  );
173
185
 
174
- if (guiApi.showNotification) {
175
- guiApi.showNotification({
176
- title: "正在同步",
177
- body: "正在从 Gitee 拉取远程文件...",
178
- });
179
- }
186
+ notify("success", ctx, guiApi, "正在同步", "正在从 Gitee 拉取远程文件...");
180
187
 
181
188
  log("info", "[同步]开始递归拉取 Gitee 文件列表");
182
189
  const remoteFiles = await listAllRemoteFiles(userConfig);
@@ -184,17 +191,25 @@ module.exports = (ctx) => {
184
191
 
185
192
  if (remoteFiles.length === 0) {
186
193
  log("warn", "[同步]仓库里没有任何图片文件");
187
- if (guiApi.showNotification) {
188
- guiApi.showNotification({
189
- title: "Gitee 同步完成",
190
- body: "仓库里没有任何图片文件",
191
- });
192
- }
194
+ notify(
195
+ "success",
196
+ ctx,
197
+ guiApi,
198
+ "Gitee 同步完成",
199
+ "仓库里没有任何图片文件"
200
+ );
193
201
  return;
194
202
  }
195
203
 
196
204
  log("info", "[同步]读取本地相册");
197
- const existing = (await guiApi.galleryDB.get()) || [];
205
+ // galleryDB.get() 返回 { total, data }(picgo/store 格式),
206
+ // 也可能直接返回数组(早期版本兼容)。两种都处理。
207
+ const getResult = await guiApi.galleryDB.get();
208
+ const existing = Array.isArray(getResult)
209
+ ? getResult
210
+ : getResult && Array.isArray(getResult.data)
211
+ ? getResult.data
212
+ : [];
198
213
  log("info", `[同步]本地相册已有 ${existing.length} 条`);
199
214
  const existingUrls = new Set(existing.map((x) => x.imgUrl));
200
215
  const newItems = remoteFiles
@@ -212,12 +227,13 @@ module.exports = (ctx) => {
212
227
  );
213
228
 
214
229
  if (newItems.length === 0) {
215
- if (guiApi.showNotification) {
216
- guiApi.showNotification({
217
- title: "Gitee 同步完成",
218
- body: "没有新增文件,相册已是最新",
219
- });
220
- }
230
+ notify(
231
+ "success",
232
+ ctx,
233
+ guiApi,
234
+ "Gitee 同步完成",
235
+ "没有新增文件,相册已是最新"
236
+ );
221
237
  return;
222
238
  }
223
239
 
@@ -227,11 +243,51 @@ module.exports = (ctx) => {
227
243
  "success",
228
244
  `[同步]完成:共 ${remoteFiles.length} 个,新增 ${newItems.length} 个`
229
245
  );
230
- if (guiApi.showNotification) {
231
- guiApi.showNotification({
232
- title: "Gitee 同步完成",
233
- body: `新增 ${newItems.length} 个文件到相册(共发现 ${remoteFiles.length} 个)`,
234
- });
246
+ notify(
247
+ "success",
248
+ ctx,
249
+ guiApi,
250
+ "Gitee 同步完成",
251
+ `新增 ${newItems.length} 个文件到相册(共发现 ${remoteFiles.length} 个)`
252
+ );
253
+ };
254
+
255
+ // 多通道通知:根据场景选用合适的通道。
256
+ // picgo 文档说明:ctx.emit('notification') 是给**失败**提示专用的通道;
257
+ // 成功时 picgo 主进程已经处理(gui 弹窗/消息中心),插件无需重复 emit。
258
+ // 所以 notify 函数有 success / fail 两种模式。
259
+ const notify = function (mode, ctx, guiApi, title, body) {
260
+ log(mode === "fail" ? "error" : "success", `[notify] ${title}: ${body}`);
261
+
262
+ if (mode === "fail") {
263
+ // 失败:ctx.emit(picgo 主窗口消息中心)+ showMessageBox 弹窗(最醒目)+ showNotification
264
+ try {
265
+ if (ctx && ctx.emit) {
266
+ ctx.emit("notification", { title, body });
267
+ }
268
+ } catch (e) {
269
+ log("error", "[notify] ctx.emit 失败:" + e.message);
270
+ }
271
+ try {
272
+ if (guiApi && guiApi.showMessageBox) {
273
+ guiApi.showMessageBox({
274
+ type: "error",
275
+ title: title,
276
+ message: body,
277
+ });
278
+ }
279
+ } catch (e) {
280
+ log("error", "[notify] showMessageBox 失败:" + e.message);
281
+ }
282
+ }
283
+
284
+ // 成功和失败都触发系统通知(依赖 OS 权限)
285
+ try {
286
+ if (guiApi && guiApi.showNotification) {
287
+ log("11111", guiApi.showNotification({ title, body }));
288
+ }
289
+ } catch (e) {
290
+ log("error", "[notify] showNotification 失败:" + e.message);
235
291
  }
236
292
  };
237
293
 
@@ -317,13 +373,8 @@ module.exports = (ctx) => {
317
373
  delete imgList[i].buffer;
318
374
  }
319
375
 
320
- // 上传结束后统一给一条汇总通知,避免每次上传都点弹窗打扰用户。
321
- if (successCount > 0) {
322
- ctx.emit("notification", {
323
- title: "上传完成",
324
- body: `成功 ${successCount} 张${failCount > 0 ? `,失败 ${failCount} 张` : ""}`,
325
- });
326
- }
376
+ // 上传成功:让 picgo 主进程自己处理(它会弹"上传成功"提示),
377
+ // 不重复 emit('notification')。这里只写日志 + 显示消息中心(如果失败)。
327
378
  if (fails.length > 0) {
328
379
  ctx.log.info("[上传操作]失败明细:" + JSON.stringify(fails));
329
380
  }
@@ -356,7 +407,9 @@ module.exports = (ctx) => {
356
407
  };
357
408
 
358
409
  // trigger delete file
359
- const onRemove = async function (files) {
410
+ // trigger delete file
411
+ // 从 PicGo GUI 2.3.0 起,remove 事件的第二个参数是 guiApi(Electron 专属)
412
+ const onRemove = async function (files, guiApi) {
360
413
  const rms = files.filter((each) => each.type === uploadedName);
361
414
  if (rms.length === 0) {
362
415
  return;
@@ -379,10 +432,13 @@ module.exports = (ctx) => {
379
432
  "[删除操作]获取 sha 失败,跳过:" + each.imgUrl + " " + err.message
380
433
  );
381
434
  fails.push(`${each.fileName || each.imgUrl}: 获取 sha 失败`);
382
- ctx.emit("notification", {
383
- title: "删除失败",
384
- body: `${each.fileName || each.imgUrl} 获取 sha 失败`,
385
- });
435
+ notify(
436
+ "fail",
437
+ ctx,
438
+ guiApi,
439
+ "删除失败",
440
+ `${each.fileName || each.imgUrl} 获取 sha 失败`
441
+ );
386
442
  continue;
387
443
  }
388
444
 
@@ -419,21 +475,28 @@ module.exports = (ctx) => {
419
475
  err.message
420
476
  );
421
477
  fails.push(`${each.fileName || each.imgUrl}: ${err.message}`);
422
- ctx.emit("notification", {
423
- title: "删除失败",
424
- body: `${each.fileName || each.imgUrl} ${err.message}`,
425
- });
478
+ notify(
479
+ "fail",
480
+ ctx,
481
+ guiApi,
482
+ "删除失败",
483
+ `${each.fileName || each.imgUrl} ${err.message}`
484
+ );
426
485
  }
427
486
  }
428
487
 
429
- // 汇总通知,避免重复打扰。
488
+ // 删除成功:用 guiApi.showNotification 通知(picgo 不自动处理此通知)
430
489
  if (successNames.length > 0) {
431
- ctx.emit("notification", {
432
- title: "删除完成",
433
- body:
434
- `成功同步删除 ${successNames.length} 个` +
435
- (fails.length > 0 ? `,失败 ${fails.length} 个` : ""),
436
- });
490
+ try {
491
+ if (guiApi && guiApi.showNotification) {
492
+ guiApi.showNotification({
493
+ title: "删除完成",
494
+ body: `成功同步删除 ${successNames.length} 个`,
495
+ });
496
+ }
497
+ } catch (e) {
498
+ log("error", "[删除操作]showNotification 失败:" + e.message);
499
+ }
437
500
  }
438
501
  if (fails.length > 0) {
439
502
  ctx.log.info("[删除操作]失败明细:" + JSON.stringify(fails));