From a1e46c4d7608fb8265372d919f2fd3e09e53a558 Mon Sep 17 00:00:00 2001 From: kjqwer <2990346238@qq.com> Date: Wed, 27 Aug 2025 08:40:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8B=E8=BD=BD=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=EF=BC=8C=E6=B8=85=E9=99=A4=E9=83=A8=E5=88=86=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/auth.js | 7 --- backend/core.js | 2 +- backend/routes/download.js | 37 ------------- backend/services/download.js | 94 +++++----------------------------- backend/services/repository.js | 17 ++++-- 5 files changed, 29 insertions(+), 128 deletions(-) diff --git a/backend/auth.js b/backend/auth.js index 3f583b6..b1f34fa 100644 --- a/backend/auth.js +++ b/backend/auth.js @@ -124,23 +124,16 @@ class PixivAuth { include_policy: true }; - console.log('请求数据:', data); - const headers = { ...this.getDefaultHeaders(), 'Content-Type': 'application/x-www-form-urlencoded' }; - console.log('请求头部:', headers); - const response = await this.axiosInstance.post('https://oauth.secure.pixiv.net/auth/token', stringify(data), { headers } ); - console.log('响应状态:', response.status); - console.log('响应数据:', JSON.stringify(response.data, null, 2)); - const tokenData = response.data.response; this.accessToken = tokenData.access_token; diff --git a/backend/core.js b/backend/core.js index 665a892..a1ac9fd 100644 --- a/backend/core.js +++ b/backend/core.js @@ -4,7 +4,7 @@ const PixivAuth = require('./auth'); const DownloadService = require('./services/download'); // 配置文件路径 -const CONFIG_FILE_DIR = require('appdata-path').getAppDataPath('pxder'); +const CONFIG_FILE_DIR = require('appdata-path').getAppDataPath('pmanager'); const CONFIG_FILE = Path.resolve(CONFIG_FILE_DIR, 'config.json'); // 默认配置 diff --git a/backend/routes/download.js b/backend/routes/download.js index 0293ddf..95bb2a5 100644 --- a/backend/routes/download.js +++ b/backend/routes/download.js @@ -650,41 +650,4 @@ router.get('/stats', async (req, res) => { } }); -/** - * 强制重新检查作品下载状态 - * POST /api/download/force-check/:artworkId - */ -router.post('/force-check/:artworkId', async (req, res) => { - try { - const { artworkId } = req.params; - - if (!artworkId || isNaN(parseInt(artworkId))) { - return res.status(400).json({ - success: false, - error: 'Invalid artwork ID' - }); - } - - const downloadService = req.backend.getDownloadService(); - - // 强制重新检查,包括清理不完整的文件 - const result = await downloadService.forceCheckArtworkDownloaded(parseInt(artworkId)); - - res.json({ - success: true, - data: { - artwork_id: parseInt(artworkId), - is_downloaded: result.is_downloaded, - cleaned_files: result.cleaned_files || 0, - message: result.message - } - }); - } catch (error) { - res.status(500).json({ - success: false, - error: error.message - }); - } -}); - module.exports = router; \ No newline at end of file diff --git a/backend/services/download.js b/backend/services/download.js index 2b35114..7ea8dd3 100644 --- a/backend/services/download.js +++ b/backend/services/download.js @@ -319,7 +319,11 @@ class DownloadService { // 检查作品信息文件 - 这是最可靠的判断标准 const infoPath = path.join(artworkPath, 'artwork_info.json'); - if (!(await this.fileManager.fileExists(infoPath))) { + let artworkInfo; + try { + const infoContent = await fs.readFile(infoPath, 'utf8'); + artworkInfo = JSON.parse(infoContent); + } catch (error) { console.log(`作品 ${artworkId} 缺少信息文件,认为未下载`); return false; } @@ -333,8 +337,15 @@ class DownloadService { return false; } - // 有信息文件且有图片文件,认为已下载 - console.log(`作品 ${artworkId} 已下载,有信息文件和 ${imageFiles.length} 个图片文件`); + // 检查图片数量是否与artwork_info.json中记录的一致 + const expectedImageCount = artworkInfo.page_count || 1; + if (imageFiles.length < expectedImageCount) { + console.log(`作品 ${artworkId} 图片数量不匹配: 期望 ${expectedImageCount} 个,实际 ${imageFiles.length} 个`); + return false; + } + + // 有信息文件、有图片文件且数量匹配,认为已下载 + console.log(`作品 ${artworkId} 已完整下载,有信息文件和 ${imageFiles.length}/${expectedImageCount} 个图片文件`); return true; } } @@ -347,83 +358,6 @@ class DownloadService { } } - /** - * 强制重新检查作品下载状态,包括清理不完整的文件 - */ - async forceCheckArtworkDownloaded(artworkId) { - try { - const downloadPath = await this.fileManager.getDownloadPath(); - let cleanedFiles = 0; - - // 扫描所有作者目录 - const artistEntries = await this.fileManager.listDirectory(downloadPath); - - for (const artistEntry of artistEntries) { - const artistPath = path.join(downloadPath, artistEntry); - const artistStat = await this.fileManager.getFileInfo(artistPath); - - if (!artistStat.exists || !artistStat.isDirectory) continue; - - // 扫描作者下的作品目录 - const artworkEntries = await this.fileManager.listDirectory(artistPath); - - for (const artworkEntry of artworkEntries) { - // 检查是否是目标作品目录(包含数字ID) - const artworkMatch = artworkEntry.match(/^(\d+)_(.+)$/); - if (artworkMatch && artworkMatch[1] === artworkId.toString()) { - const artworkPath = path.join(artistPath, artworkEntry); - - // 检查作品信息文件 - 这是最可靠的判断标准 - const infoPath = path.join(artworkPath, 'artwork_info.json'); - if (!(await this.fileManager.fileExists(infoPath))) { - console.log(`作品 ${artworkId} 缺少信息文件,清理目录`); - await this.fileManager.removeDirectory(artworkPath); - return { - is_downloaded: false, - cleaned_files: 1, - message: '作品目录不完整,已清理' - }; - } - - // 检查是否有图片文件 - const files = await this.fileManager.listDirectory(artworkPath); - const imageFiles = files.filter(file => /\.(jpg|jpeg|png|gif|webp)$/i.test(file) && file !== 'artwork_info.json'); - - if (imageFiles.length === 0) { - console.log(`作品 ${artworkId} 有信息文件但没有图片文件,清理目录`); - await this.fileManager.removeDirectory(artworkPath); - return { - is_downloaded: false, - cleaned_files: 1, - message: '作品目录没有图片文件,已清理' - }; - } - - // 有信息文件且有图片文件,认为已下载 - return { - is_downloaded: true, - cleaned_files: cleanedFiles, - message: `作品已下载,有信息文件和 ${imageFiles.length} 个图片文件` - }; - } - } - } - - return { - is_downloaded: false, - cleaned_files: cleanedFiles, - message: '作品未找到' - }; - } catch (error) { - console.error('强制检查作品下载状态失败:', error); - return { - is_downloaded: false, - cleaned_files: 0, - message: `检查失败: ${error.message}` - }; - } - } - /** * 下载单个作品 */ diff --git a/backend/services/repository.js b/backend/services/repository.js index 553026d..ce9e70e 100644 --- a/backend/services/repository.js +++ b/backend/services/repository.js @@ -466,10 +466,12 @@ class RepositoryService { // 检查作品信息文件 - 这是最可靠的判断标准 const infoPath = path.join(artworkPath, 'artwork_info.json') + let artworkInfo; try { - await fs.access(infoPath) + const infoContent = await fs.readFile(infoPath, 'utf8') + artworkInfo = JSON.parse(infoContent) } catch (error) { - // 信息文件不存在,认为未下载 + // 信息文件不存在或无法读取,认为未下载 return false } @@ -480,7 +482,16 @@ class RepositoryService { return false } - // 有信息文件且有图片文件,认为已下载 + // 检查图片数量是否与artwork_info.json中记录的一致 + const expectedImageCount = artworkInfo.page_count || 1 + if (files.length < expectedImageCount) { + // 图片文件数量不足,认为下载不完整 + console.log(`作品 ${artworkId} 图片数量不匹配: 期望 ${expectedImageCount} 个,实际 ${files.length} 个`) + return false + } + + // 有信息文件、有图片文件且数量匹配,认为已下载 + console.log(`作品 ${artworkId} 已完整下载: ${files.length}/${expectedImageCount} 个图片文件`) return true } }