koishi-plugin-custom-image 0.1.7 → 0.1.8
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/lib/index.js +5 -5
- package/package.json +1 -1
- package/src/index.ts +5 -5
package/lib/index.js
CHANGED
|
@@ -67,7 +67,7 @@ exports.Config = koishi_1.Schema.object({
|
|
|
67
67
|
.description('【格式设置】图片发送前的提示文本'),
|
|
68
68
|
tempDir: koishi_1.Schema.string()
|
|
69
69
|
.default(path_1.default.join(os_1.default.tmpdir(), 'koishi-custom-image'))
|
|
70
|
-
.description('
|
|
70
|
+
.description('【文件设置】临时图片保存目录(需机器人能访问)')
|
|
71
71
|
});
|
|
72
72
|
const processedApi = new Map();
|
|
73
73
|
const imageBuffer = new Map();
|
|
@@ -103,10 +103,10 @@ function createTempDir(config) {
|
|
|
103
103
|
async function saveImageToTemp(buffer, config) {
|
|
104
104
|
createTempDir(config);
|
|
105
105
|
const filename = `${crypto_1.default.randomUUID()}.jpg`;
|
|
106
|
-
const filepath = path_1.default.
|
|
106
|
+
const filepath = path_1.default.resolve(config.tempDir, filename);
|
|
107
107
|
await fs_1.default.promises.writeFile(filepath, buffer);
|
|
108
108
|
tempFiles.add(filepath);
|
|
109
|
-
return filepath
|
|
109
|
+
return `file://${filepath.replace(/\\/g, '/')}`;
|
|
110
110
|
}
|
|
111
111
|
function isImageUrl(url) {
|
|
112
112
|
return /^https?:\/\/.+\.(jpg|jpeg|png|gif|webp|bmp)/i.test(url);
|
|
@@ -125,8 +125,8 @@ async function fetchImage(url, config) {
|
|
|
125
125
|
}
|
|
126
126
|
const res = await http.get(url, { responseType: 'arraybuffer' });
|
|
127
127
|
if (res.status === 200 && res.data) {
|
|
128
|
-
const
|
|
129
|
-
return { success: true, type: 'file', data:
|
|
128
|
+
const fileUrl = await saveImageToTemp(Buffer.from(res.data), config);
|
|
129
|
+
return { success: true, type: 'file', data: fileUrl };
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
catch {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -81,7 +81,7 @@ export const Config: Schema<Config> = Schema.object({
|
|
|
81
81
|
.description('【格式设置】图片发送前的提示文本'),
|
|
82
82
|
tempDir: Schema.string()
|
|
83
83
|
.default(path.join(os.tmpdir(), 'koishi-custom-image'))
|
|
84
|
-
.description('
|
|
84
|
+
.description('【文件设置】临时图片保存目录(需机器人能访问)')
|
|
85
85
|
})
|
|
86
86
|
|
|
87
87
|
const processedApi = new Map<string, number>()
|
|
@@ -122,10 +122,10 @@ function createTempDir(config: Config) {
|
|
|
122
122
|
async function saveImageToTemp(buffer: Buffer, config: Config) {
|
|
123
123
|
createTempDir(config)
|
|
124
124
|
const filename = `${crypto.randomUUID()}.jpg`
|
|
125
|
-
const filepath = path.
|
|
125
|
+
const filepath = path.resolve(config.tempDir, filename)
|
|
126
126
|
await fs.promises.writeFile(filepath, buffer)
|
|
127
127
|
tempFiles.add(filepath)
|
|
128
|
-
return filepath
|
|
128
|
+
return `file://${filepath.replace(/\\/g, '/')}`
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
function isImageUrl(url: string) {
|
|
@@ -148,8 +148,8 @@ async function fetchImage(url: string, config: Config) {
|
|
|
148
148
|
|
|
149
149
|
const res = await http.get(url, { responseType: 'arraybuffer' })
|
|
150
150
|
if (res.status === 200 && res.data) {
|
|
151
|
-
const
|
|
152
|
-
return { success: true, type: 'file', data:
|
|
151
|
+
const fileUrl = await saveImageToTemp(Buffer.from(res.data), config)
|
|
152
|
+
return { success: true, type: 'file', data: fileUrl }
|
|
153
153
|
}
|
|
154
154
|
} catch {
|
|
155
155
|
if (retry === config.retryTimes) return { success: false, type: '', data: '' }
|