blob: a6a166331cbd985b62058261edff25f71c134010 [file] [log] [blame]
Akane121765b61a72025-05-17 13:52:25 +08001// src/api/torrent.js
2import { api } from './auth'; // 复用已有的axios实例
3
DREWae420b22025-06-02 14:07:20 +08004/**
5 * 创建并上传一个种子
6 * @param {File} file 种子文件 (.torrent)
7 * @param {Object} bodyObj 包含种子信息的对象,格式如下:
8 * {
9 * torrentName: "测试下载",
10 * description: "A high-quality 1080p version of Example Movie.",
11 * category: "电影",
12 * region: "USA",
13 * resolution: "1080p",
14 * subtitle: "English",
15 * filePath: "D:/大学/大三_下/torrentFrom/[电影天堂www.dytt89.com]两杆大烟枪BD中英双字.mp4.torrent"
16 * }
17 */
18export async function createTorrent(file, bodyObj) {
22301080a93bebb2025-05-27 19:48:11 +080019 const formData = new FormData();
DREWae420b22025-06-02 14:07:20 +080020 formData.append('file', file);
22301080a93bebb2025-05-27 19:48:11 +080021
DREWae420b22025-06-02 14:07:20 +080022 // 关键修改:将JSON字符串转换为Blob对象,并设置正确的Content-Type
23 formData.append('body', new Blob([JSON.stringify(bodyObj)], {
24 type: 'application/json'
25 }));
26
27 try {
28 const response = await api.post('/torrent', formData, {
29 headers: {
30 }
31 });
32 return response.data;
33 } catch (error) {
34 console.error('上传种子失败:', error);
35 throw error;
22301080a93bebb2025-05-27 19:48:11 +080036 }
Akane121765b61a72025-05-17 13:52:25 +080037};
38
DREWae420b22025-06-02 14:07:20 +080039/**
40 * 下载种子文件
DREW311a1a42025-06-07 21:19:03 +080041 * @param {number} id 种子ID
DREWae420b22025-06-02 14:07:20 +080042 * @param {string} downloadPath 下载路径
43 */
DREW311a1a42025-06-07 21:19:03 +080044export function downloadTorrent(id, downloadPath) {
45 return api.get('/torrent/downloadTorrent', {
46 params: {
47 id,
48 downloadPath
49 }
50 });
DREWae420b22025-06-02 14:07:20 +080051}
52
53/**
54 * 获取下载进度
55 */
56export async function getDownloadProgress() {
57 try {
58 const response = await api.get('/torrent/getProgress');
59 return response.data;
60 } catch (error) {
61 console.error('获取下载进度失败:', error);
62 throw error;
63 }
64}
65
Akane121765b61a72025-05-17 13:52:25 +080066export const getTorrents = (page = 1, size = 5) => {
67 return api.get('/torrent', {
68 params: { page, size }
69 });
70};
71
72export const getTorrentDetail = (torrentId) => {
73 return api.get(`/torrent/${torrentId}`).then(response => {
74 // 确保数据结构一致
75 if (response.data && response.data.data) {
76 return {
77 ...response,
78 data: {
79 ...response.data,
80 data: {
Akane12173a7bb972025-06-01 01:05:27 +080081 torrent: response.data.data.torrent, // 直接使用后端返回的格式化数据
Akane121765b61a72025-05-17 13:52:25 +080082 comments: response.data.data.comments || []
83 }
84 }
85 };
86 }
87 return response;
88 });
89};
90
91export const likeTorrent = (torrentId) => {
92 return api.post(`/torrent/${torrentId}/like`);
93};
94
95export const addTorrentComment = (torrentId, commentData) => {
96 return api.post(`/torrent/${torrentId}/comments`, commentData);
Akane12173a7bb972025-06-01 01:05:27 +080097};
98
DREWae420b22025-06-02 14:07:20 +080099export const deleteTorrent = (torrentId) => {
100 return api.delete(`/torrent/deleteTorrent/${torrentId}`);
101};
102
103// // 上传种子接口
104// export const uploadTorrent = async (torrentFile, torrentData, onProgress) => {
105// // 1. 基础验证
106// if (!torrentFile || !torrentFile.name.endsWith('.torrent')) {
107// throw new Error('请选择有效的.torrent文件');
108// }
109
110// // 2. 构建FormData
111// const formData = new FormData();
112// formData.append('file', torrentFile);
113
114// // 确保JSON内容使用正确的Content-Type
115// const metadataBlob = new Blob(
116// [JSON.stringify(torrentData)],
117// { type: 'application/json' }
118// );
119// formData.append('body', metadataBlob);
120
121// // 3. 获取认证token(根据你的实际存储方式调整)
122// const token = localStorage.getItem('token') || '';
123
124// try {
125// const response = await api.post('/api/torrents', formData, {
126// headers: {
127// 'Authorization': `Bearer ${token}`,
128// },
129// onUploadProgress: (progressEvent) => {
130// if (onProgress && progressEvent.total) {
131// const percent = Math.round((progressEvent.loaded * 100) / progressEvent.total);
132// onProgress(percent);
133// }
134// }
135// });
136
137// return response.data;
138// } catch (error) {
139// console.error('上传失败:', error);
140
141// // 增强错误处理
142// let errorMessage = '上传失败';
143// if (error.response) {
144// errorMessage = error.response.data?.message ||
145// `服务器错误: ${error.response.status}`;
146// } else if (error.request) {
147// errorMessage = '网络错误,请检查连接';
148// }
149
150// throw new Error(errorMessage);
151// }
152// };
153
Akane12173a7bb972025-06-01 01:05:27 +0800154export const searchTorrents = (keyword, page = 1, size = 5) => {
155 return api.get('/torrent/search', {
156 params: { keyword, page, size }
157 });
Akane121765b61a72025-05-17 13:52:25 +0800158};