feat: 功能优化
This commit is contained in:
@@ -9,6 +9,39 @@ import { API_BASE } from '@gold/config/api'
|
||||
// 使用 webApi 前缀,确保能够被代理
|
||||
const BASE_URL = `${API_BASE.APP_TIK}/file`
|
||||
|
||||
/**
|
||||
* 获取视频时长(秒)
|
||||
* @param {File} file - 视频文件对象
|
||||
* @returns {Promise<number>} 时长(秒)
|
||||
*/
|
||||
function getVideoDuration(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 只处理视频文件
|
||||
if (!file.type.startsWith('video/')) {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const video = document.createElement('video');
|
||||
video.preload = 'metadata';
|
||||
video.muted = true; // 静音,避免浏览器阻止自动播放
|
||||
|
||||
video.onloadedmetadata = function() {
|
||||
const duration = Math.round(video.duration);
|
||||
URL.revokeObjectURL(video.src);
|
||||
resolve(duration);
|
||||
};
|
||||
|
||||
video.onerror = function() {
|
||||
URL.revokeObjectURL(video.src);
|
||||
console.warn('[视频时长] 获取失败,使用默认值60秒');
|
||||
resolve(60); // 返回默认值
|
||||
};
|
||||
|
||||
video.src = URL.createObjectURL(file);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 素材库 API 服务
|
||||
*/
|
||||
@@ -34,20 +67,33 @@ export const MaterialService = {
|
||||
* @param {File} file - 文件对象
|
||||
* @param {string} fileCategory - 文件分类(video/generate/audio/mix/voice)
|
||||
* @param {string} coverBase64 - 视频封面 base64(可选,data URI 格式)
|
||||
* @param {number} duration - 视频时长(秒,可选,自动获取)
|
||||
* @returns {Promise}
|
||||
*/
|
||||
uploadFile(file, fileCategory, coverBase64 = null) {
|
||||
async uploadFile(file, fileCategory, coverBase64 = null, duration = null) {
|
||||
// 如果没有提供时长且是视频文件,自动获取
|
||||
if (duration === null && file.type.startsWith('video/')) {
|
||||
duration = await getVideoDuration(file);
|
||||
console.log('[上传] 获取到视频时长:', duration, '秒');
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
formData.append('fileCategory', fileCategory)
|
||||
|
||||
|
||||
// 添加时长(如果是视频文件)
|
||||
if (duration !== null) {
|
||||
formData.append('duration', duration.toString());
|
||||
console.log('[上传] 附加视频时长:', duration, '秒');
|
||||
}
|
||||
|
||||
// 如果有封面 base64,添加到表单数据
|
||||
if (coverBase64) {
|
||||
// base64 格式:data:image/jpeg;base64,/9j/4AAQ...
|
||||
// 后端会解析这个格式
|
||||
formData.append('coverBase64', coverBase64)
|
||||
}
|
||||
|
||||
|
||||
// 大文件上传需要更长的超时时间(30分钟)
|
||||
return http.post(`${BASE_URL}/upload`, formData, {
|
||||
timeout: 30 * 60 * 1000 // 30分钟
|
||||
|
||||
Reference in New Issue
Block a user