多余日志清理,修复部分作品无法创建文件夹的问题
This commit is contained in:
@@ -8,7 +8,6 @@ const DownloadService = require('../services/download');
|
|||||||
*/
|
*/
|
||||||
router.post('/artwork/:id', async (req, res) => {
|
router.post('/artwork/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
console.log(`收到下载请求: 作品ID ${req.params.id}`);
|
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const {
|
const {
|
||||||
size = 'original',
|
size = 'original',
|
||||||
@@ -17,8 +16,6 @@ router.post('/artwork/:id', async (req, res) => {
|
|||||||
skipExisting = true
|
skipExisting = true
|
||||||
} = req.body;
|
} = req.body;
|
||||||
|
|
||||||
console.log(`下载参数: size=${size}, quality=${quality}, format=${format}, skipExisting=${skipExisting}`);
|
|
||||||
|
|
||||||
if (!id || isNaN(parseInt(id))) {
|
if (!id || isNaN(parseInt(id))) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
@@ -27,7 +24,6 @@ router.post('/artwork/:id', async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const downloadService = req.backend.getDownloadService();
|
const downloadService = req.backend.getDownloadService();
|
||||||
console.log('开始调用下载服务...');
|
|
||||||
const result = await downloadService.downloadArtwork(parseInt(id), {
|
const result = await downloadService.downloadArtwork(parseInt(id), {
|
||||||
size,
|
size,
|
||||||
quality,
|
quality,
|
||||||
@@ -35,8 +31,6 @@ router.post('/artwork/:id', async (req, res) => {
|
|||||||
skipExisting
|
skipExisting
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('下载服务返回结果:', result);
|
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
+6
-3
@@ -22,7 +22,7 @@ const proxyConfig = require('./config');
|
|||||||
|
|
||||||
// 自定义日志中间件
|
// 自定义日志中间件
|
||||||
function customLogger(req, res, next) {
|
function customLogger(req, res, next) {
|
||||||
// 过滤掉静态资源请求
|
// 过滤掉静态资源请求和图片代理请求
|
||||||
const isStaticResource = req.path.startsWith('/assets/') ||
|
const isStaticResource = req.path.startsWith('/assets/') ||
|
||||||
req.path.startsWith('/downloads/') ||
|
req.path.startsWith('/downloads/') ||
|
||||||
req.path.includes('.js') ||
|
req.path.includes('.js') ||
|
||||||
@@ -38,8 +38,11 @@ function customLogger(req, res, next) {
|
|||||||
req.path.includes('.ttf') ||
|
req.path.includes('.ttf') ||
|
||||||
req.path.includes('.eot');
|
req.path.includes('.eot');
|
||||||
|
|
||||||
// 只记录API请求和重要请求
|
// 过滤掉图片代理请求
|
||||||
if (!isStaticResource) {
|
const isImageProxy = req.path === '/api/proxy/image';
|
||||||
|
|
||||||
|
// 只记录API请求和重要请求,排除静态资源和图片代理
|
||||||
|
if (!isStaticResource && !isImageProxy) {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|
||||||
// 原始响应结束方法
|
// 原始响应结束方法
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ class ArtistService {
|
|||||||
const response = await this.makeRequest('GET', '/v1/user/detail', { user_id: artistId });
|
const response = await this.makeRequest('GET', '/v1/user/detail', { user_id: artistId });
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
||||||
success: true,
|
success: true,
|
||||||
data: response.user,
|
data: response.user,
|
||||||
};
|
};
|
||||||
@@ -41,7 +40,7 @@ class ArtistService {
|
|||||||
account: response.user.account,
|
account: response.user.account,
|
||||||
profile_image_urls: response.user.profile_image_urls,
|
profile_image_urls: response.user.profile_image_urls,
|
||||||
comment: response.user.comment,
|
comment: response.user.comment,
|
||||||
is_followed: response.user.is_followed || false
|
is_followed: response.user.is_followed || false,
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -72,9 +71,7 @@ class ArtistService {
|
|||||||
|
|
||||||
const response = await this.makeRequest('GET', `/v1/user/illusts?${stringify(params)}`);
|
const response = await this.makeRequest('GET', `/v1/user/illusts?${stringify(params)}`);
|
||||||
|
|
||||||
console.log('Artworks response keys:', Object.keys(response));
|
// 获取作者作品列表
|
||||||
console.log('Artworks count:', response.illusts?.length || 0);
|
|
||||||
console.log('Next URL:', response.next_url);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
@@ -337,7 +334,7 @@ class ArtistService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`发送API请求: ${method} ${endpoint}`);
|
// 发送API请求
|
||||||
const response = await axios(config);
|
const response = await axios(config);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
+51
-98
@@ -16,24 +16,19 @@ class ArtworkService {
|
|||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
include_user,
|
include_user,
|
||||||
include_series
|
include_series,
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await this.makeRequest(
|
const response = await this.makeRequest('GET', `/v1/illust/detail?${stringify(params)}`, { illust_id: artworkId });
|
||||||
'GET',
|
|
||||||
`/v1/illust/detail?${stringify(params)}`,
|
|
||||||
{ illust_id: artworkId }
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: response.illust
|
data: response.illust,
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,11 +38,7 @@ class ArtworkService {
|
|||||||
*/
|
*/
|
||||||
async getArtworkPreview(artworkId) {
|
async getArtworkPreview(artworkId) {
|
||||||
try {
|
try {
|
||||||
const response = await this.makeRequest(
|
const response = await this.makeRequest('GET', '/v1/illust/detail', { illust_id: artworkId });
|
||||||
'GET',
|
|
||||||
'/v1/illust/detail',
|
|
||||||
{ illust_id: artworkId }
|
|
||||||
);
|
|
||||||
|
|
||||||
const artwork = response.illust;
|
const artwork = response.illust;
|
||||||
|
|
||||||
@@ -59,7 +50,7 @@ class ArtworkService {
|
|||||||
user: {
|
user: {
|
||||||
id: artwork.user.id,
|
id: artwork.user.id,
|
||||||
name: artwork.user.name,
|
name: artwork.user.name,
|
||||||
account: artwork.user.account
|
account: artwork.user.account,
|
||||||
},
|
},
|
||||||
image_urls: artwork.image_urls,
|
image_urls: artwork.image_urls,
|
||||||
tags: artwork.tags.map(tag => tag.name),
|
tags: artwork.tags.map(tag => tag.name),
|
||||||
@@ -74,18 +65,17 @@ class ArtworkService {
|
|||||||
total_view: artwork.total_view,
|
total_view: artwork.total_view,
|
||||||
is_muted: artwork.is_muted,
|
is_muted: artwork.is_muted,
|
||||||
meta_single_page: artwork.meta_single_page,
|
meta_single_page: artwork.meta_single_page,
|
||||||
meta_pages: artwork.meta_pages
|
meta_pages: artwork.meta_pages,
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: preview
|
data: preview,
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,11 +85,7 @@ class ArtworkService {
|
|||||||
*/
|
*/
|
||||||
async getArtworkImages(artworkId, size = 'medium') {
|
async getArtworkImages(artworkId, size = 'medium') {
|
||||||
try {
|
try {
|
||||||
const response = await this.makeRequest(
|
const response = await this.makeRequest('GET', '/v1/illust/detail', { illust_id: artworkId });
|
||||||
'GET',
|
|
||||||
'/v1/illust/detail',
|
|
||||||
{ illust_id: artworkId }
|
|
||||||
);
|
|
||||||
|
|
||||||
const artwork = response.illust;
|
const artwork = response.illust;
|
||||||
const images = [];
|
const images = [];
|
||||||
@@ -111,7 +97,7 @@ class ArtworkService {
|
|||||||
original: artwork.meta_single_page.original_image_url,
|
original: artwork.meta_single_page.original_image_url,
|
||||||
large: artwork.meta_single_page.large_image_url,
|
large: artwork.meta_single_page.large_image_url,
|
||||||
medium: artwork.image_urls.medium,
|
medium: artwork.image_urls.medium,
|
||||||
square_medium: artwork.image_urls.square_medium
|
square_medium: artwork.image_urls.square_medium,
|
||||||
});
|
});
|
||||||
} else if (artwork.meta_pages && artwork.meta_pages.length > 0) {
|
} else if (artwork.meta_pages && artwork.meta_pages.length > 0) {
|
||||||
// 多页作品
|
// 多页作品
|
||||||
@@ -121,7 +107,7 @@ class ArtworkService {
|
|||||||
original: page.image_urls.original,
|
original: page.image_urls.original,
|
||||||
large: page.image_urls.large,
|
large: page.image_urls.large,
|
||||||
medium: page.image_urls.medium,
|
medium: page.image_urls.medium,
|
||||||
square_medium: page.image_urls.square_medium
|
square_medium: page.image_urls.square_medium,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -132,14 +118,13 @@ class ArtworkService {
|
|||||||
artwork_id: artworkId,
|
artwork_id: artworkId,
|
||||||
total_pages: artwork.page_count,
|
total_pages: artwork.page_count,
|
||||||
images: images,
|
images: images,
|
||||||
selected_size: size
|
selected_size: size,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -149,43 +134,35 @@ class ArtworkService {
|
|||||||
*/
|
*/
|
||||||
async searchArtworks(searchOptions) {
|
async searchArtworks(searchOptions) {
|
||||||
try {
|
try {
|
||||||
const {
|
const { keyword, tags, type = 'all', sort = 'date_desc', duration = 'all', offset = 0, limit = 30 } = searchOptions;
|
||||||
keyword,
|
|
||||||
tags,
|
|
||||||
type = 'all',
|
|
||||||
sort = 'date_desc',
|
|
||||||
duration = 'all',
|
|
||||||
offset = 0,
|
|
||||||
limit = 30
|
|
||||||
} = searchOptions;
|
|
||||||
|
|
||||||
// 验证搜索参数
|
// 验证搜索参数
|
||||||
if ((!keyword || keyword.trim() === '') && (!tags || tags.length === 0)) {
|
if ((!keyword || keyword.trim() === '') && (!tags || tags.length === 0)) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: 'Search keyword or tags are required'
|
error: 'Search keyword or tags are required',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 映射搜索参数到Pixiv API格式
|
// 映射搜索参数到Pixiv API格式
|
||||||
const searchTargetMap = {
|
const searchTargetMap = {
|
||||||
'all': 'partial_match_for_tags',
|
all: 'partial_match_for_tags',
|
||||||
'art': 'partial_match_for_tags',
|
art: 'partial_match_for_tags',
|
||||||
'manga': 'partial_match_for_tags',
|
manga: 'partial_match_for_tags',
|
||||||
'novel': 'partial_match_for_tags'
|
novel: 'partial_match_for_tags',
|
||||||
};
|
};
|
||||||
|
|
||||||
const sortMap = {
|
const sortMap = {
|
||||||
'date_desc': 'date_desc',
|
date_desc: 'date_desc',
|
||||||
'date_asc': 'date_asc',
|
date_asc: 'date_asc',
|
||||||
'popular_desc': 'popular_desc'
|
popular_desc: 'popular_desc',
|
||||||
};
|
};
|
||||||
|
|
||||||
const durationMap = {
|
const durationMap = {
|
||||||
'all': null, // 不传递duration参数表示全部时间
|
all: null, // 不传递duration参数表示全部时间
|
||||||
'within_last_day': 'within_last_day',
|
within_last_day: 'within_last_day',
|
||||||
'within_last_week': 'within_last_week',
|
within_last_week: 'within_last_week',
|
||||||
'within_last_month': 'within_last_month'
|
within_last_month: 'within_last_month',
|
||||||
};
|
};
|
||||||
|
|
||||||
// 构建搜索关键词
|
// 构建搜索关键词
|
||||||
@@ -202,7 +179,7 @@ class ArtworkService {
|
|||||||
search_target: searchTargetMap[type] || 'partial_match_for_tags',
|
search_target: searchTargetMap[type] || 'partial_match_for_tags',
|
||||||
sort: sortMap[sort] || 'date_desc',
|
sort: sortMap[sort] || 'date_desc',
|
||||||
offset: parseInt(offset) || 0,
|
offset: parseInt(offset) || 0,
|
||||||
filter: 'for_ios'
|
filter: 'for_ios',
|
||||||
};
|
};
|
||||||
|
|
||||||
// 只有当duration不是'all'时才添加duration参数
|
// 只有当duration不是'all'时才添加duration参数
|
||||||
@@ -210,12 +187,9 @@ class ArtworkService {
|
|||||||
params.duration = durationMap[duration];
|
params.duration = durationMap[duration];
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Search params:', params);
|
// 搜索参数已设置
|
||||||
|
|
||||||
const response = await this.makeRequest(
|
const response = await this.makeRequest('GET', `/v1/search/illust?${stringify(params)}`);
|
||||||
'GET',
|
|
||||||
`/v1/search/illust?${stringify(params)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
@@ -223,17 +197,16 @@ class ArtworkService {
|
|||||||
artworks: response.illusts || [],
|
artworks: response.illusts || [],
|
||||||
next_url: response.next_url,
|
next_url: response.next_url,
|
||||||
search_span_limit: response.search_span_limit,
|
search_span_limit: response.search_span_limit,
|
||||||
total: response.illusts ? response.illusts.length : 0
|
total: response.illusts ? response.illusts.length : 0,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Search error:', error.message);
|
console.error('Search error:', error.message);
|
||||||
console.error('Search error details:', error.response?.data);
|
console.error('Search error details:', error.response?.data);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message || 'Search failed'
|
error: error.message || 'Search failed',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -243,38 +216,29 @@ class ArtworkService {
|
|||||||
*/
|
*/
|
||||||
async getRecommendedArtworks(options = {}) {
|
async getRecommendedArtworks(options = {}) {
|
||||||
try {
|
try {
|
||||||
const {
|
const { offset = 0, limit = 30, include_ranking_illusts = true, include_privacy_policy = false } = options;
|
||||||
offset = 0,
|
|
||||||
limit = 30,
|
|
||||||
include_ranking_illusts = true,
|
|
||||||
include_privacy_policy = false
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
offset,
|
offset,
|
||||||
include_ranking_illusts,
|
include_ranking_illusts,
|
||||||
include_privacy_policy,
|
include_privacy_policy,
|
||||||
filter: 'for_ios'
|
filter: 'for_ios',
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await this.makeRequest(
|
const response = await this.makeRequest('GET', `/v1/illust/recommended?${stringify(params)}`);
|
||||||
'GET',
|
|
||||||
`/v1/illust/recommended?${stringify(params)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
artworks: response.illusts,
|
artworks: response.illusts,
|
||||||
next_url: response.next_url,
|
next_url: response.next_url,
|
||||||
ranking_illusts: response.ranking_illusts || []
|
ranking_illusts: response.ranking_illusts || [],
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -284,26 +248,17 @@ class ArtworkService {
|
|||||||
*/
|
*/
|
||||||
async getRankingArtworks(options = {}) {
|
async getRankingArtworks(options = {}) {
|
||||||
try {
|
try {
|
||||||
const {
|
const { mode = 'day', content = 'illust', filter = 'for_ios', offset = 0, limit = 30 } = options;
|
||||||
mode = 'day',
|
|
||||||
content = 'illust',
|
|
||||||
filter = 'for_ios',
|
|
||||||
offset = 0,
|
|
||||||
limit = 30
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
mode,
|
mode,
|
||||||
content,
|
content,
|
||||||
filter,
|
filter,
|
||||||
offset,
|
offset,
|
||||||
limit
|
limit,
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await this.makeRequest(
|
const response = await this.makeRequest('GET', `/v1/illust/ranking?${stringify(params)}`);
|
||||||
'GET',
|
|
||||||
`/v1/illust/ranking?${stringify(params)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
@@ -311,14 +266,13 @@ class ArtworkService {
|
|||||||
artworks: response.illusts,
|
artworks: response.illusts,
|
||||||
next_url: response.next_url,
|
next_url: response.next_url,
|
||||||
mode,
|
mode,
|
||||||
date: response.date
|
date: response.date,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -333,19 +287,19 @@ class ArtworkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
'Authorization': `Bearer ${this.auth.accessToken}`,
|
Authorization: `Bearer ${this.auth.accessToken}`,
|
||||||
'Accept-Language': 'en-us',
|
'Accept-Language': 'en-us',
|
||||||
'App-OS': 'android',
|
'App-OS': 'android',
|
||||||
'App-OS-Version': '9.0',
|
'App-OS-Version': '9.0',
|
||||||
'App-Version': '5.0.234',
|
'App-Version': '5.0.234',
|
||||||
'User-Agent': 'PixivAndroidApp/5.0.234 (Android 9.0; Pixel 3)'
|
'User-Agent': 'PixivAndroidApp/5.0.234 (Android 9.0; Pixel 3)',
|
||||||
};
|
};
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
method,
|
method,
|
||||||
url: `${this.baseURL}${endpoint}`,
|
url: `${this.baseURL}${endpoint}`,
|
||||||
headers,
|
headers,
|
||||||
timeout: 60000 // 增加到60秒
|
timeout: 60000, // 增加到60秒
|
||||||
};
|
};
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
@@ -356,8 +310,7 @@ class ArtworkService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Making request to: ${config.url}`);
|
// 发送API请求
|
||||||
console.log('Request config:', { method, endpoint, data });
|
|
||||||
|
|
||||||
const response = await axios(config);
|
const response = await axios(config);
|
||||||
return response.data;
|
return response.data;
|
||||||
@@ -367,7 +320,7 @@ class ArtworkService {
|
|||||||
endpoint,
|
endpoint,
|
||||||
error: error.message,
|
error: error.message,
|
||||||
status: error.response?.status,
|
status: error.response?.status,
|
||||||
data: error.response?.data
|
data: error.response?.data,
|
||||||
});
|
});
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,9 @@ class DownloadExecutor {
|
|||||||
|
|
||||||
const image = images[index];
|
const image = images[index];
|
||||||
const imageUrl = image[size] || image.original;
|
const imageUrl = image[size] || image.original;
|
||||||
const fileName = `${artwork.title || 'Untitled'}_${artwork.id}_${index + 1}${this.fileManager.getFileExtension(imageUrl)}`;
|
// 使用安全处理的标题创建文件名
|
||||||
|
const safeTitle = this.fileManager.createSafeDirectoryName(artwork.title || 'Untitled');
|
||||||
|
const fileName = `${safeTitle}_${artwork.id}_${index + 1}${this.fileManager.getFileExtension(imageUrl)}`;
|
||||||
const filePath = path.join(artworkDir, fileName);
|
const filePath = path.join(artworkDir, fileName);
|
||||||
|
|
||||||
// 如果文件已存在,跳过下载
|
// 如果文件已存在,跳过下载
|
||||||
@@ -51,6 +53,9 @@ class DownloadExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 确保目录存在
|
||||||
|
await this.fileManager.ensureDirectory(path.dirname(filePath));
|
||||||
|
|
||||||
await this.fileManager.downloadFile(imageUrl, filePath);
|
await this.fileManager.downloadFile(imageUrl, filePath);
|
||||||
|
|
||||||
task.completed_files++;
|
task.completed_files++;
|
||||||
@@ -61,7 +66,7 @@ class DownloadExecutor {
|
|||||||
results.push({ success: true, file: fileName });
|
results.push({ success: true, file: fileName });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
task.failed_files++;
|
task.failed_files++;
|
||||||
console.error(`下载图片失败 ${index + 1}:`, error.message);
|
console.error(`下载图片失败 ${index + 1}: ${error.message}`);
|
||||||
this.progressManager.notifyProgressUpdate(task.id, task);
|
this.progressManager.notifyProgressUpdate(task.id, task);
|
||||||
results.push({ success: false, error: error.message });
|
results.push({ success: false, error: error.message });
|
||||||
}
|
}
|
||||||
@@ -91,17 +96,10 @@ class DownloadExecutor {
|
|||||||
failed_files: task.failed_files,
|
failed_files: task.failed_files,
|
||||||
start_time: task.start_time,
|
start_time: task.start_time,
|
||||||
end_time: task.end_time,
|
end_time: task.end_time,
|
||||||
status: task.status
|
status: task.status,
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.historyManager.addHistoryItem(historyItem);
|
await this.historyManager.addHistoryItem(historyItem);
|
||||||
|
|
||||||
console.log('下载完成,历史记录已保存:', {
|
|
||||||
taskId: task.id,
|
|
||||||
historyLength: this.historyManager.history.length,
|
|
||||||
tasksCount: this.taskManager.tasks.size
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('异步下载执行失败:', error);
|
console.error('异步下载执行失败:', error);
|
||||||
task.status = 'failed';
|
task.status = 'failed';
|
||||||
@@ -128,7 +126,7 @@ class DownloadExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const batch = task.filtered_ids.slice(i, i + concurrent);
|
const batch = task.filtered_ids.slice(i, i + concurrent);
|
||||||
const batchPromises = batch.map(async (artworkId) => {
|
const batchPromises = batch.map(async artworkId => {
|
||||||
try {
|
try {
|
||||||
// 这里需要调用主下载服务的方法,暂时返回模拟结果
|
// 这里需要调用主下载服务的方法,暂时返回模拟结果
|
||||||
task.completed++;
|
task.completed++;
|
||||||
@@ -160,7 +158,6 @@ class DownloadExecutor {
|
|||||||
task.results = results;
|
task.results = results;
|
||||||
await this.taskManager.saveTasks();
|
await this.taskManager.saveTasks();
|
||||||
this.progressManager.notifyProgressUpdate(task.id, task);
|
this.progressManager.notifyProgressUpdate(task.id, task);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
task.status = 'failed';
|
task.status = 'failed';
|
||||||
task.error = error.message;
|
task.error = error.message;
|
||||||
@@ -186,7 +183,7 @@ class DownloadExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const batch = newArtworks.slice(i, i + maxConcurrent);
|
const batch = newArtworks.slice(i, i + maxConcurrent);
|
||||||
const batchPromises = batch.map(async (artwork) => {
|
const batchPromises = batch.map(async artwork => {
|
||||||
try {
|
try {
|
||||||
// 这里需要调用主下载服务的方法,暂时返回模拟结果
|
// 这里需要调用主下载服务的方法,暂时返回模拟结果
|
||||||
task.completed++;
|
task.completed++;
|
||||||
@@ -218,7 +215,6 @@ class DownloadExecutor {
|
|||||||
task.results = results;
|
task.results = results;
|
||||||
await this.taskManager.saveTasks();
|
await this.taskManager.saveTasks();
|
||||||
this.progressManager.notifyProgressUpdate(task.id, task);
|
this.progressManager.notifyProgressUpdate(task.id, task);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
task.status = 'failed';
|
task.status = 'failed';
|
||||||
task.error = error.message;
|
task.error = error.message;
|
||||||
@@ -244,7 +240,7 @@ class DownloadExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const batch = newArtworks.slice(i, i + maxConcurrent);
|
const batch = newArtworks.slice(i, i + maxConcurrent);
|
||||||
const batchPromises = batch.map(async (artwork) => {
|
const batchPromises = batch.map(async artwork => {
|
||||||
try {
|
try {
|
||||||
// 这里需要调用主下载服务的方法,暂时返回模拟结果
|
// 这里需要调用主下载服务的方法,暂时返回模拟结果
|
||||||
task.completed++;
|
task.completed++;
|
||||||
@@ -276,7 +272,6 @@ class DownloadExecutor {
|
|||||||
task.results = results;
|
task.results = results;
|
||||||
await this.taskManager.saveTasks();
|
await this.taskManager.saveTasks();
|
||||||
this.progressManager.notifyProgressUpdate(task.id, task);
|
this.progressManager.notifyProgressUpdate(task.id, task);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
task.status = 'failed';
|
task.status = 'failed';
|
||||||
task.error = error.message;
|
task.error = error.message;
|
||||||
|
|||||||
+54
-112
@@ -32,12 +32,7 @@ class DownloadService {
|
|||||||
this.taskManager = new TaskManager(this.dataPath);
|
this.taskManager = new TaskManager(this.dataPath);
|
||||||
this.progressManager = new ProgressManager();
|
this.progressManager = new ProgressManager();
|
||||||
this.historyManager = new HistoryManager(this.dataPath);
|
this.historyManager = new HistoryManager(this.dataPath);
|
||||||
this.downloadExecutor = new DownloadExecutor(
|
this.downloadExecutor = new DownloadExecutor(this.fileManager, this.taskManager, this.progressManager, this.historyManager);
|
||||||
this.fileManager,
|
|
||||||
this.taskManager,
|
|
||||||
this.progressManager,
|
|
||||||
this.historyManager
|
|
||||||
);
|
|
||||||
|
|
||||||
this.initialized = false;
|
this.initialized = false;
|
||||||
}
|
}
|
||||||
@@ -57,7 +52,7 @@ class DownloadService {
|
|||||||
await this.historyManager.init();
|
await this.historyManager.init();
|
||||||
|
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
console.log('下载服务初始化完成,下载路径:', downloadPath);
|
// 下载服务初始化完成
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('下载服务初始化失败:', error);
|
console.error('下载服务初始化失败:', error);
|
||||||
this.initialized = false;
|
this.initialized = false;
|
||||||
@@ -90,14 +85,14 @@ class DownloadService {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: task
|
data: task,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllTasks() {
|
async getAllTasks() {
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: this.taskManager.getAllTasks()
|
data: this.taskManager.getAllTasks(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +104,7 @@ class DownloadService {
|
|||||||
|
|
||||||
await this.taskManager.updateTask(taskId, {
|
await this.taskManager.updateTask(taskId, {
|
||||||
status: 'cancelled',
|
status: 'cancelled',
|
||||||
end_time: new Date()
|
end_time: new Date(),
|
||||||
});
|
});
|
||||||
|
|
||||||
this.progressManager.notifyProgressUpdate(taskId, task);
|
this.progressManager.notifyProgressUpdate(taskId, task);
|
||||||
@@ -149,7 +144,7 @@ class DownloadService {
|
|||||||
const result = this.historyManager.getDownloadHistory(offset, limit);
|
const result = this.historyManager.getDownloadHistory(offset, limit);
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: result
|
data: result,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,9 +168,7 @@ class DownloadService {
|
|||||||
|
|
||||||
if (artworkStat.exists && artworkStat.isDirectory) {
|
if (artworkStat.exists && artworkStat.isDirectory) {
|
||||||
const artworkFiles = await this.fileManager.listDirectory(artworkPath);
|
const artworkFiles = await this.fileManager.listDirectory(artworkPath);
|
||||||
const imageFiles = artworkFiles.filter(file =>
|
const imageFiles = artworkFiles.filter(file => /\.(jpg|jpeg|png|gif|webp)$/i.test(file));
|
||||||
/\.(jpg|jpeg|png|gif|webp)$/i.test(file)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (imageFiles.length > 0) {
|
if (imageFiles.length > 0) {
|
||||||
files.push({
|
files.push({
|
||||||
@@ -184,7 +177,7 @@ class DownloadService {
|
|||||||
path: artworkPath,
|
path: artworkPath,
|
||||||
files: imageFiles,
|
files: imageFiles,
|
||||||
total_size: await this.fileManager.getDirectorySize(artworkPath),
|
total_size: await this.fileManager.getDirectorySize(artworkPath),
|
||||||
created_at: artworkStat.created
|
created_at: artworkStat.created,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,9 +219,7 @@ class DownloadService {
|
|||||||
|
|
||||||
if (artworkStat.exists && artworkStat.isDirectory) {
|
if (artworkStat.exists && artworkStat.isDirectory) {
|
||||||
const files = await this.fileManager.listDirectory(artworkPath);
|
const files = await this.fileManager.listDirectory(artworkPath);
|
||||||
const imageFiles = files.filter(file =>
|
const imageFiles = files.filter(file => /\.(jpg|jpeg|png|gif|webp)$/i.test(file));
|
||||||
/\.(jpg|jpeg|png|gif|webp)$/i.test(file)
|
|
||||||
);
|
|
||||||
if (imageFiles.length > 0) {
|
if (imageFiles.length > 0) {
|
||||||
downloadedIds.add(parseInt(artworkId));
|
downloadedIds.add(parseInt(artworkId));
|
||||||
}
|
}
|
||||||
@@ -247,13 +238,10 @@ class DownloadService {
|
|||||||
|
|
||||||
async isArtworkDownloaded(artworkId) {
|
async isArtworkDownloaded(artworkId) {
|
||||||
try {
|
try {
|
||||||
console.log(`开始检查作品 ${artworkId} 的下载状态...`);
|
|
||||||
const downloadPath = await this.fileManager.getDownloadPath();
|
const downloadPath = await this.fileManager.getDownloadPath();
|
||||||
console.log(`下载路径: ${downloadPath}`);
|
|
||||||
|
|
||||||
// 扫描所有作者目录
|
// 扫描所有作者目录
|
||||||
const artistEntries = await this.fileManager.listDirectory(downloadPath);
|
const artistEntries = await this.fileManager.listDirectory(downloadPath);
|
||||||
console.log(`找到 ${artistEntries.length} 个作者目录`);
|
|
||||||
|
|
||||||
for (const artistEntry of artistEntries) {
|
for (const artistEntry of artistEntries) {
|
||||||
const artistPath = path.join(downloadPath, artistEntry);
|
const artistPath = path.join(downloadPath, artistEntry);
|
||||||
@@ -268,27 +256,19 @@ class DownloadService {
|
|||||||
// 检查是否是目标作品目录(包含数字ID)
|
// 检查是否是目标作品目录(包含数字ID)
|
||||||
const artworkMatch = artworkEntry.match(/^(\d+)_(.+)$/);
|
const artworkMatch = artworkEntry.match(/^(\d+)_(.+)$/);
|
||||||
if (artworkMatch && artworkMatch[1] === artworkId.toString()) {
|
if (artworkMatch && artworkMatch[1] === artworkId.toString()) {
|
||||||
console.log(`找到作品目录: ${artworkEntry}`);
|
|
||||||
const artworkPath = path.join(artistPath, artworkEntry);
|
const artworkPath = path.join(artistPath, artworkEntry);
|
||||||
|
|
||||||
// 检查作品信息文件
|
// 检查作品信息文件
|
||||||
const infoPath = path.join(artworkPath, 'artwork_info.json');
|
const infoPath = path.join(artworkPath, 'artwork_info.json');
|
||||||
if (!await this.fileManager.fileExists(infoPath)) {
|
if (!(await this.fileManager.fileExists(infoPath))) {
|
||||||
console.log(`作品信息文件不存在: ${infoPath}`);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查图片文件
|
// 检查图片文件
|
||||||
const files = await this.fileManager.listDirectory(artworkPath);
|
const files = await this.fileManager.listDirectory(artworkPath);
|
||||||
const imageFiles = files.filter(file =>
|
const imageFiles = files.filter(file => /\.(jpg|jpeg|png|gif|webp)$/i.test(file) && file !== 'artwork_info.json');
|
||||||
/\.(jpg|jpeg|png|gif|webp)$/i.test(file) &&
|
|
||||||
file !== 'artwork_info.json'
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(`找到 ${imageFiles.length} 个图片文件`);
|
|
||||||
|
|
||||||
if (imageFiles.length === 0) {
|
if (imageFiles.length === 0) {
|
||||||
console.log(`没有找到图片文件`);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,18 +277,15 @@ class DownloadService {
|
|||||||
const imagePath = path.join(artworkPath, imageFile);
|
const imagePath = path.join(artworkPath, imageFile);
|
||||||
const integrity = await this.fileManager.checkFileIntegrity(imagePath);
|
const integrity = await this.fileManager.checkFileIntegrity(imagePath);
|
||||||
if (!integrity.valid) {
|
if (!integrity.valid) {
|
||||||
console.log(`作品 ${artworkId} 的文件 ${imageFile} 不完整: ${integrity.reason}`);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`作品 ${artworkId} 已完整下载`);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`作品 ${artworkId} 未找到`);
|
|
||||||
return false;
|
return false;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('检查作品下载状态失败:', error);
|
console.error('检查作品下载状态失败:', error);
|
||||||
@@ -324,19 +301,16 @@ class DownloadService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 检查是否已下载
|
// 检查是否已下载
|
||||||
if (skipExisting && await this.isArtworkDownloaded(artworkId)) {
|
if (skipExisting && (await this.isArtworkDownloaded(artworkId))) {
|
||||||
console.log(`作品 ${artworkId} 已存在且完整,跳过下载`);
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
task_id: null,
|
task_id: null,
|
||||||
artwork_id: artworkId,
|
artwork_id: artworkId,
|
||||||
skipped: true,
|
skipped: true,
|
||||||
message: '作品已存在且完整,跳过下载'
|
message: '作品已存在且完整,跳过下载',
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
} else if (skipExisting) {
|
|
||||||
console.log(`作品 ${artworkId} 目录存在但不完整,将重新下载`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取作品信息
|
// 获取作品信息
|
||||||
@@ -362,7 +336,7 @@ class DownloadService {
|
|||||||
const artworkDir = path.join(artistDir, artworkDirName);
|
const artworkDir = path.join(artistDir, artworkDirName);
|
||||||
|
|
||||||
// 如果是重新下载,先删除现有目录
|
// 如果是重新下载,先删除现有目录
|
||||||
if (!skipExisting && await this.fileManager.directoryExists(artworkDir)) {
|
if (!skipExisting && (await this.fileManager.directoryExists(artworkDir))) {
|
||||||
console.log(`删除现有作品目录: ${artworkDir}`);
|
console.log(`删除现有作品目录: ${artworkDir}`);
|
||||||
await this.fileManager.removeDirectory(artworkDir);
|
await this.fileManager.removeDirectory(artworkDir);
|
||||||
}
|
}
|
||||||
@@ -384,7 +358,7 @@ class DownloadService {
|
|||||||
artwork_title: artworkTitle,
|
artwork_title: artworkTitle,
|
||||||
total_files: images.length,
|
total_files: images.length,
|
||||||
completed_files: 0,
|
completed_files: 0,
|
||||||
failed_files: 0
|
failed_files: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.taskManager.saveTasks();
|
await this.taskManager.saveTasks();
|
||||||
@@ -400,15 +374,14 @@ class DownloadService {
|
|||||||
artist_name: artistName,
|
artist_name: artistName,
|
||||||
artwork_title: artworkTitle,
|
artwork_title: artworkTitle,
|
||||||
status: 'downloading',
|
status: 'downloading',
|
||||||
message: '下载任务已创建,正在后台执行'
|
message: '下载任务已创建,正在后台执行',
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('下载作品失败:', error);
|
console.error('下载作品失败:', error);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -430,8 +403,6 @@ class DownloadService {
|
|||||||
|
|
||||||
filteredIds = artworkIds.filter(id => !downloadedSet.has(id));
|
filteredIds = artworkIds.filter(id => !downloadedSet.has(id));
|
||||||
skippedCount = artworkIds.length - filteredIds.length;
|
skippedCount = artworkIds.length - filteredIds.length;
|
||||||
|
|
||||||
console.log(`批量下载: 总共 ${artworkIds.length} 个作品,跳过 ${skippedCount} 个已下载的作品,需要下载 ${filteredIds.length} 个作品`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建任务记录
|
// 创建任务记录
|
||||||
@@ -442,7 +413,7 @@ class DownloadService {
|
|||||||
completed: 0,
|
completed: 0,
|
||||||
failed: 0,
|
failed: 0,
|
||||||
skipped: skippedCount,
|
skipped: skippedCount,
|
||||||
results: []
|
results: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.taskManager.saveTasks();
|
await this.taskManager.saveTasks();
|
||||||
@@ -451,7 +422,7 @@ class DownloadService {
|
|||||||
if (filteredIds.length === 0) {
|
if (filteredIds.length === 0) {
|
||||||
await this.taskManager.updateTask(task.id, {
|
await this.taskManager.updateTask(task.id, {
|
||||||
status: 'completed',
|
status: 'completed',
|
||||||
end_time: new Date()
|
end_time: new Date(),
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -462,8 +433,8 @@ class DownloadService {
|
|||||||
completed_artworks: 0,
|
completed_artworks: 0,
|
||||||
failed_artworks: 0,
|
failed_artworks: 0,
|
||||||
skipped_artworks: skippedCount,
|
skipped_artworks: skippedCount,
|
||||||
message: '所有作品都已下载完成'
|
message: '所有作品都已下载完成',
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -477,15 +448,14 @@ class DownloadService {
|
|||||||
total_artworks: task.total,
|
total_artworks: task.total,
|
||||||
completed_artworks: task.completed,
|
completed_artworks: task.completed,
|
||||||
failed_artworks: task.failed,
|
failed_artworks: task.failed,
|
||||||
message: '批量下载任务已创建,正在后台执行'
|
message: '批量下载任务已创建,正在后台执行',
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('批量下载失败:', error);
|
console.error('批量下载失败:', error);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -494,16 +464,7 @@ class DownloadService {
|
|||||||
* 下载作者作品
|
* 下载作者作品
|
||||||
*/
|
*/
|
||||||
async downloadArtistArtworks(artistId, options = {}) {
|
async downloadArtistArtworks(artistId, options = {}) {
|
||||||
const {
|
const { type = 'art', limit = 50, size = 'original', quality = 'high', format = 'auto', skipExisting = true, maxConcurrent = 3, pageSize = 30 } = options;
|
||||||
type = 'art',
|
|
||||||
limit = 50,
|
|
||||||
size = 'original',
|
|
||||||
quality = 'high',
|
|
||||||
format = 'auto',
|
|
||||||
skipExisting = true,
|
|
||||||
maxConcurrent = 3,
|
|
||||||
pageSize = 30
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 创建任务记录
|
// 创建任务记录
|
||||||
@@ -513,7 +474,7 @@ class DownloadService {
|
|||||||
completed: 0,
|
completed: 0,
|
||||||
failed: 0,
|
failed: 0,
|
||||||
skipped: 0,
|
skipped: 0,
|
||||||
results: []
|
results: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.taskManager.saveTasks();
|
await this.taskManager.saveTasks();
|
||||||
@@ -531,7 +492,7 @@ class DownloadService {
|
|||||||
const artworksResult = await this.artistService.getArtistArtworks(artistId, {
|
const artworksResult = await this.artistService.getArtistArtworks(artistId, {
|
||||||
type,
|
type,
|
||||||
offset: offset,
|
offset: offset,
|
||||||
limit: Math.min(pageSize, limit - allArtworks.length)
|
limit: Math.min(pageSize, limit - allArtworks.length),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!artworksResult.success) {
|
if (!artworksResult.success) {
|
||||||
@@ -554,24 +515,22 @@ class DownloadService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 过滤已下载的作品
|
// 过滤已下载的作品
|
||||||
const newArtworks = skipExisting
|
const newArtworks = skipExisting ? allArtworks.filter(artwork => !downloadedSet.has(artwork.id)) : allArtworks;
|
||||||
? allArtworks.filter(artwork => !downloadedSet.has(artwork.id))
|
|
||||||
: allArtworks;
|
|
||||||
|
|
||||||
const skippedCount = allArtworks.length - newArtworks.length;
|
const skippedCount = allArtworks.length - newArtworks.length;
|
||||||
|
|
||||||
await this.taskManager.updateTask(task.id, {
|
await this.taskManager.updateTask(task.id, {
|
||||||
skipped: skippedCount,
|
skipped: skippedCount,
|
||||||
total: newArtworks.length
|
total: newArtworks.length,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`作者作品下载: 总共 ${allArtworks.length} 个作品,跳过 ${skippedCount} 个已下载的作品,需要下载 ${newArtworks.length} 个作品`);
|
// 作者作品下载统计
|
||||||
|
|
||||||
// 如果没有需要下载的作品,直接返回
|
// 如果没有需要下载的作品,直接返回
|
||||||
if (newArtworks.length === 0) {
|
if (newArtworks.length === 0) {
|
||||||
await this.taskManager.updateTask(task.id, {
|
await this.taskManager.updateTask(task.id, {
|
||||||
status: 'completed',
|
status: 'completed',
|
||||||
end_time: new Date()
|
end_time: new Date(),
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -583,8 +542,8 @@ class DownloadService {
|
|||||||
completed_artworks: 0,
|
completed_artworks: 0,
|
||||||
failed_artworks: 0,
|
failed_artworks: 0,
|
||||||
skipped_artworks: skippedCount,
|
skipped_artworks: skippedCount,
|
||||||
message: '所有作品都已下载完成'
|
message: '所有作品都已下载完成',
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -599,15 +558,14 @@ class DownloadService {
|
|||||||
total_artworks: task.total,
|
total_artworks: task.total,
|
||||||
completed_artworks: task.completed,
|
completed_artworks: task.completed,
|
||||||
failed_artworks: task.failed,
|
failed_artworks: task.failed,
|
||||||
message: '作者作品下载任务已创建,正在后台执行'
|
message: '作者作品下载任务已创建,正在后台执行',
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('作者作品下载失败:', error);
|
console.error('作者作品下载失败:', error);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -616,17 +574,7 @@ class DownloadService {
|
|||||||
* 下载排行榜作品
|
* 下载排行榜作品
|
||||||
*/
|
*/
|
||||||
async downloadRankingArtworks(options = {}) {
|
async downloadRankingArtworks(options = {}) {
|
||||||
const {
|
const { mode = 'day', type = 'art', limit = 50, size = 'original', quality = 'high', format = 'auto', skipExisting = true, maxConcurrent = 3, pageSize = 30 } = options;
|
||||||
mode = 'day',
|
|
||||||
type = 'art',
|
|
||||||
limit = 50,
|
|
||||||
size = 'original',
|
|
||||||
quality = 'high',
|
|
||||||
format = 'auto',
|
|
||||||
skipExisting = true,
|
|
||||||
maxConcurrent = 3,
|
|
||||||
pageSize = 30
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 创建任务记录
|
// 创建任务记录
|
||||||
@@ -637,7 +585,7 @@ class DownloadService {
|
|||||||
completed: 0,
|
completed: 0,
|
||||||
failed: 0,
|
failed: 0,
|
||||||
skipped: 0,
|
skipped: 0,
|
||||||
results: []
|
results: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.taskManager.saveTasks();
|
await this.taskManager.saveTasks();
|
||||||
@@ -654,7 +602,7 @@ class DownloadService {
|
|||||||
while (hasMore && allArtworks.length < limit) {
|
while (hasMore && allArtworks.length < limit) {
|
||||||
const rankingResult = await this.getRankingArtworks(mode, type, {
|
const rankingResult = await this.getRankingArtworks(mode, type, {
|
||||||
offset: offset,
|
offset: offset,
|
||||||
limit: Math.min(pageSize, limit - allArtworks.length)
|
limit: Math.min(pageSize, limit - allArtworks.length),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!rankingResult.success) {
|
if (!rankingResult.success) {
|
||||||
@@ -677,24 +625,22 @@ class DownloadService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 过滤已下载的作品
|
// 过滤已下载的作品
|
||||||
const newArtworks = skipExisting
|
const newArtworks = skipExisting ? allArtworks.filter(artwork => !downloadedSet.has(artwork.id)) : allArtworks;
|
||||||
? allArtworks.filter(artwork => !downloadedSet.has(artwork.id))
|
|
||||||
: allArtworks;
|
|
||||||
|
|
||||||
const skippedCount = allArtworks.length - newArtworks.length;
|
const skippedCount = allArtworks.length - newArtworks.length;
|
||||||
|
|
||||||
await this.taskManager.updateTask(task.id, {
|
await this.taskManager.updateTask(task.id, {
|
||||||
skipped: skippedCount,
|
skipped: skippedCount,
|
||||||
total: newArtworks.length
|
total: newArtworks.length,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`排行榜作品下载: 总共 ${allArtworks.length} 个作品,跳过 ${skippedCount} 个已下载的作品,需要下载 ${newArtworks.length} 个作品`);
|
// 排行榜作品下载统计
|
||||||
|
|
||||||
// 如果没有需要下载的作品,直接返回
|
// 如果没有需要下载的作品,直接返回
|
||||||
if (newArtworks.length === 0) {
|
if (newArtworks.length === 0) {
|
||||||
await this.taskManager.updateTask(task.id, {
|
await this.taskManager.updateTask(task.id, {
|
||||||
status: 'completed',
|
status: 'completed',
|
||||||
end_time: new Date()
|
end_time: new Date(),
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -707,8 +653,8 @@ class DownloadService {
|
|||||||
completed_artworks: 0,
|
completed_artworks: 0,
|
||||||
failed_artworks: 0,
|
failed_artworks: 0,
|
||||||
skipped_artworks: skippedCount,
|
skipped_artworks: skippedCount,
|
||||||
message: '所有作品都已下载完成'
|
message: '所有作品都已下载完成',
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -724,15 +670,14 @@ class DownloadService {
|
|||||||
total_artworks: task.total,
|
total_artworks: task.total,
|
||||||
completed_artworks: task.completed,
|
completed_artworks: task.completed,
|
||||||
failed_artworks: task.failed,
|
failed_artworks: task.failed,
|
||||||
message: '排行榜作品下载任务已创建,正在后台执行'
|
message: '排行榜作品下载任务已创建,正在后台执行',
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('排行榜作品下载失败:', error);
|
console.error('排行榜作品下载失败:', error);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -751,27 +696,24 @@ class DownloadService {
|
|||||||
mode,
|
mode,
|
||||||
content: type,
|
content: type,
|
||||||
offset,
|
offset,
|
||||||
limit
|
limit,
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
artworks: result.artworks,
|
artworks: result.artworks,
|
||||||
next_url: result.next_url || null
|
next_url: result.next_url || null,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取排行榜失败:', error);
|
console.error('获取排行榜失败:', error);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = DownloadService;
|
module.exports = DownloadService;
|
||||||
@@ -3,6 +3,7 @@ const fs = require('fs-extra');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const ConfigManager = require('../config/config-manager');
|
const ConfigManager = require('../config/config-manager');
|
||||||
|
const FileUtils = require('../utils/file-utils');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件管理器 - 负责文件下载、检查和目录管理
|
* 文件管理器 - 负责文件下载、检查和目录管理
|
||||||
@@ -107,9 +108,13 @@ class FileManager {
|
|||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
writer.on('finish', resolve);
|
writer.on('finish', resolve);
|
||||||
writer.on('error', (error) => {
|
writer.on('error', async (error) => {
|
||||||
// 下载失败时删除文件
|
// 下载失败时删除文件
|
||||||
fs.unlink(filePath, () => {});
|
try {
|
||||||
|
await this.safeDeleteFile(filePath);
|
||||||
|
} catch (removeError) {
|
||||||
|
console.warn('清理失败文件时出错:', removeError.message);
|
||||||
|
}
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -149,14 +154,35 @@ class FileManager {
|
|||||||
* 创建安全的目录名
|
* 创建安全的目录名
|
||||||
*/
|
*/
|
||||||
createSafeDirectoryName(name) {
|
createSafeDirectoryName(name) {
|
||||||
return name.replace(/[<>:"/\\|?*]/g, '_');
|
if (!name) return 'Untitled';
|
||||||
|
|
||||||
|
// 移除或替换Windows文件系统不允许的字符
|
||||||
|
let safeName = name.replace(/[<>:"/\\|?*]/g, '_');
|
||||||
|
|
||||||
|
// 移除前后空格和点
|
||||||
|
safeName = safeName.trim().replace(/^\.+|\.+$/g, '');
|
||||||
|
|
||||||
|
// 如果处理后为空,使用默认名称
|
||||||
|
if (!safeName) {
|
||||||
|
safeName = 'Untitled';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 限制长度,避免路径过长
|
||||||
|
if (safeName.length > 100) {
|
||||||
|
safeName = safeName.substring(0, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
return safeName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 确保目录存在
|
* 确保目录存在
|
||||||
*/
|
*/
|
||||||
async ensureDirectory(dirPath) {
|
async ensureDirectory(dirPath) {
|
||||||
await fs.ensureDir(dirPath);
|
const success = await FileUtils.safeEnsureDir(dirPath);
|
||||||
|
if (!success) {
|
||||||
|
throw new Error(`目录创建失败: ${dirPath}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -204,11 +230,23 @@ class FileManager {
|
|||||||
* 删除文件
|
* 删除文件
|
||||||
*/
|
*/
|
||||||
async deleteFile(filePath) {
|
async deleteFile(filePath) {
|
||||||
if (await fs.pathExists(filePath)) {
|
try {
|
||||||
await fs.unlink(filePath);
|
if (await fs.pathExists(filePath)) {
|
||||||
|
await fs.unlink(filePath);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`文件删除失败: ${filePath}`, error.message);
|
||||||
|
// 不抛出错误,避免影响其他操作
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 安全删除文件(兼容 pkg 打包)
|
||||||
|
*/
|
||||||
|
async safeDeleteFile(filePath) {
|
||||||
|
return await FileUtils.safeDeleteFile(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查文件是否存在
|
* 检查文件是否存在
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class HistoryManager {
|
|||||||
await fs.ensureDir(this.dataPath);
|
await fs.ensureDir(this.dataPath);
|
||||||
await this.loadHistory();
|
await this.loadHistory();
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
console.log('历史记录管理器初始化完成');
|
// 历史记录管理器初始化完成
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('历史记录管理器初始化失败:', error);
|
console.error('历史记录管理器初始化失败:', error);
|
||||||
this.initialized = false;
|
this.initialized = false;
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class TaskManager {
|
|||||||
await fs.ensureDir(this.dataPath);
|
await fs.ensureDir(this.dataPath);
|
||||||
await this.loadTasks();
|
await this.loadTasks();
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
console.log('任务管理器初始化完成');
|
// 任务管理器初始化完成
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('任务管理器初始化失败:', error);
|
console.error('任务管理器初始化失败:', error);
|
||||||
this.initialized = false;
|
this.initialized = false;
|
||||||
@@ -75,7 +75,7 @@ class TaskManager {
|
|||||||
start_time: new Date(),
|
start_time: new Date(),
|
||||||
end_time: null,
|
end_time: null,
|
||||||
error: null,
|
error: null,
|
||||||
...data
|
...data,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.tasks.set(taskId, task);
|
this.tasks.set(taskId, task);
|
||||||
@@ -144,7 +144,7 @@ class TaskManager {
|
|||||||
|
|
||||||
if (cleanedCount > 0) {
|
if (cleanedCount > 0) {
|
||||||
await this.saveTasks();
|
await this.saveTasks();
|
||||||
console.log(`清理了 ${cleanedCount} 个已完成的任务`);
|
// 清理了已完成的任务
|
||||||
}
|
}
|
||||||
|
|
||||||
return cleanedCount;
|
return cleanedCount;
|
||||||
@@ -161,7 +161,7 @@ class TaskManager {
|
|||||||
completed: 0,
|
completed: 0,
|
||||||
failed: 0,
|
failed: 0,
|
||||||
cancelled: 0,
|
cancelled: 0,
|
||||||
partial: 0
|
partial: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const task of this.tasks.values()) {
|
for (const task of this.tasks.values()) {
|
||||||
|
|||||||
@@ -0,0 +1,134 @@
|
|||||||
|
const fs = require('fs-extra');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件操作工具类 - 确保与 pkg 打包兼容
|
||||||
|
*/
|
||||||
|
class FileUtils {
|
||||||
|
/**
|
||||||
|
* 安全删除文件(兼容 pkg 打包)
|
||||||
|
*/
|
||||||
|
static async safeDeleteFile(filePath) {
|
||||||
|
try {
|
||||||
|
// 首先尝试使用 fs-extra
|
||||||
|
if (await fs.pathExists(filePath)) {
|
||||||
|
await fs.remove(filePath);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
try {
|
||||||
|
// 降级到原生 fs
|
||||||
|
const nativeFs = require('fs').promises;
|
||||||
|
await nativeFs.unlink(filePath);
|
||||||
|
return true;
|
||||||
|
} catch (nativeError) {
|
||||||
|
console.error(`文件删除失败: ${filePath}`, nativeError.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 安全创建目录(兼容 pkg 打包)
|
||||||
|
*/
|
||||||
|
static async safeEnsureDir(dirPath) {
|
||||||
|
try {
|
||||||
|
// 首先尝试使用 fs-extra
|
||||||
|
await fs.ensureDir(dirPath);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
try {
|
||||||
|
// 降级到原生 fs
|
||||||
|
const nativeFs = require('fs').promises;
|
||||||
|
await nativeFs.mkdir(dirPath, { recursive: true });
|
||||||
|
return true;
|
||||||
|
} catch (nativeError) {
|
||||||
|
console.error(`目录创建失败: ${dirPath}`, nativeError.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 安全检查文件是否存在(兼容 pkg 打包)
|
||||||
|
*/
|
||||||
|
static async safePathExists(filePath) {
|
||||||
|
try {
|
||||||
|
// 首先尝试使用 fs-extra
|
||||||
|
return await fs.pathExists(filePath);
|
||||||
|
} catch (error) {
|
||||||
|
try {
|
||||||
|
// 降级到原生 fs
|
||||||
|
const nativeFs = require('fs').promises;
|
||||||
|
await nativeFs.access(filePath);
|
||||||
|
return true;
|
||||||
|
} catch (nativeError) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 安全读取目录(兼容 pkg 打包)
|
||||||
|
*/
|
||||||
|
static async safeReadDir(dirPath) {
|
||||||
|
try {
|
||||||
|
// 首先尝试使用 fs-extra
|
||||||
|
return await fs.readdir(dirPath);
|
||||||
|
} catch (error) {
|
||||||
|
try {
|
||||||
|
// 降级到原生 fs
|
||||||
|
const nativeFs = require('fs').promises;
|
||||||
|
return await nativeFs.readdir(dirPath);
|
||||||
|
} catch (nativeError) {
|
||||||
|
console.error(`读取目录失败: ${dirPath}`, nativeError.message);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 安全写入 JSON 文件(兼容 pkg 打包)
|
||||||
|
*/
|
||||||
|
static async safeWriteJson(filePath, data, options = {}) {
|
||||||
|
try {
|
||||||
|
// 首先尝试使用 fs-extra
|
||||||
|
await fs.writeJson(filePath, data, options);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
try {
|
||||||
|
// 降级到原生 fs
|
||||||
|
const nativeFs = require('fs').promises;
|
||||||
|
const jsonString = JSON.stringify(data, null, options.spaces || 2);
|
||||||
|
await nativeFs.writeFile(filePath, jsonString, 'utf8');
|
||||||
|
return true;
|
||||||
|
} catch (nativeError) {
|
||||||
|
console.error(`JSON 写入失败: ${filePath}`, nativeError.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测是否在 pkg 打包环境中运行
|
||||||
|
*/
|
||||||
|
static isPkgEnvironment() {
|
||||||
|
return process.pkg !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前运行环境信息
|
||||||
|
*/
|
||||||
|
static getEnvironmentInfo() {
|
||||||
|
return {
|
||||||
|
isPkg: this.isPkgEnvironment(),
|
||||||
|
nodeVersion: process.version,
|
||||||
|
platform: process.platform,
|
||||||
|
arch: process.arch,
|
||||||
|
pkgVersion: process.pkg ? process.pkg.version : null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = FileUtils;
|
||||||
Reference in New Issue
Block a user