Skip to content

Commit 71f4356

Browse files
committed
0.17.1
Enhanced the DLink upload logic to intelligently handle unsupported file extensions by renaming them to supported formats (e.g., .png for images). Added automatic retry with fallback extensions if the initial upload returns a 403 error, improving compatibility and reliability of file uploads.
1 parent d50e907 commit 71f4356

File tree

2 files changed

+91
-27
lines changed

2 files changed

+91
-27
lines changed

cloudflare-worker-js-api/API_IMG_dlink.js

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,56 @@ async function handleDlinkRequest(request) {
2525

2626
console.log(`File received: ${imageFile.name}, size: ${imageFile.size}, type: ${imageFile.type}`);
2727

28-
// 使用上传的文件名
29-
const fileName = imageFile.name;
28+
// 智能后缀处理
29+
let fileName = imageFile.name;
30+
const nameParts = fileName.split('.');
31+
const ext = nameParts.length > 1 ? nameParts.pop().toLowerCase() : '';
32+
const baseName = nameParts.join('.');
33+
34+
// 已知支持的格式(不修改)
35+
const supportedFormats = ['jpg', 'jpeg', 'png', 'webp', 'mp4'];
36+
37+
// 已知不支持的格式 -> 替换为 png
38+
const unsupportedFormats = ['heic', 'avif', 'heif'];
39+
40+
if (unsupportedFormats.includes(ext)) {
41+
// 明确不支持的格式,替换为 png
42+
fileName = `${baseName}.png`;
43+
console.log(`Format ${ext} not supported, renamed to: ${fileName}`);
44+
}
45+
// 已知支持的格式和其他未知格式,保持不变(由 403 重试机制处理)
3046

3147
// 读取文件数据
3248
const imageData = await imageFile.arrayBuffer();
3349

34-
// 目标上传 URL
35-
const uploadUrl = `https://www.dlink666.com/api/upload?name=${encodeURIComponent(fileName)}`;
36-
37-
// 发送 PUT 请求到 DLink 上传接口
38-
const response = await fetch(uploadUrl, {
39-
method: 'PUT',
40-
headers: {
41-
'Content-Type': 'application/x-www-form-urlencoded',
42-
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
43-
},
44-
body: imageData,
45-
});
50+
// 上传函数(支持重试)
51+
const uploadToServer = async (name) => {
52+
const uploadUrl = `https://www.dlink666.com/api/upload?name=${encodeURIComponent(name)}`;
53+
return await fetch(uploadUrl, {
54+
method: 'PUT',
55+
headers: {
56+
'Content-Type': 'application/x-www-form-urlencoded',
57+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
58+
},
59+
body: imageData,
60+
});
61+
};
62+
63+
// 首次上传尝试
64+
let response = await uploadToServer(fileName);
65+
66+
// 如果返回 403,自动更换后缀重试
67+
if (response.status === 403) {
68+
console.log(`Upload with ${fileName} got 403, retrying with fallback extension...`);
69+
70+
// 判断文件类型,选择回退后缀
71+
const isImageType = imageFile.type && imageFile.type.startsWith('image/');
72+
const fallbackExt = isImageType ? 'png' : 'mp4';
73+
const fallbackName = `${baseName}.${fallbackExt}`;
74+
75+
console.log(`Retrying with: ${fallbackName}`);
76+
response = await uploadToServer(fallbackName);
77+
}
4678

4779
// 处理响应
4880
if (response.ok) {

cloudflare-worker-js-api/worker.js

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -910,24 +910,56 @@ async function handleDlinkRequest(request) {
910910

911911
console.log(`File received: ${imageFile.name}, size: ${imageFile.size}, type: ${imageFile.type}`);
912912

913-
// 使用上传的文件名
914-
const fileName = imageFile.name;
913+
// 智能后缀处理
914+
let fileName = imageFile.name;
915+
const nameParts = fileName.split('.');
916+
const ext = nameParts.length > 1 ? nameParts.pop().toLowerCase() : '';
917+
const baseName = nameParts.join('.');
918+
919+
// 已知支持的格式(不修改)
920+
const supportedFormats = ['jpg', 'jpeg', 'png', 'webp', 'mp4'];
921+
922+
// 已知不支持的格式 -> 替换为 png
923+
const unsupportedFormats = ['heic', 'avif', 'heif'];
924+
925+
if (unsupportedFormats.includes(ext)) {
926+
// 明确不支持的格式,替换为 png
927+
fileName = `${baseName}.png`;
928+
console.log(`Format ${ext} not supported, renamed to: ${fileName}`);
929+
}
930+
// 已知支持的格式和其他未知格式,保持不变(由 403 重试机制处理)
915931

916932
// 读取文件数据
917933
const imageData = await imageFile.arrayBuffer();
918934

919-
// 目标上传 URL
920-
const uploadUrl = `https://www.dlink666.com/api/upload?name=${encodeURIComponent(fileName)}`;
935+
// 上传函数(支持重试)
936+
const uploadToServer = async (name) => {
937+
const uploadUrl = `https://www.dlink666.com/api/upload?name=${encodeURIComponent(name)}`;
938+
return await fetch(uploadUrl, {
939+
method: 'PUT',
940+
headers: {
941+
'Content-Type': 'application/x-www-form-urlencoded',
942+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
943+
},
944+
body: imageData,
945+
});
946+
};
921947

922-
// 发送 PUT 请求到 DLink 上传接口
923-
const response = await fetch(uploadUrl, {
924-
method: 'PUT',
925-
headers: {
926-
'Content-Type': 'application/x-www-form-urlencoded',
927-
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
928-
},
929-
body: imageData,
930-
});
948+
// 首次上传尝试
949+
let response = await uploadToServer(fileName);
950+
951+
// 如果返回 403,自动更换后缀重试
952+
if (response.status === 403) {
953+
console.log(`Upload with ${fileName} got 403, retrying with fallback extension...`);
954+
955+
// 判断文件类型,选择回退后缀
956+
const isImageType = imageFile.type && imageFile.type.startsWith('image/');
957+
const fallbackExt = isImageType ? 'png' : 'mp4';
958+
const fallbackName = `${baseName}.${fallbackExt}`;
959+
960+
console.log(`Retrying with: ${fallbackName}`);
961+
response = await uploadToServer(fallbackName);
962+
}
931963

932964
// 处理响应
933965
if (response.ok) {

0 commit comments

Comments
 (0)