mirror of
https://github.com/MarSeventh/CloudFlare-ImgBed.git
synced 2026-01-31 09:03:19 +08:00
支持从文件名中读取上传路径
This commit is contained in:
@@ -100,13 +100,7 @@ export async function onRequestPost(context) { // Contents of context object
|
|||||||
// 获得上传渠道
|
// 获得上传渠道
|
||||||
const urlParamUploadChannel = url.searchParams.get('uploadChannel');
|
const urlParamUploadChannel = url.searchParams.get('uploadChannel');
|
||||||
// 获取上传文件夹路径
|
// 获取上传文件夹路径
|
||||||
const uploadFolder = url.searchParams.get('uploadFolder') || '';
|
let uploadFolder = url.searchParams.get('uploadFolder') || '';
|
||||||
// 处理文件夹路径格式,确保没有开头的/
|
|
||||||
const normalizedFolder = uploadFolder
|
|
||||||
? uploadFolder.replace(/^\/+/, '') // 移除开头的/
|
|
||||||
.replace(/\/{2,}/g, '/') // 替换多个连续的/为单个/
|
|
||||||
.replace(/\/$/, '') // 移除末尾的/
|
|
||||||
: '';
|
|
||||||
|
|
||||||
let uploadChannel = 'TelegramNew';
|
let uploadChannel = 'TelegramNew';
|
||||||
switch (urlParamUploadChannel) {
|
switch (urlParamUploadChannel) {
|
||||||
@@ -142,8 +136,25 @@ export async function onRequestPost(context) { // Contents of context object
|
|||||||
const time = new Date().getTime();
|
const time = new Date().getTime();
|
||||||
const formdata = await clonedRequest.formData();
|
const formdata = await clonedRequest.formData();
|
||||||
const fileType = formdata.get('file').type;
|
const fileType = formdata.get('file').type;
|
||||||
const fileName = formdata.get('file').name;
|
let fileName = formdata.get('file').name;
|
||||||
const fileSize = (formdata.get('file').size / 1024 / 1024).toFixed(2); // 文件大小,单位MB
|
const fileSize = (formdata.get('file').size / 1024 / 1024).toFixed(2); // 文件大小,单位MB
|
||||||
|
// 检查fileType和fileName是否存在
|
||||||
|
if (fileType === null || fileType === undefined || fileName === null || fileName === undefined) {
|
||||||
|
return new Response('Error: fileType or fileName is wrong, check the integrity of this file!', { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
fileName = fileName.split('/').pop();
|
||||||
|
// 如果上传文件夹路径为空,尝试从文件名中获取
|
||||||
|
if (uploadFolder === '' || uploadFolder === null || uploadFolder === undefined) {
|
||||||
|
uploadFolder = fileName.split('/').slice(0, -1).join('/');
|
||||||
|
}
|
||||||
|
// 处理文件夹路径格式,确保没有开头的/
|
||||||
|
const normalizedFolder = uploadFolder
|
||||||
|
? uploadFolder.replace(/^\/+/, '') // 移除开头的/
|
||||||
|
.replace(/\/{2,}/g, '/') // 替换多个连续的/为单个/
|
||||||
|
.replace(/\/$/, '') // 移除末尾的/
|
||||||
|
: '';
|
||||||
|
|
||||||
const metadata = {
|
const metadata = {
|
||||||
FileName: fileName,
|
FileName: fileName,
|
||||||
FileType: fileType,
|
FileType: fileType,
|
||||||
@@ -157,11 +168,6 @@ export async function onRequestPost(context) { // Contents of context object
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 检查fileType和fileName是否存在
|
|
||||||
if (fileType === null || fileType === undefined || fileName === null || fileName === undefined) {
|
|
||||||
return new Response('Error: fileType or fileName is wrong, check the integrity of this file!', { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
let fileExt = fileName.split('.').pop(); // 文件扩展名
|
let fileExt = fileName.split('.').pop(); // 文件扩展名
|
||||||
if (!isExtValid(fileExt)) {
|
if (!isExtValid(fileExt)) {
|
||||||
// 如果文件名中没有扩展名,尝试从文件类型中获取
|
// 如果文件名中没有扩展名,尝试从文件类型中获取
|
||||||
|
|||||||
Reference in New Issue
Block a user