Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 1 | // src/api/torrent.js
|
| 2 | import { api } from './auth'; // 复用已有的axios实例
|
| 3 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame] | 4 | /**
|
| 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 | */
|
| 18 | export async function createTorrent(file, bodyObj) {
|
22301080 | a93bebb | 2025-05-27 19:48:11 +0800 | [diff] [blame] | 19 | const formData = new FormData();
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame] | 20 | formData.append('file', file);
|
22301080 | a93bebb | 2025-05-27 19:48:11 +0800 | [diff] [blame] | 21 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame] | 22 | // 关键修改:将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;
|
22301080 | a93bebb | 2025-05-27 19:48:11 +0800 | [diff] [blame] | 36 | }
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 37 | };
|
| 38 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame] | 39 | /**
|
| 40 | * 下载种子文件
|
| 41 | * @param {number} torrentId 种子ID
|
| 42 | * @param {string} downloadPath 下载路径
|
| 43 | */
|
| 44 | export async function downloadTorrent(torrentId, downloadPath) {
|
| 45 | try {
|
| 46 | const response = await api.get(`/torrent/downloadTorrent`, {
|
| 47 | params: {
|
| 48 | id: torrentId,
|
| 49 | downloadPath: downloadPath
|
| 50 | }
|
| 51 | });
|
| 52 | return response.data;
|
| 53 | } catch (error) {
|
| 54 | console.error('下载种子失败:', error);
|
| 55 | throw error;
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | /**
|
| 60 | * 获取下载进度
|
| 61 | */
|
| 62 | export async function getDownloadProgress() {
|
| 63 | try {
|
| 64 | const response = await api.get('/torrent/getProgress');
|
| 65 | return response.data;
|
| 66 | } catch (error) {
|
| 67 | console.error('获取下载进度失败:', error);
|
| 68 | throw error;
|
| 69 | }
|
| 70 | }
|
| 71 |
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 72 | export const getTorrents = (page = 1, size = 5) => {
|
| 73 | return api.get('/torrent', {
|
| 74 | params: { page, size }
|
| 75 | });
|
| 76 | };
|
| 77 |
|
| 78 | export const getTorrentDetail = (torrentId) => {
|
| 79 | return api.get(`/torrent/${torrentId}`).then(response => {
|
| 80 | // 确保数据结构一致
|
| 81 | if (response.data && response.data.data) {
|
| 82 | return {
|
| 83 | ...response,
|
| 84 | data: {
|
| 85 | ...response.data,
|
| 86 | data: {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 87 | torrent: response.data.data.torrent, // 直接使用后端返回的格式化数据
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 88 | comments: response.data.data.comments || []
|
| 89 | }
|
| 90 | }
|
| 91 | };
|
| 92 | }
|
| 93 | return response;
|
| 94 | });
|
| 95 | };
|
| 96 |
|
| 97 | export const likeTorrent = (torrentId) => {
|
| 98 | return api.post(`/torrent/${torrentId}/like`);
|
| 99 | };
|
| 100 |
|
| 101 | export const addTorrentComment = (torrentId, commentData) => {
|
| 102 | return api.post(`/torrent/${torrentId}/comments`, commentData);
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 103 | };
|
| 104 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame] | 105 | export const deleteTorrent = (torrentId) => {
|
| 106 | return api.delete(`/torrent/deleteTorrent/${torrentId}`);
|
| 107 | };
|
| 108 |
|
| 109 | // // 上传种子接口
|
| 110 | // export const uploadTorrent = async (torrentFile, torrentData, onProgress) => {
|
| 111 | // // 1. 基础验证
|
| 112 | // if (!torrentFile || !torrentFile.name.endsWith('.torrent')) {
|
| 113 | // throw new Error('请选择有效的.torrent文件');
|
| 114 | // }
|
| 115 |
|
| 116 | // // 2. 构建FormData
|
| 117 | // const formData = new FormData();
|
| 118 | // formData.append('file', torrentFile);
|
| 119 |
|
| 120 | // // 确保JSON内容使用正确的Content-Type
|
| 121 | // const metadataBlob = new Blob(
|
| 122 | // [JSON.stringify(torrentData)],
|
| 123 | // { type: 'application/json' }
|
| 124 | // );
|
| 125 | // formData.append('body', metadataBlob);
|
| 126 |
|
| 127 | // // 3. 获取认证token(根据你的实际存储方式调整)
|
| 128 | // const token = localStorage.getItem('token') || '';
|
| 129 |
|
| 130 | // try {
|
| 131 | // const response = await api.post('/api/torrents', formData, {
|
| 132 | // headers: {
|
| 133 | // 'Authorization': `Bearer ${token}`,
|
| 134 | // },
|
| 135 | // onUploadProgress: (progressEvent) => {
|
| 136 | // if (onProgress && progressEvent.total) {
|
| 137 | // const percent = Math.round((progressEvent.loaded * 100) / progressEvent.total);
|
| 138 | // onProgress(percent);
|
| 139 | // }
|
| 140 | // }
|
| 141 | // });
|
| 142 |
|
| 143 | // return response.data;
|
| 144 | // } catch (error) {
|
| 145 | // console.error('上传失败:', error);
|
| 146 |
|
| 147 | // // 增强错误处理
|
| 148 | // let errorMessage = '上传失败';
|
| 149 | // if (error.response) {
|
| 150 | // errorMessage = error.response.data?.message ||
|
| 151 | // `服务器错误: ${error.response.status}`;
|
| 152 | // } else if (error.request) {
|
| 153 | // errorMessage = '网络错误,请检查连接';
|
| 154 | // }
|
| 155 |
|
| 156 | // throw new Error(errorMessage);
|
| 157 | // }
|
| 158 | // };
|
| 159 |
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 160 | export const searchTorrents = (keyword, page = 1, size = 5) => {
|
| 161 | return api.get('/torrent/search', {
|
| 162 | params: { keyword, page, size }
|
| 163 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 164 | }; |