forked from 77media/video-flow
66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
|
|
import { getUploadToken, uploadToQiniu } from "@/api/common";
|
|
import { useState, useCallback } from "react";
|
|
import { ScriptEditKey } from "../usecase/ScriptEditUseCase";
|
|
/**
|
|
* 渲染数据转换器
|
|
* @param key
|
|
* @param headerName
|
|
* @param scriptText
|
|
* @returns
|
|
*/
|
|
export function parseScriptBlock(
|
|
key: ScriptEditKey,
|
|
headerName: string,
|
|
scriptText: string,
|
|
contentType?: 'paragraph' | 'bold' | 'italic' | 'heading' | 'tag',
|
|
) {
|
|
return {
|
|
id: key,
|
|
title: headerName,
|
|
content: [
|
|
{
|
|
type: contentType || "paragraph",
|
|
text: scriptText,
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 用于上传文件到七牛云的自定义 Hook
|
|
* @returns {object} - 包含上传函数和加载状态
|
|
*/
|
|
export function useUploadFile() {
|
|
/** 加载状态 */
|
|
const [isUploading, setIsUploading] = useState(false);
|
|
|
|
/**
|
|
* 上传文件到七牛云
|
|
* @param {File} file - 要上传的文件
|
|
* @param {(progress: number) => void} [onProgress] - 上传进度回调
|
|
* @returns {Promise<string>} - 上传后文件的 URL
|
|
* @throws {Error} - 上传失败时抛出异常
|
|
*/
|
|
const uploadFile = useCallback(
|
|
async (file: File, onProgress?: (progress: number) => void): Promise<string> => {
|
|
try {
|
|
setIsUploading(true);
|
|
const { token } = await getUploadToken();
|
|
const fileUrl = await uploadToQiniu(file, token, onProgress);
|
|
return fileUrl;
|
|
} catch (err) {
|
|
console.error('文件上传失败:', err);
|
|
throw err;
|
|
} finally {
|
|
setIsUploading(false);
|
|
}
|
|
},
|
|
[]
|
|
);
|
|
|
|
return { uploadFile, isUploading };
|
|
}
|