优化

Change-Id: I0aaa165219226ad4a22bfb03141dcafe9efdf6c3
diff --git a/src/api/torrent.js b/src/api/torrent.js
index 4a623d7..a6a1663 100644
--- a/src/api/torrent.js
+++ b/src/api/torrent.js
@@ -38,22 +38,16 @@
 

 /**

  * 下载种子文件

- * @param {number} torrentId 种子ID

+ * @param {number} id 种子ID

  * @param {string} downloadPath 下载路径

  */

-export async function downloadTorrent(torrentId, downloadPath) {

-  try {

-    const response = await api.get(`/torrent/downloadTorrent`, {

-      params: {

-        id: torrentId,

-        downloadPath: downloadPath

-      }

-    });

-    return response.data;

-  } catch (error) {

-    console.error('下载种子失败:', error);

-    throw error;

-  }

+export function downloadTorrent(id, downloadPath) {

+  return api.get('/torrent/downloadTorrent', {

+    params: {

+      id,

+      downloadPath

+    }

+  });

 }

 

 /**

diff --git a/src/components/Dashboard.jsx b/src/components/Dashboard.jsx
index e393fcd..3476eca 100644
--- a/src/components/Dashboard.jsx
+++ b/src/components/Dashboard.jsx
@@ -1,4 +1,4 @@
-import React, {useEffect, useState} from 'react';

+import React, {useEffect, useState, useRef} from 'react';

 import {useNavigate, useLocation, useParams} from 'react-router-dom';  

 import {createTorrent, getTorrents, downloadTorrent, getDownloadProgress, deleteTorrent, searchTorrents} from '../api/torrent';

 import './Dashboard.css';

@@ -63,6 +63,7 @@
     const [downloadProgress, setDownloadProgress] = useState(0);

     const [isDownloading, setIsDownloading] = useState(false);

     const [downloadPath, setDownloadPath] = useState('D:/studies/ptPlatform/torrent');

+    // const timerRef = useRef(null);

 

     // 新增搜索状态

     const [announcementSearch, setAnnouncementSearch] = useState('');

@@ -360,80 +361,139 @@
     };

 

     // 执行下载

-    const handleDownload = async () => {

-    if (!selectedTorrent || !downloadPath) return;

+    // const handleDownload = async () => {

+    // if (!selectedTorrent || !downloadPath) return;

 

-    setIsDownloading(true);

-    setDownloadProgress(0);

+    // setIsDownloading(true);

+    // setDownloadProgress(0);

 

-    try {      

-        // 发起下载请求

-        await downloadTorrent(selectedTorrent.id, downloadPath);

-        message.success("下载任务已提交");

-        // 开始轮询进度

-        const interval = setInterval(async () => {

-            try {

-                const res = await getDownloadProgress();

-                const progresses = res.data.progresses;

+    // try {      

+    //     // 发起下载请求

+    //     await downloadTorrent(selectedTorrent.id, downloadPath);

+    //     message.success("下载任务已提交");

+    //     // 开始轮询进度

+    //     const interval = setInterval(async () => {

+    //         try {

+    //             const res = await getDownloadProgress();

+    //             const progresses = res.data.progresses;

                 

-                if (progresses) {

-                    // 使用完整的 torrent 文件路径作为键

-                    const torrentPath = selectedTorrent.downloadPath.replace(/\\/g, '/');

-                    const torrentHash = selectedTorrent.hash;

-                    // 查找匹配的进度

-                    let foundProgress = null;

-                    for (const [key, value] of Object.entries(progresses)) {

-                        const normalizedKey = key.replace(/\\/g, '/');

-                        if (normalizedKey.includes(selectedTorrent.hash) || 

-                            normalizedKey.includes(selectedTorrent.torrentName)) {

-                            foundProgress = value;

-                            break;

-                        }

-                    }

-                    if (foundProgress !== null) {

-                        const newProgress = Math.round(foundProgress * 100);

-                        setDownloadProgress(newProgress);

+    //             if (progresses) {

+    //                 // 使用完整的 torrent 文件路径作为键

+    //                 const torrentPath = selectedTorrent.downloadPath.replace(/\\/g, '/');

+    //                 const torrentHash = selectedTorrent.hash;

+    //                 // 查找匹配的进度

+    //                 let foundProgress = null;

+    //                 for (const [key, value] of Object.entries(progresses)) {

+    //                     const normalizedKey = key.replace(/\\/g, '/');

+    //                     if (normalizedKey.includes(selectedTorrent.hash) || 

+    //                         normalizedKey.includes(selectedTorrent.torrentName)) {

+    //                         foundProgress = value;

+    //                         break;

+    //                     }

+    //                 }

+    //                 if (foundProgress !== null) {

+    //                     const newProgress = Math.round(foundProgress * 100);

+    //                     setDownloadProgress(newProgress);

                         

-                        // 检查是否下载完成

-                        if (newProgress >= 100) {

-                        clearInterval(interval);

-                        setIsDownloading(false);

-                        message.success('下载完成!');

-                        setTimeout(() => setShowDownloadModal(false), 2000);

-                        }

-                    } else {

-                        console.log('当前下载进度:', progresses); // 添加日志

-                    }

-                    }

-                } catch (error) {

-                    console.error('获取进度失败:', error);

-                    // 如果获取进度失败但文件已存在,也视为完成

-                    const filePath = `${downloadPath}${selectedTorrent.torrentName || 'downloaded_file'}`;

-                    try {

-                    const exists = await checkFileExists(filePath);

-                    if (exists) {

-                        clearInterval(interval);

-                        setDownloadProgress(100);

-                        setIsDownloading(false);

-                        message.success('下载完成!');

-                        setTimeout(() => setShowDownloadModal(false), 2000);

-                    }

-                    } catch (e) {

-                    console.error('文件检查失败:', e);

-                    }

-                }

-                }, 2000);

+    //                     // 检查是否下载完成

+    //                     if (newProgress >= 100) {

+    //                     clearInterval(interval);

+    //                     setIsDownloading(false);

+    //                     message.success('下载完成!');

+    //                     setTimeout(() => setShowDownloadModal(false), 2000);

+    //                     }

+    //                 } else {

+    //                     console.log('当前下载进度:', progresses); // 添加日志

+    //                 }

+    //                 }

+    //             } catch (error) {

+    //                 console.error('获取进度失败:', error);

+    //                 // 如果获取进度失败但文件已存在,也视为完成

+    //                 const filePath = `${downloadPath}${selectedTorrent.torrentName || 'downloaded_file'}`;

+    //                 try {

+    //                 const exists = await checkFileExists(filePath);

+    //                 if (exists) {

+    //                     clearInterval(interval);

+    //                     setDownloadProgress(100);

+    //                     setIsDownloading(false);

+    //                     message.success('下载完成!');

+    //                     setTimeout(() => setShowDownloadModal(false), 2000);

+    //                 }

+    //                 } catch (e) {

+    //                 console.error('文件检查失败:', e);

+    //                 }

+    //             }

+    //             }, 2000);

 

-        return () => clearInterval(interval);

-    } catch (error) {

-        setIsDownloading(false);

-        if (error.response && error.response.status === 409) {

-        message.error('分享率不足,无法下载此资源');

-        } else {

-        message.error('下载失败: ' + (error.message || '未知错误'));

+    //     return () => clearInterval(interval);

+    // } catch (error) {

+    //     setIsDownloading(false);

+    //     if (error.response && error.response.status === 409) {

+    //     message.error('分享率不足,无法下载此资源');

+    //     } else {

+    //     message.error('下载失败: ' + (error.message || '未知错误'));

+    //     }

+    // }

+    // };

+

+    const handleDownload = async () => {

+        if (!selectedTorrent || !downloadPath) return;

+

+        setIsDownloading(true);

+        // startPollingProgress();

+

+        try {      

+            // 发起下载请求

+            await downloadTorrent(selectedTorrent.id, downloadPath);

+            message.success("下载任务已提交");

+            // setTimeout(() => setShowDownloadModal(false), 2000);

+        } catch (error) {

+            if (error.response && error.response.status === 409) {

+                message.error('分享率不足,无法下载此资源');

+            } else {

+                message.error('下载失败: ' + (error.message || '未知错误'));

+            }

+        }finally{

+            setIsDownloading(false);

         }

-    }

     };

+    

+

+    // const startPollingProgress = () => {

+    //     timerRef.current = setInterval(async () => {

+    //     try {

+    //         const res = await getDownloadProgress();

+    //         if (res.code === 200 && res.data.progresses) {

+    //         const taskProgress = res.data.progresses[selectedTorrent?.id];

+    //         if (taskProgress !== undefined) {

+    //             const percent = Math.floor(taskProgress * 100);

+    //             setDownloadProgress(percent);

+

+    //             if (percent >= 100) {

+    //             clearInterval(timerRef.current);

+    //             message.success("下载完成");

+    //             setTimeout(() => {

+    //                 setIsDownloading(false);

+    //                 setShowDownloadModal(false);

+    //             }, 1500);

+    //             }

+    //         }

+    //         }

+    //     } catch (err) {

+    //         console.error("获取进度失败", err);

+    //     }

+    //     }, 1000);

+    // };

+

+    // const stopPollingProgress = () => {

+    //     clearInterval(timerRef.current);

+    // };

+

+    // useEffect(() => {

+    //     return () => {

+    //     stopPollingProgress();

+    //     };

+    // }, []);

 

     const checkFileExists = async (filePath) => {

         try {