picgo-plugin-gitee 2.1.0 → 2.1.1
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.
- package/package.json +1 -1
- package/readme.md +1 -0
- package/src/index.js +165 -0
package/package.json
CHANGED
package/readme.md
CHANGED
package/src/index.js
CHANGED
|
@@ -18,6 +18,170 @@ module.exports = (ctx) => {
|
|
|
18
18
|
ctx.on("remove", onRemove);
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
+
// GUI 菜单项:手动触发"从 Gitee 拉取远程文件到相册"
|
|
22
|
+
const guiMenu = (ctx) => {
|
|
23
|
+
return [
|
|
24
|
+
{
|
|
25
|
+
label: "从 Gitee 同步远程文件到相册",
|
|
26
|
+
async handle(ctx, guiApi) {
|
|
27
|
+
await syncRemoteToGallery(ctx, guiApi);
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// 相册里只保留图片文件,避免把 README/配置文件等也拉进来
|
|
34
|
+
const IMAGE_EXTS = new Set([
|
|
35
|
+
"jpg", "jpeg", "png", "gif", "webp", "bmp", "svg",
|
|
36
|
+
"tif", "tiff", "ico", "avif", "heic",
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
// 递归列出 Gitee 仓库所有图片文件(含子目录)。
|
|
40
|
+
// 返回 [{ fileName, imgUrl, sha, size }]
|
|
41
|
+
const listAllRemoteFiles = async function (userConfig) {
|
|
42
|
+
const headers = getHeaders();
|
|
43
|
+
const listUrl =
|
|
44
|
+
userConfig.baseUrl + "/contents" + formatConfigPath(userConfig);
|
|
45
|
+
const out = [];
|
|
46
|
+
await walkContents(listUrl, userConfig, headers, out);
|
|
47
|
+
return out
|
|
48
|
+
.filter((it) => {
|
|
49
|
+
const ext = (it.name || "").split(".").pop().toLowerCase();
|
|
50
|
+
return IMAGE_EXTS.has(ext);
|
|
51
|
+
})
|
|
52
|
+
.map((it) => ({
|
|
53
|
+
fileName: it.path,
|
|
54
|
+
imgUrl: userConfig.previewUrl + "/" + it.path,
|
|
55
|
+
sha: it.sha,
|
|
56
|
+
size: it.size,
|
|
57
|
+
}));
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const walkContents = async function (url, userConfig, headers, out) {
|
|
61
|
+
const fullUrl = url + "?access_token=" + userConfig.token;
|
|
62
|
+
let res;
|
|
63
|
+
try {
|
|
64
|
+
res = await ctx.Request.request({
|
|
65
|
+
method: "GET",
|
|
66
|
+
url: fullUrl,
|
|
67
|
+
headers: headers,
|
|
68
|
+
});
|
|
69
|
+
} catch (err) {
|
|
70
|
+
throw new Error("获取 Gitee 目录失败:" + err.message);
|
|
71
|
+
}
|
|
72
|
+
// 兼容 picgo-core(字符串)和 picgo GUI(对象)
|
|
73
|
+
const items = typeof res === "string" ? JSON.parse(res) : res;
|
|
74
|
+
if (!Array.isArray(items)) {
|
|
75
|
+
// 单文件(理论上 listAllRemoteFiles 不会传单文件路径,兜底)
|
|
76
|
+
if (items && items.type === "file") {
|
|
77
|
+
out.push(items);
|
|
78
|
+
}
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
for (const item of items) {
|
|
82
|
+
if (item.type === "file") {
|
|
83
|
+
out.push(item);
|
|
84
|
+
} else if (item.type === "dir") {
|
|
85
|
+
// 递归子目录
|
|
86
|
+
await walkContents(url + "/" + item.path, userConfig, headers, out);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// 把 Gitee 远程文件写入 PicGo GUI 相册。
|
|
92
|
+
// 关键点:每条记录带 type="gitee",这样从相册删除时会触发 onRemove 真正删除 gitee 上的文件。
|
|
93
|
+
const syncRemoteToGallery = async function (ctx, guiApi) {
|
|
94
|
+
if (!guiApi || !guiApi.galleryDB) {
|
|
95
|
+
ctx.log.info(
|
|
96
|
+
"[同步操作]失败当前:当前不在 PicGo GUI 环境(galleryDB 不可用)"
|
|
97
|
+
);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
let userConfig;
|
|
101
|
+
try {
|
|
102
|
+
userConfig = getUserConfig();
|
|
103
|
+
} catch (err) {
|
|
104
|
+
guiApi.showNotification &&
|
|
105
|
+
guiApi.showNotification({
|
|
106
|
+
title: "Gitee 同步失败",
|
|
107
|
+
body: "请先在插件设置中填写 owner / repo / token",
|
|
108
|
+
});
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
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;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
guiApi.showNotification &&
|
|
125
|
+
guiApi.showNotification({
|
|
126
|
+
title: "正在同步",
|
|
127
|
+
body: "正在从 Gitee 拉取远程文件...",
|
|
128
|
+
});
|
|
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
|
+
}
|
|
141
|
+
|
|
142
|
+
if (remoteFiles.length === 0) {
|
|
143
|
+
guiApi.showNotification &&
|
|
144
|
+
guiApi.showNotification({
|
|
145
|
+
title: "Gitee 同步完成",
|
|
146
|
+
body: "仓库里没有任何文件",
|
|
147
|
+
});
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 相册里已有的就不重复插入
|
|
152
|
+
const existing = (await guiApi.galleryDB.get()) || [];
|
|
153
|
+
const existingUrls = new Set(existing.map((x) => x.imgUrl));
|
|
154
|
+
const newItems = remoteFiles
|
|
155
|
+
.filter((f) => !existingUrls.has(f.imgUrl))
|
|
156
|
+
.map((f) => ({
|
|
157
|
+
fileName: f.fileName,
|
|
158
|
+
imgUrl: f.imgUrl,
|
|
159
|
+
type: uploadedName,
|
|
160
|
+
sha: f.sha,
|
|
161
|
+
// picgo 相册的可选字段
|
|
162
|
+
extname: f.fileName.split(".").pop(),
|
|
163
|
+
}));
|
|
164
|
+
|
|
165
|
+
if (newItems.length === 0) {
|
|
166
|
+
guiApi.showNotification &&
|
|
167
|
+
guiApi.showNotification({
|
|
168
|
+
title: "Gitee 同步完成",
|
|
169
|
+
body: "没有新增文件,相册已是最新",
|
|
170
|
+
});
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
await guiApi.galleryDB.insertMany(newItems);
|
|
175
|
+
guiApi.showNotification &&
|
|
176
|
+
guiApi.showNotification({
|
|
177
|
+
title: "Gitee 同步完成",
|
|
178
|
+
body: `新增 ${newItems.length} 个文件到相册(共发现 ${remoteFiles.length} 个)`,
|
|
179
|
+
});
|
|
180
|
+
ctx.log.info(
|
|
181
|
+
`[同步操作]完成:共 ${remoteFiles.length} 个,新增 ${newItems.length} 个`
|
|
182
|
+
);
|
|
183
|
+
};
|
|
184
|
+
|
|
21
185
|
const getHeaders = function () {
|
|
22
186
|
return {
|
|
23
187
|
"Content-Type": "application/json;charset=UTF-8",
|
|
@@ -327,5 +491,6 @@ module.exports = (ctx) => {
|
|
|
327
491
|
return {
|
|
328
492
|
uploader: "gitee",
|
|
329
493
|
register,
|
|
494
|
+
guiMenu,
|
|
330
495
|
};
|
|
331
496
|
};
|