Skip to content

Commit 2560ede

Browse files
authored
Update worker.js
1 parent c09225c commit 2560ede

File tree

1 file changed

+68
-43
lines changed

1 file changed

+68
-43
lines changed

worker.js

+68-43
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,26 @@ $(document).ready(function() {
127127
}).on('filebatchselected', handleFileSelection)
128128
.on('fileclear', handleFileClear);
129129
}
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+
};
130141
131142
// 处理接口选择器变更事件
132143
$('#interfaceSelector').change(function() {
133144
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);
140149
}
141-
$('#fileInput').attr('accept', acceptTypes);
142150
}).trigger('change');
143151
144152
// 处理文件选择事件
@@ -150,36 +158,34 @@ $(document).ready(function() {
150158
}
151159
}
152160
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-
172161
// 处理上传文件函数
173162
async function uploadFile(file) {
174163
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');
178175
return;
179176
}
177+
// 不压缩,直接上传原文件
180178
} 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+
}
183189
}
184190
185191
$('#uploadingText').show();
@@ -196,8 +202,37 @@ $(document).ready(function() {
196202
} finally {
197203
$('#uploadingText').hide();
198204
}
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+
201236
//处理图片压缩事件
202237
async function compressImage(file, quality = 0.5, maxResolution = 20000000) {
203238
$('#compressingText').show();
@@ -233,16 +268,6 @@ $(document).ready(function() {
233268
});
234269
}
235270
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-
246271
// 处理按钮点击事件
247272
$('#urlBtn, #bbcodeBtn, #markdownBtn').on('click', function() {
248273
const fileLink = originalImageURL.trim();

0 commit comments

Comments
 (0)