Skip to content

Commit

Permalink
sync(service): 根据setting配置项决定是否需要登录上传针对直接调用api
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Dec 19, 2024
1 parent 452a640 commit 8bfe99c
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions service/src/controllers/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,45 @@ const ImageUtils = {
}
};

/**
* 检查上传是否需要认证
*/
async function checkUploadAuthRequired(env) {
try {
const setting = await env.CYI_IMGDB
.prepare('SELECT value FROM settings WHERE key = ?')
.bind('upload_require_auth')
.first();

// 直接返回布尔值
return setting?.value === true;
} catch (error) {
console.error('Error checking upload auth setting:', error);
// 默认需要认证,确保安全
return true;
}
}

/**
* 处理图片上传
* 支持多图上传,区分已登录用户和公共上传
* 支持多图上传,区分已登录用户和公共上传 根据配置决定是否需要登录
*/
export async function handleImageUpload(request, env) {
console.log('handleImageUpload called');

// 检查是否需要认证
const requireAuth = await checkUploadAuthRequired(env);

// 尝试认证用户
let user = null;
try {
user = await authenticate(request, env);
console.log('Authenticated user:', user);
} catch (authError) {
console.log('Unauthenticated upload, treating as public');
if (requireAuth) {
throw new ValidationError('当前配置要求登录后才能上传图片');
}
console.log('Unauthenticated upload, treating as public or allowed by configuration');
}

await ImageUtils.validateContentType(request, 'multipart/form-data');
Expand Down Expand Up @@ -311,17 +336,24 @@ export async function handleGetImage(request, env) {
}

/**
* 处理图片链接上传
* 处理图片链接上传 根据配置决定是否需要登录
*/
export async function handleImageLinkUpload(request, env) {
console.log('handleImageLinkUpload called');

// 检查是否需要认证
const requireAuth = await checkUploadAuthRequired(env);

// 尝试认证用户
let user = null;
try {
user = await authenticate(request, env);
console.log('Authenticated user:', user);
} catch (authError) {
console.log('Unauthenticated upload, treating as public');
if (requireAuth) {
throw new ValidationError('当前配置要求登录后才能上传图片');
}
console.log('Unauthenticated upload, treating as public or allowed by configuration');
}

await ImageUtils.validateContentType(request, 'application/json');
Expand Down

0 comments on commit 8bfe99c

Please sign in to comment.