picgo-plugin-gitee 2.1.1 → 2.1.2

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 +1 -1
  2. package/src/index.js +97 -44
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "picgo-plugin-gitee",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "picgo uploader for gitee",
5
5
  "main": "src/index.js",
6
6
  "files": [
package/src/index.js CHANGED
@@ -22,14 +22,75 @@ module.exports = (ctx) => {
22
22
  const guiMenu = (ctx) => {
23
23
  return [
24
24
  {
25
- label: " Gitee 同步远程文件到相册",
25
+ label: "同步 Gitee 远程文件到相册",
26
26
  async handle(ctx, guiApi) {
27
- await syncRemoteToGallery(ctx, guiApi);
27
+ // picgo GUI 有时会把 async handle 里的异常静默吞掉。
28
+ // 手动加 try/catch 确保任何错误都能反馈到 UI 和日志。
29
+ const started = Date.now();
30
+ log("info", "[同步]按钮被点击");
31
+ log("info", `[同步]ctx 类型: ${typeof ctx}, guiApi 类型: ${typeof guiApi}`);
32
+ log(
33
+ "info",
34
+ `[同步]guiApi.galleryDB: ${guiApi && !!guiApi.galleryDB}, guiApi.showNotification: ${guiApi && !!guiApi.showNotification}`
35
+ );
36
+ try {
37
+ await syncRemoteToGallery(ctx, guiApi);
38
+ log("success", `[同步]完成,耗时 ${Date.now() - started}ms`);
39
+ } catch (err) {
40
+ const msg = err && err.message ? err.message : String(err);
41
+ const stack = err && err.stack ? err.stack : "";
42
+ log("error", "[同步]失败:" + msg);
43
+ if (stack) log("error", stack);
44
+ // 弹窗 + 系统通知,三重保险
45
+ if (guiApi && guiApi.showMessageBox) {
46
+ try {
47
+ guiApi.showMessageBox({
48
+ type: "error",
49
+ title: "Gitee 同步失败",
50
+ message: msg,
51
+ detail: stack ? stack.split("\n").slice(0, 3).join("\n") : "",
52
+ });
53
+ } catch (e) {
54
+ log("error", "[同步]showMessageBox 失败:" + e.message);
55
+ }
56
+ }
57
+ if (guiApi && guiApi.showNotification) {
58
+ try {
59
+ guiApi.showNotification({
60
+ title: "Gitee 同步失败",
61
+ body: msg,
62
+ });
63
+ } catch (e) {
64
+ log("error", "[同步]showNotification 失败:" + e.message);
65
+ }
66
+ }
67
+ }
28
68
  },
29
69
  },
30
70
  ];
31
71
  };
32
72
 
73
+ // 统一的日志输出,兼容 picgo-core (ctx.log.info) 和 picgo GUI (ctx.log 可能行为不同)
74
+ // 同时打 console 兜底,确保 Electron 主进程也能看到
75
+ const log = function (level, msg) {
76
+ try {
77
+ // picgo 的 ctx.log 支持 info / warn / success / error
78
+ const fn = ctx.log && (ctx.log[level] || ctx.log.info);
79
+ if (fn) fn.call(ctx.log, msg);
80
+ } catch (e) {
81
+ // 静吞
82
+ }
83
+ // 兜底:直接打到 stdout,这样无论 picgo GUI 把日志写到哪,这里都至少有一条记录
84
+ // (用户终端如果通过 npm 链接加载插件,能看到;GUI 主进程 console 也能看到)
85
+ if (typeof console !== "undefined") {
86
+ try {
87
+ console.log("[picgo-plugin-gitee]", level.toUpperCase(), msg);
88
+ } catch (e) {
89
+ // 静吞
90
+ }
91
+ }
92
+ };
93
+
33
94
  // 相册里只保留图片文件,避免把 README/配置文件等也拉进来
34
95
  const IMAGE_EXTS = new Set([
35
96
  "jpg", "jpeg", "png", "gif", "webp", "bmp", "svg",
@@ -90,66 +151,51 @@ module.exports = (ctx) => {
90
151
 
91
152
  // 把 Gitee 远程文件写入 PicGo GUI 相册。
92
153
  // 关键点:每条记录带 type="gitee",这样从相册删除时会触发 onRemove 真正删除 gitee 上的文件。
154
+ // 异常统一向上抛出,由 guiMenu.handle 里的 catch 统一弹窗。
93
155
  const syncRemoteToGallery = async function (ctx, guiApi) {
94
156
  if (!guiApi || !guiApi.galleryDB) {
95
- ctx.log.info(
96
- "[同步操作]失败当前:当前不在 PicGo GUI 环境(galleryDB 不可用)"
97
- );
98
- return;
157
+ throw new Error("当前不在 PicGo GUI 环境(galleryDB 不可用)");
99
158
  }
159
+ log("info", "[同步]开始检查配置");
100
160
  let userConfig;
101
161
  try {
102
162
  userConfig = getUserConfig();
103
163
  } catch (err) {
104
- guiApi.showNotification &&
105
- guiApi.showNotification({
106
- title: "Gitee 同步失败",
107
- body: "请先在插件设置中填写 owner / repo / token",
108
- });
109
- return;
164
+ throw new Error("请先在插件设置中填写 owner / repo / token");
110
165
  }
111
- if (
112
- !userConfig.owner ||
113
- !userConfig.repo ||
114
- !userConfig.token
115
- ) {
116
- guiApi.showNotification &&
117
- guiApi.showNotification({
118
- title: "Gitee 同步失败",
119
- body: "请先在插件设置中填写 owner / repo / token",
120
- });
121
- return;
166
+ if (!userConfig.owner || !userConfig.repo || !userConfig.token) {
167
+ throw new Error("请先在插件设置中填写 owner / repo / token");
122
168
  }
169
+ log(
170
+ "info",
171
+ `[同步]配置 OK: owner=${userConfig.owner} repo=${userConfig.repo} path=${userConfig.path || "(root)"}`
172
+ );
123
173
 
124
- guiApi.showNotification &&
174
+ if (guiApi.showNotification) {
125
175
  guiApi.showNotification({
126
176
  title: "正在同步",
127
177
  body: "正在从 Gitee 拉取远程文件...",
128
178
  });
129
-
130
- let remoteFiles;
131
- try {
132
- remoteFiles = await listAllRemoteFiles(userConfig);
133
- } catch (err) {
134
- guiApi.showNotification &&
135
- guiApi.showNotification({
136
- title: "Gitee 同步失败",
137
- body: err.message,
138
- });
139
- return;
140
179
  }
141
180
 
181
+ log("info", "[同步]开始递归拉取 Gitee 文件列表");
182
+ const remoteFiles = await listAllRemoteFiles(userConfig);
183
+ log("info", `[同步]仓库共 ${remoteFiles.length} 个图片文件`);
184
+
142
185
  if (remoteFiles.length === 0) {
143
- guiApi.showNotification &&
186
+ log("warn", "[同步]仓库里没有任何图片文件");
187
+ if (guiApi.showNotification) {
144
188
  guiApi.showNotification({
145
189
  title: "Gitee 同步完成",
146
- body: "仓库里没有任何文件",
190
+ body: "仓库里没有任何图片文件",
147
191
  });
192
+ }
148
193
  return;
149
194
  }
150
195
 
151
- // 相册里已有的就不重复插入
196
+ log("info", "[同步]读取本地相册");
152
197
  const existing = (await guiApi.galleryDB.get()) || [];
198
+ log("info", `[同步]本地相册已有 ${existing.length} 条`);
153
199
  const existingUrls = new Set(existing.map((x) => x.imgUrl));
154
200
  const newItems = remoteFiles
155
201
  .filter((f) => !existingUrls.has(f.imgUrl))
@@ -158,28 +204,35 @@ module.exports = (ctx) => {
158
204
  imgUrl: f.imgUrl,
159
205
  type: uploadedName,
160
206
  sha: f.sha,
161
- // picgo 相册的可选字段
162
207
  extname: f.fileName.split(".").pop(),
163
208
  }));
209
+ log(
210
+ "info",
211
+ `[同步]需要新增 ${newItems.length} 条(已存在 ${remoteFiles.length - newItems.length} 条)`
212
+ );
164
213
 
165
214
  if (newItems.length === 0) {
166
- guiApi.showNotification &&
215
+ if (guiApi.showNotification) {
167
216
  guiApi.showNotification({
168
217
  title: "Gitee 同步完成",
169
218
  body: "没有新增文件,相册已是最新",
170
219
  });
220
+ }
171
221
  return;
172
222
  }
173
223
 
224
+ log("info", "[同步]写入相册 insertMany");
174
225
  await guiApi.galleryDB.insertMany(newItems);
175
- guiApi.showNotification &&
226
+ log(
227
+ "success",
228
+ `[同步]完成:共 ${remoteFiles.length} 个,新增 ${newItems.length} 个`
229
+ );
230
+ if (guiApi.showNotification) {
176
231
  guiApi.showNotification({
177
232
  title: "Gitee 同步完成",
178
233
  body: `新增 ${newItems.length} 个文件到相册(共发现 ${remoteFiles.length} 个)`,
179
234
  });
180
- ctx.log.info(
181
- `[同步操作]完成:共 ${remoteFiles.length} 个,新增 ${newItems.length} 个`
182
- );
235
+ }
183
236
  };
184
237
 
185
238
  const getHeaders = function () {