@@ -127,18 +127,26 @@ $(document).ready(function() {
127
127
}).on('filebatchselected', handleFileSelection)
128
128
.on('fileclear', handleFileClear);
129
129
}
130
+
131
+ // 配置接口信息
132
+ const interfaceConfig = {
133
+ tg: {
134
+ acceptTypes: 'image/gif,image/jpeg,image/jpg,image/png,video/mp4',
135
+ gifAndVideoMaxSize: 5 * 1024 * 1024, // GIF 和视频文件的最大大小为 5MB
136
+ otherMaxSize: 5 * 1024 * 1024, // 非 GIF 和视频文件的最大大小为 5MB
137
+ compressImage: true
138
+ },
139
+ // 添加其他接口的配置信息
140
+ };
130
141
131
142
// 处理接口选择器变更事件
132
143
$('#interfaceSelector').change(function() {
133
144
const selectedInterface = $(this).val();
134
- let acceptTypes = '';
135
-
136
- switch (selectedInterface) {
137
- case 'tg':
138
- acceptTypes = 'image/gif,image/jpeg,image/jpg,image/png,video/mp4';
139
- break;
145
+ const interfaceInfo = interfaceConfig[selectedInterface];
146
+
147
+ if (interfaceInfo) {
148
+ $('#fileInput').attr('accept', interfaceInfo.acceptTypes);
140
149
}
141
- $('#fileInput').attr('accept', acceptTypes);
142
150
}).trigger('change');
143
151
144
152
// 处理文件选择事件
@@ -150,36 +158,34 @@ $(document).ready(function() {
150
158
}
151
159
}
152
160
153
- // 监听粘贴事件
154
- $(document).on('paste', function(event) {
155
- // 获取粘贴板中的内容
156
- const clipboardData = event.originalEvent.clipboardData;
157
- if (clipboardData && clipboardData.items) {
158
- // 遍历粘贴板中的项
159
- for (let i = 0; i < clipboardData.items.length; i++) {
160
- const item = clipboardData.items[i];
161
- // 如果是文件类型
162
- if (item.kind === 'file') {
163
- const pasteFile = item.getAsFile();
164
- // 上传粘贴的文件
165
- uploadFile(pasteFile);
166
- break; // 处理完第一个文件即可
167
- }
168
- }
169
- }
170
- });
171
-
172
161
// 处理上传文件函数
173
162
async function uploadFile(file) {
174
163
try {
175
- if (file.type === 'image/gif' || file.type === 'video/mp4') {
176
- if (file.size > 5 * 1024 * 1024) {
177
- toastr.error('文件必须≤5MB');
164
+ const selectedInterface = $('#interfaceSelector').val();
165
+ const interfaceInfo = interfaceConfig[selectedInterface];
166
+
167
+ if (!interfaceInfo) {
168
+ console.error('未找到接口配置信息');
169
+ return;
170
+ }
171
+
172
+ if (['image/gif', 'video/mp4'].includes(file.type)) {
173
+ if (file.size > interfaceInfo.gifAndVideoMaxSize) {
174
+ toastr.error('文件必须≤' + interfaceInfo.gifAndVideoMaxSize / (1024 * 1024) + 'MB');
178
175
return;
179
176
}
177
+ // 不压缩,直接上传原文件
180
178
} else {
181
- const compressedFile = await compressImage(file);
182
- file = compressedFile; // 如果不是GIF或视频,使用压缩后的文件
179
+ if (interfaceInfo.compressImage === true) {
180
+ const compressedFile = await compressImage(file);
181
+ file = compressedFile;
182
+ } else if (interfaceInfo.compressImage === false) {
183
+ if (file.size > interfaceInfo.otherMaxSize) {
184
+ toastr.error('文件必须≤' + interfaceInfo.otherMaxSize / (1024 * 1024) + 'MB');
185
+ return;
186
+ }
187
+ // 不压缩,直接上传原文件
188
+ }
183
189
}
184
190
185
191
$('#uploadingText').show();
@@ -196,8 +202,37 @@ $(document).ready(function() {
196
202
} finally {
197
203
$('#uploadingText').hide();
198
204
}
199
- }
200
-
205
+ }
206
+
207
+ // 处理上传响应函数
208
+ async function handleUploadResponse(response) {
209
+ if (response.ok) {
210
+ const result = await response.json();
211
+ return result.data;
212
+ } else {
213
+ return '文件上传失败!';
214
+ }
215
+ }
216
+
217
+ // 监听粘贴事件
218
+ $(document).on('paste', function(event) {
219
+ // 获取粘贴板中的内容
220
+ const clipboardData = event.originalEvent.clipboardData;
221
+ if (clipboardData && clipboardData.items) {
222
+ // 遍历粘贴板中的项
223
+ for (let i = 0; i < clipboardData.items.length; i++) {
224
+ const item = clipboardData.items[i];
225
+ // 如果是文件类型
226
+ if (item.kind === 'file') {
227
+ const pasteFile = item.getAsFile();
228
+ // 上传粘贴的文件
229
+ uploadFile(pasteFile);
230
+ break; // 处理完第一个文件即可
231
+ }
232
+ }
233
+ }
234
+ });
235
+
201
236
//处理图片压缩事件
202
237
async function compressImage(file, quality = 0.5, maxResolution = 20000000) {
203
238
$('#compressingText').show();
@@ -233,16 +268,6 @@ $(document).ready(function() {
233
268
});
234
269
}
235
270
236
- // 处理上传响应函数
237
- async function handleUploadResponse(response) {
238
- if (response.ok) {
239
- const result = await response.json();
240
- return result.data;
241
- } else {
242
- return '文件上传失败!';
243
- }
244
- }
245
-
246
271
// 处理按钮点击事件
247
272
$('#urlBtn, #bbcodeBtn, #markdownBtn').on('click', function() {
248
273
const fileLink = originalImageURL.trim();
0 commit comments