//没有固定的文档 //接口是通过查看huggingface.co的请求来实现的 //有个huggingface hub api //https://huggingface.co/docs/huggingface.js/hub/README //https://github.com/huggingface/huggingface.js/tree/main/packages/hub //https://github.com/huggingface/huggingface.js/blob/main/packages/hub/src/lib/list-files.ts //https://github.com/huggingface/huggingface.js/blob/main/packages/hub/src/lib/download-file.ts //https://github.com/huggingface/huggingface.js/blob/main/packages/hub/src/lib/commit.ts //https://github.com/huggingface/huggingface.js/blob/main/packages/hub/src/lib/delete-files.ts export const onRequest = async (context: RouteContext): Promise => { const request = context.request; const env = context.env as Env; // 从 Authorization header 中获取 token 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'); // 操作类型: raw, upload, tree, delete 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' } }); } // Hugging Face API 基础 URL const hfApiBaseUrl = 'https://huggingface.co/api/datasets'; // 处理 GET 请求 - 获取文件内容或列出文件 if (operation === 'raw' && request.method === 'GET') { const path = pathParts.length > 6 ? pathParts.slice(6).join('/') : ''; //这里没有错..getraw就是没有api 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('/') : ''; // 4. 列出文件 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' } }); } // 处理 POST 请求 - 上传文件 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' } }); } };