|
4 | 4 | * @module core/DownloadManager |
5 | 5 | * @license MIT |
6 | 6 | * @author ivanvit |
7 | | - * @version 1.0.0 |
| 7 | + * @version 1.0.1 |
8 | 8 | */ |
9 | 9 |
|
10 | 10 | 'use strict'; |
|
118 | 118 |
|
119 | 119 | downloadState.chapters = chapters; |
120 | 120 |
|
121 | | - const chapterContents = await this.downloadChapters( |
122 | | - service, |
123 | | - downloadState, |
124 | | - chapters, |
125 | | - onProgress |
126 | | - ); |
| 121 | + const MAX_CHAPTERS_PER_FILE = serviceKey === 'mangalib' ? 80 : Infinity; |
| 122 | + |
| 123 | + if (chapters.length > MAX_CHAPTERS_PER_FILE) { |
| 124 | + const totalParts = Math.ceil(chapters.length / MAX_CHAPTERS_PER_FILE); |
| 125 | + |
| 126 | + for (let partIndex = 0; partIndex < totalParts; partIndex++) { |
| 127 | + await downloadState.controller.waitIfPaused(); |
| 128 | + if (downloadState.controller.shouldStop()) break; |
| 129 | + |
| 130 | + const startIdx = partIndex * MAX_CHAPTERS_PER_FILE; |
| 131 | + const endIdx = Math.min((partIndex + 1) * MAX_CHAPTERS_PER_FILE, chapters.length); |
| 132 | + const chaptersForPart = chapters.slice(startIdx, endIdx); |
| 133 | + |
| 134 | + this.updateStatus(downloadId, `Часть ${partIndex + 1}/${totalParts}: Загрузка глав ${startIdx + 1}-${endIdx}...`, 10); |
| 135 | + |
| 136 | + const chapterContents = await this.downloadChapters( |
| 137 | + service, |
| 138 | + downloadState, |
| 139 | + chaptersForPart, |
| 140 | + onProgress, |
| 141 | + startIdx, |
| 142 | + chapters.length |
| 143 | + ); |
| 144 | + |
| 145 | + this.updateStatus(downloadId, `Часть ${partIndex + 1}/${totalParts}: Создание ${format.toUpperCase()}...`, 90); |
| 146 | + const exporter = global.ExporterFactory.create(format); |
| 147 | + |
| 148 | + const partSuffix = ` (Часть ${partIndex + 1} из ${totalParts})` |
| 149 | + const mangaWithSuffix = { ...manga, rus_name: (manga.rus_name || manga.name) + partSuffix }; |
| 150 | + |
| 151 | + const file = await exporter.export(mangaWithSuffix, chapterContents, coverBase64); |
| 152 | + |
| 153 | + await this.saveFile(file.blob, file.filename); |
| 154 | + |
| 155 | + downloadState.chapterContents = []; |
| 156 | + } |
| 157 | + |
| 158 | + this.updateStatus(downloadId, 'Готово!', 100); |
| 159 | + this.eventBus.emit('download:completed', downloadState); |
| 160 | + |
| 161 | + return { success: true, downloadId }; |
| 162 | + } else { |
| 163 | + const chapterContents = await this.downloadChapters( |
| 164 | + service, |
| 165 | + downloadState, |
| 166 | + chapters, |
| 167 | + onProgress |
| 168 | + ); |
127 | 169 |
|
128 | | - this.updateStatus(downloadId, `Создание ${format.toUpperCase()}...`, 95); |
129 | | - const exporter = global.ExporterFactory.create(format); |
130 | | - const file = await exporter.export(manga, chapterContents, coverBase64); |
| 170 | + this.updateStatus(downloadId, `Создание ${format.toUpperCase()}...`, 95); |
| 171 | + const exporter = global.ExporterFactory.create(format); |
| 172 | + const file = await exporter.export(manga, chapterContents, coverBase64); |
131 | 173 |
|
132 | | - await this.saveFile(file.blob, file.filename); |
| 174 | + await this.saveFile(file.blob, file.filename); |
133 | 175 |
|
134 | | - this.updateStatus(downloadId, 'Готово!', 100); |
135 | | - this.eventBus.emit('download:completed', downloadState); |
136 | | - |
137 | | - return { success: true, downloadId }; |
| 176 | + this.updateStatus(downloadId, 'Готово!', 100); |
| 177 | + this.eventBus.emit('download:completed', downloadState); |
| 178 | + |
| 179 | + return { success: true, downloadId }; |
| 180 | + } |
138 | 181 |
|
139 | 182 | } catch (error) { |
140 | 183 | console.error('[DownloadManager] Error:', error); |
|
420 | 463 | }; |
421 | 464 | } |
422 | 465 |
|
423 | | - async downloadChapters(service, downloadState, chapters, onProgress) { |
| 466 | + async downloadChapters(service, downloadState, chapters, onProgress, startIndex = 0, totalChapters = null) { |
424 | 467 | const results = []; |
425 | | - const total = chapters.length; |
| 468 | + const total = totalChapters || chapters.length; |
426 | 469 |
|
427 | | - for (let i = 0; i < total; i++) { |
| 470 | + for (let i = 0; i < chapters.length; i++) { |
428 | 471 | await downloadState.controller.waitIfPaused(); |
429 | 472 | if (downloadState.controller.shouldStop()) break; |
430 | 473 |
|
431 | | - downloadState.currentChapterIndex = i; |
| 474 | + downloadState.currentChapterIndex = startIndex + i; |
432 | 475 |
|
433 | 476 | const chapter = chapters[i]; |
434 | | - const progress = Math.floor((i / total) * 80) + 10; |
| 477 | + const globalIndex = startIndex + i; |
| 478 | + const progress = Math.floor((globalIndex / total) * 80) + 10; |
435 | 479 |
|
436 | 480 | this.updateStatus( |
437 | 481 | downloadState.id, |
438 | | - `Глава ${i + 1}/${total}: ${chapter.name || chapter.number}`, |
| 482 | + `Глава ${globalIndex + 1}/${total}: ${chapter.name || chapter.number}`, |
439 | 483 | progress |
440 | 484 | ); |
441 | 485 |
|
|
0 commit comments