mirror of
https://github.com/MarSeventh/CloudFlare-ImgBed.git
synced 2026-01-31 00:53:20 +08:00
Fix:HuggingFace渠道文件命名和读取问题
This commit is contained in:
File diff suppressed because one or more lines are too long
Binary file not shown.
@@ -22,7 +22,7 @@ export async function onRequestPost(context) {
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { fullId, filePath, sha256, fileSize, fileName, channelName, multipartParts } = body;
|
||||
const { fullId, filePath, sha256, fileSize, fileName, fileType, channelName, multipartParts } = body;
|
||||
|
||||
if (!fullId || !filePath || !sha256 || !fileSize) {
|
||||
return new Response(JSON.stringify({
|
||||
@@ -86,6 +86,7 @@ export async function onRequestPost(context) {
|
||||
// 构建 metadata
|
||||
const metadata = {
|
||||
FileName: fileName || fullId,
|
||||
FileType: fileType || null,
|
||||
Channel: "HuggingFace",
|
||||
ChannelName: hfChannel.name || "HuggingFace_env",
|
||||
FileSize: (fileSize / 1024 / 1024).toFixed(2),
|
||||
|
||||
@@ -1,22 +1,24 @@
|
||||
/**
|
||||
* HuggingFace 大文件直传 API
|
||||
*
|
||||
*
|
||||
* 流程:
|
||||
* 1. 前端计算 SHA256 和文件样本
|
||||
* 2. 前端调用此 API 获取 LFS 上传 URL
|
||||
* 3. 前端直接上传到 HuggingFace S3
|
||||
* 4. 前端调用 commitUpload API 提交文件引用
|
||||
*
|
||||
*
|
||||
* 这样可以绕过 CF Workers 的 100MB 请求体限制和 CPU 时间限制
|
||||
*/
|
||||
|
||||
import { HuggingFaceAPI } from '../../utils/huggingfaceAPI.js';
|
||||
import { fetchUploadConfig } from '../../utils/sysConfig.js';
|
||||
import { userAuthCheck, UnauthorizedResponse } from '../../utils/userAuth.js';
|
||||
import { buildUniqueFileId } from '../../upload/uploadTools.js';
|
||||
|
||||
export async function onRequestPost(context) {
|
||||
const { request, env } = context;
|
||||
const url = new URL(request.url);
|
||||
context.url = url; // 将 url 添加到 context 以便 buildUniqueFileId 使用
|
||||
|
||||
try {
|
||||
// 鉴权
|
||||
@@ -26,7 +28,7 @@ export async function onRequestPost(context) {
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { fileSize, fileName, sha256, fileSample, channelName } = body;
|
||||
const { fileSize, fileName, fileType, sha256, fileSample, channelName, uploadNameType, uploadFolder } = body;
|
||||
|
||||
if (!fileSize || !fileName || !sha256 || !fileSample) {
|
||||
return new Response(JSON.stringify({
|
||||
@@ -66,15 +68,19 @@ export async function onRequestPost(context) {
|
||||
});
|
||||
}
|
||||
|
||||
// 构建文件路径
|
||||
const now = new Date();
|
||||
const yearMonth = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}`;
|
||||
// 将命名参数添加到 URL 以便 buildUniqueFileId 使用
|
||||
if (uploadNameType) {
|
||||
url.searchParams.set('uploadNameType', uploadNameType);
|
||||
}
|
||||
if (uploadFolder) {
|
||||
url.searchParams.set('uploadFolder', uploadFolder);
|
||||
}
|
||||
|
||||
// 生成唯一文件名
|
||||
const ext = fileName.includes('.') ? fileName.substring(fileName.lastIndexOf('.')) : '';
|
||||
const uniqueId = crypto.randomUUID().replace(/-/g, '');
|
||||
const fullId = uniqueId + ext;
|
||||
const filePath = `images/${yearMonth}/${fullId}`;
|
||||
// 使用统一的文件命名函数生成文件ID
|
||||
const fullId = await buildUniqueFileId(context, fileName, fileType || 'application/octet-stream');
|
||||
|
||||
// 构建 HuggingFace 文件路径:直接使用 fullId(与其他渠道保持一致)
|
||||
const filePath = fullId;
|
||||
|
||||
// 获取 LFS 上传信息
|
||||
const huggingfaceAPI = new HuggingFaceAPI(hfChannel.token, hfChannel.repo, hfChannel.isPrivate || false);
|
||||
|
||||
@@ -693,10 +693,8 @@ async function uploadFileToHuggingFace(context, fullId, metadata, returnLink) {
|
||||
const precomputedSha256 = formdata.get('sha256') || null;
|
||||
console.log('File to upload:', fileName, 'size:', file?.size, 'precomputed SHA256:', precomputedSha256 ? 'yes' : 'no');
|
||||
|
||||
// 构建文件路径:images/年月/文件名
|
||||
const now = new Date();
|
||||
const yearMonth = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}`;
|
||||
const hfFilePath = `images/${yearMonth}/${fullId}`;
|
||||
// 构建文件路径:直接使用 fullId(与其他渠道保持一致)
|
||||
const hfFilePath = fullId;
|
||||
console.log('HuggingFace file path:', hfFilePath);
|
||||
|
||||
const huggingfaceAPI = new HuggingFaceAPI(hfChannel.token, hfChannel.repo, hfChannel.isPrivate || false);
|
||||
|
||||
@@ -178,7 +178,6 @@ export async function purgeCDNCache(env, cdnUrl, url, normalizedFolder) {
|
||||
}
|
||||
|
||||
// 结束上传:清除缓存,维护索引
|
||||
// 注意:容量统计现在由索引自动维护,不需要单独更新 quota
|
||||
export async function endUpload(context, fileId, metadata) {
|
||||
const { env, url } = context;
|
||||
|
||||
|
||||
@@ -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>
|
||||
<!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>
|
||||
BIN
index.html.gz
BIN
index.html.gz
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
BIN
js/703.75e4e404.js.gz
Normal file
BIN
js/703.75e4e404.js.gz
Normal file
Binary file not shown.
1
js/703.75e4e404.js.map
Normal file
1
js/703.75e4e404.js.map
Normal file
File diff suppressed because one or more lines are too long
BIN
js/703.75e4e404.js.map.gz
Normal file
BIN
js/703.75e4e404.js.map.gz
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
BIN
js/832.e1c0a4cd.js.gz
Normal file
BIN
js/832.e1c0a4cd.js.gz
Normal file
Binary file not shown.
File diff suppressed because one or more lines are too long
BIN
js/832.e1c0a4cd.js.map.gz
Normal file
BIN
js/832.e1c0a4cd.js.map.gz
Normal file
Binary file not shown.
File diff suppressed because one or more lines are too long
BIN
js/app.180f63ae.js.gz
Normal file
BIN
js/app.180f63ae.js.gz
Normal file
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user