// 媒体查询 API
|
const GRAPHQL_ENDPOINT = 'http://localhost:8080/api/graphql';
|
|
const MEDIAS_BY_TARGET_QUERY = `
|
query MediasByTarget($targetType: Int!, $targetId: ID!) {
|
mediasByTarget(targetType: $targetType, targetId: $targetId) {
|
id
|
name
|
path
|
fileExt
|
mediaType
|
fullUrl
|
}
|
}
|
`;
|
|
const SAVE_MEDIA_MUTATION = `
|
mutation SaveMedia($input: MediaInput!) {
|
saveMedia(input: $input) {
|
id
|
name
|
path
|
fileExt
|
mediaType
|
fullUrl
|
targetType
|
targetId
|
}
|
}
|
`;
|
|
const DELETE_MEDIA_MUTATION = `
|
mutation DeleteMedia($id: ID!) {
|
deleteMedia(id: $id)
|
}
|
`;
|
|
export const getMediasByTarget = async (targetType, targetId) => {
|
const res = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: { 'Content-Type': 'application/json' },
|
body: JSON.stringify({
|
query: MEDIAS_BY_TARGET_QUERY,
|
variables: { targetType, targetId }
|
})
|
});
|
const result = await res.json();
|
if (result.errors) {
|
throw new Error(result.errors[0].message);
|
}
|
return result.data.mediasByTarget || [];
|
};
|
|
export const saveMedia = async (input) => {
|
const res = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: { 'Content-Type': 'application/json' },
|
body: JSON.stringify({
|
query: SAVE_MEDIA_MUTATION,
|
variables: { input }
|
})
|
});
|
const result = await res.json();
|
if (result.errors) {
|
throw new Error(result.errors[0].message);
|
}
|
return result.data.saveMedia;
|
};
|
|
export const deleteMedia = async (id) => {
|
console.log('=== deleteMedia API调用 ===');
|
console.log('要删除的媒体ID:', id);
|
console.log('GraphQL查询:', DELETE_MEDIA_MUTATION);
|
|
const res = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: { 'Content-Type': 'application/json' },
|
body: JSON.stringify({
|
query: DELETE_MEDIA_MUTATION,
|
variables: { id: id.toString() }
|
})
|
});
|
const result = await res.json();
|
console.log('GraphQL响应:', result);
|
console.log('deleteMedia结果:', result.data?.deleteMedia);
|
|
if (result.errors) {
|
console.error('GraphQL错误:', result.errors);
|
throw new Error(result.errors[0].message);
|
}
|
|
const deleteResult = result.data.deleteMedia;
|
console.log('返回的删除结果:', deleteResult, '类型:', typeof deleteResult);
|
return deleteResult;
|
};
|
|
// 上传文件到服务器
|
export const uploadFile = async (file) => {
|
const formData = new FormData();
|
formData.append('file', file);
|
|
const response = await fetch('http://localhost:8080/api/upload/image', {
|
method: 'POST',
|
body: formData
|
});
|
|
const result = await response.json();
|
if (!result.success) {
|
throw new Error(result.error || '上传失败');
|
}
|
|
return result;
|
};
|
|
// 上传视频文件并自动生成缩略图
|
export const uploadVideoWithThumbnail = async (videoFile) => {
|
const { extractVideoFrame, generateThumbnailFileName } = await import('@/utils/video.js');
|
|
try {
|
console.log('开始处理视频文件:', videoFile.name);
|
|
// 1. 上传原视频文件
|
console.log('上传视频文件...');
|
const videoUploadResult = await uploadFile(videoFile);
|
console.log('视频上传成功:', videoUploadResult);
|
|
// 2. 提取视频第一帧
|
console.log('提取视频第一帧...');
|
const thumbnailBlob = await extractVideoFrame(videoFile);
|
console.log('视频帧提取成功,大小:', thumbnailBlob.size);
|
|
// 3. 创建缩略图文件对象
|
const thumbnailFileName = generateThumbnailFileName(videoFile.name);
|
const thumbnailFile = new File([thumbnailBlob], thumbnailFileName, {
|
type: 'image/jpeg'
|
});
|
|
// 4. 上传缩略图
|
console.log('上传缩略图...');
|
const thumbnailUploadResult = await uploadFile(thumbnailFile);
|
console.log('缩略图上传成功:', thumbnailUploadResult);
|
|
// 5. 返回包含视频和缩略图信息的结果
|
return {
|
video: videoUploadResult,
|
thumbnail: thumbnailUploadResult,
|
success: true
|
};
|
|
} catch (error) {
|
console.error('视频处理失败:', error);
|
throw new Error(`视频处理失败: ${error.message}`);
|
}
|
};
|