|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const onRequest = async (context: RouteContext): Promise<Response> => { |
|
const request = context.request; |
|
const env = context.env as Env; |
|
|
|
|
|
const authHeader = request.headers.get('Authorization'); |
|
if (!authHeader || !authHeader.startsWith('Bearer ')) { |
|
return new Response(JSON.stringify({ error: '未提供有效的授权令牌' }), { |
|
status: 401, |
|
headers: { 'Content-Type': 'application/json' } |
|
}); |
|
} |
|
const hfToken = authHeader.replace('Bearer ', ''); |
|
|
|
console.log('Request URL:', request.url); |
|
try { |
|
const url = new URL(request.url); |
|
const pathParts = url.pathname.split('/').filter(Boolean); |
|
|
|
|
|
const owner = pathParts[2] || url.searchParams.get('owner'); |
|
const repo = pathParts[3] || url.searchParams.get('repo'); |
|
const operation = pathParts[4] || url.searchParams.get('op'); |
|
const ref = pathParts[5] || url.searchParams.get('ref') || 'main'; |
|
|
|
if (!owner || !repo) { |
|
return new Response(JSON.stringify({ error: '缺少仓库所有者或仓库名称' }), { |
|
status: 400, |
|
headers: { 'Content-Type': 'application/json' } |
|
}); |
|
} |
|
|
|
const hfApiBaseUrl = 'https://huggingface.co/api/datasets'; |
|
|
|
|
|
|
|
|
|
if (operation === 'raw' && request.method === 'GET') { |
|
const path = pathParts.length > 6 ? pathParts.slice(6).join('/') : ''; |
|
|
|
const hfUrl = `https://huggingface.co/datasets/${owner}/${repo}/raw/${ref}/${path}`; |
|
|
|
const response = await fetch(hfUrl, { |
|
headers: { |
|
'Authorization': `Bearer ${hfToken}`, |
|
'User-Agent': 'Cloudflare-Worker' |
|
} |
|
}); |
|
|
|
return new Response(await response.text(), { |
|
status: response.status, |
|
headers: response.headers |
|
}); |
|
} |
|
|
|
if (operation === 'tree' && request.method === 'GET') { |
|
const path = pathParts.length > 6 ? pathParts.slice(6).join('/') : ''; |
|
|
|
const hfUrl = `${hfApiBaseUrl}/${owner}/${repo}/tree/${ref}/${path}`; |
|
|
|
const response = await fetch(hfUrl, { |
|
headers: { |
|
'Authorization': `Bearer ${hfToken}`, |
|
'User-Agent': 'Cloudflare-Worker' |
|
} |
|
}); |
|
|
|
const data = await response.json(); |
|
return new Response(JSON.stringify(data), { |
|
status: response.status, |
|
headers: { 'Content-Type': 'application/json' } |
|
}); |
|
} |
|
|
|
|
|
if (operation === 'commit' && (request.method === 'POST' || request.method === 'PUT' || request.method === 'DELETE')) { |
|
const hfUrl = `${hfApiBaseUrl}/${owner}/${repo}/commit/${ref}`; |
|
const body = await request.json(); |
|
const response = await fetch(hfUrl, { |
|
method: 'POST', |
|
headers: { |
|
'Authorization': `Bearer ${hfToken}`, |
|
'Content-Type': 'application/json', |
|
'User-Agent': 'Cloudflare-Worker' |
|
}, |
|
body: JSON.stringify(body) |
|
}); |
|
|
|
const data = await response.json(); |
|
return new Response(JSON.stringify(data), { |
|
status: response.status, |
|
headers: { 'Content-Type': 'application/json' } |
|
}); |
|
} |
|
|
|
|
|
|
|
return new Response(JSON.stringify({ error: '不支持的请求方法或路径' }), { |
|
status: 400, |
|
headers: { 'Content-Type': 'application/json' } |
|
}); |
|
} catch (error: any) { |
|
console.error('Error:', error); |
|
return new Response(JSON.stringify({ error: '服务器内部错误', details: error.message }), { |
|
status: 500, |
|
headers: { 'Content-Type': 'application/json' } |
|
}); |
|
} |
|
}; |
|
|