Skip to content

Commit 1ca7a28

Browse files
committed
Fix:HuggingFace渠道文件命名和读取问题
1 parent d629c5c commit 1ca7a28

26 files changed

Lines changed: 32 additions & 28 deletions
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/api/huggingface/commitUpload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function onRequestPost(context) {
2222
}
2323

2424
const body = await request.json();
25-
const { fullId, filePath, sha256, fileSize, fileName, channelName, multipartParts } = body;
25+
const { fullId, filePath, sha256, fileSize, fileName, fileType, channelName, multipartParts } = body;
2626

2727
if (!fullId || !filePath || !sha256 || !fileSize) {
2828
return new Response(JSON.stringify({
@@ -86,6 +86,7 @@ export async function onRequestPost(context) {
8686
// 构建 metadata
8787
const metadata = {
8888
FileName: fileName || fullId,
89+
FileType: fileType || null,
8990
Channel: "HuggingFace",
9091
ChannelName: hfChannel.name || "HuggingFace_env",
9192
FileSize: (fileSize / 1024 / 1024).toFixed(2),

functions/api/huggingface/getUploadUrl.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
/**
22
* HuggingFace 大文件直传 API
3-
*
3+
*
44
* 流程:
55
* 1. 前端计算 SHA256 和文件样本
66
* 2. 前端调用此 API 获取 LFS 上传 URL
77
* 3. 前端直接上传到 HuggingFace S3
88
* 4. 前端调用 commitUpload API 提交文件引用
9-
*
9+
*
1010
* 这样可以绕过 CF Workers 的 100MB 请求体限制和 CPU 时间限制
1111
*/
1212

1313
import { HuggingFaceAPI } from '../../utils/huggingfaceAPI.js';
1414
import { fetchUploadConfig } from '../../utils/sysConfig.js';
1515
import { userAuthCheck, UnauthorizedResponse } from '../../utils/userAuth.js';
16+
import { buildUniqueFileId } from '../../upload/uploadTools.js';
1617

1718
export async function onRequestPost(context) {
1819
const { request, env } = context;
1920
const url = new URL(request.url);
21+
context.url = url; // 将 url 添加到 context 以便 buildUniqueFileId 使用
2022

2123
try {
2224
// 鉴权
@@ -26,7 +28,7 @@ export async function onRequestPost(context) {
2628
}
2729

2830
const body = await request.json();
29-
const { fileSize, fileName, sha256, fileSample, channelName } = body;
31+
const { fileSize, fileName, fileType, sha256, fileSample, channelName, uploadNameType, uploadFolder } = body;
3032

3133
if (!fileSize || !fileName || !sha256 || !fileSample) {
3234
return new Response(JSON.stringify({
@@ -66,15 +68,19 @@ export async function onRequestPost(context) {
6668
});
6769
}
6870

69-
// 构建文件路径
70-
const now = new Date();
71-
const yearMonth = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}`;
71+
// 将命名参数添加到 URL 以便 buildUniqueFileId 使用
72+
if (uploadNameType) {
73+
url.searchParams.set('uploadNameType', uploadNameType);
74+
}
75+
if (uploadFolder) {
76+
url.searchParams.set('uploadFolder', uploadFolder);
77+
}
78+
79+
// 使用统一的文件命名函数生成文件ID
80+
const fullId = await buildUniqueFileId(context, fileName, fileType || 'application/octet-stream');
7281

73-
// 生成唯一文件名
74-
const ext = fileName.includes('.') ? fileName.substring(fileName.lastIndexOf('.')) : '';
75-
const uniqueId = crypto.randomUUID().replace(/-/g, '');
76-
const fullId = uniqueId + ext;
77-
const filePath = `images/${yearMonth}/${fullId}`;
82+
// 构建 HuggingFace 文件路径:直接使用 fullId(与其他渠道保持一致)
83+
const filePath = fullId;
7884

7985
// 获取 LFS 上传信息
8086
const huggingfaceAPI = new HuggingFaceAPI(hfChannel.token, hfChannel.repo, hfChannel.isPrivate || false);

functions/upload/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,10 +693,8 @@ async function uploadFileToHuggingFace(context, fullId, metadata, returnLink) {
693693
const precomputedSha256 = formdata.get('sha256') || null;
694694
console.log('File to upload:', fileName, 'size:', file?.size, 'precomputed SHA256:', precomputedSha256 ? 'yes' : 'no');
695695

696-
// 构建文件路径:images/年月/文件名
697-
const now = new Date();
698-
const yearMonth = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}`;
699-
const hfFilePath = `images/${yearMonth}/${fullId}`;
696+
// 构建文件路径:直接使用 fullId(与其他渠道保持一致)
697+
const hfFilePath = fullId;
700698
console.log('HuggingFace file path:', hfFilePath);
701699

702700
const huggingfaceAPI = new HuggingFaceAPI(hfChannel.token, hfChannel.repo, hfChannel.isPrivate || false);

functions/upload/uploadTools.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ export async function purgeCDNCache(env, cdnUrl, url, normalizedFolder) {
178178
}
179179

180180
// 结束上传:清除缓存,维护索引
181-
// 注意:容量统计现在由索引自动维护,不需要单独更新 quota
182181
export async function endUpload(context, fileId, metadata) {
183182
const { env, url } = context;
184183

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/logo.png"><link rel="apple-touch-icon" href="/logo.png"><link rel="mask-icon" href="/logo.png" color="#f4b400"><meta name="description" content="Sanyue ImgHub - A modern file hosting platform"><meta name="keywords" content="Sanyue, ImgHub, file hosting, image hosting, cloud storage"><meta name="author" content="SanyueQi"><title>Sanyue ImgHub</title><script defer="defer" src="/js/chunk-vendors.780b6559.js"></script><script defer="defer" src="/js/app.e39a667c.js"></script><link href="/css/chunk-vendors.4363ed49.css" rel="stylesheet"><link href="/css/app.52df53e3.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but sanyue_imghub doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html>
1+
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/logo.png"><link rel="apple-touch-icon" href="/logo.png"><link rel="mask-icon" href="/logo.png" color="#f4b400"><meta name="description" content="Sanyue ImgHub - A modern file hosting platform"><meta name="keywords" content="Sanyue, ImgHub, file hosting, image hosting, cloud storage"><meta name="author" content="SanyueQi"><title>Sanyue ImgHub</title><script defer="defer" src="/js/chunk-vendors.780b6559.js"></script><script defer="defer" src="/js/app.180f63ae.js"></script><link href="/css/chunk-vendors.4363ed49.css" rel="stylesheet"><link href="/css/app.52df53e3.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but sanyue_imghub doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html>

index.html.gz

0 Bytes
Binary file not shown.

js/343.30b37afb.js.gz

-20.2 KB
Binary file not shown.

js/343.30b37afb.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)