gs-download
Version:
Downloader of Genius Scan PDFs files
57 lines • 2.85 kB
JavaScript
import logger from '../utils/logger.js';
export default class DownloadManager {
constructor(progressManager, downloadService) {
this.progressManager = progressManager;
this.downloadService = downloadService;
}
async downloadFilesFromJson(jsonFileUrl, baseUrl, outputDir = '.') {
if (!jsonFileUrl) {
const error = new Error('JSON file URL is required');
logger.error('Download process failed: JSON file URL is missing');
throw error;
}
if (!baseUrl) {
const error = new Error('Base URL is required');
logger.error('Download process failed: Base URL is missing');
throw error;
}
logger.info(`Starting download process from JSON file: ${jsonFileUrl}`);
logger.info(`Using base URL: ${baseUrl}`);
logger.info(`Files will be saved to: ${outputDir}`);
try {
const fileList = await this.downloadService.retrieveFileList(jsonFileUrl);
logger.info(`Retrieved ${fileList.length} files from JSON`);
if (fileList.length === 0) {
logger.warn('No files found in the JSON file');
return [];
}
this.progressManager.create('Total', fileList.length);
const downloadedFiles = [];
for (let i = 0; i < fileList.length; i++) {
const fileInfo = fileList[i];
logger.info(`Processing file: ${fileInfo === null || fileInfo === void 0 ? void 0 : fileInfo.name} (${fileInfo === null || fileInfo === void 0 ? void 0 : fileInfo.uid})`);
try {
const filePath = await this.downloadService.downloadFile(fileInfo, baseUrl, outputDir);
downloadedFiles.push(filePath);
logger.info(`Successfully downloaded: ${fileInfo === null || fileInfo === void 0 ? void 0 : fileInfo.name}`);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.error(`Failed to download file ${fileInfo === null || fileInfo === void 0 ? void 0 : fileInfo.name}: ${errorMessage}`);
}
finally {
this.progressManager.update('Total');
}
}
this.progressManager.stop();
logger.info(`Download process completed. Successfully downloaded ${downloadedFiles.length} of ${fileList.length} files`);
return downloadedFiles;
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.error(`Download process failed: ${errorMessage}`);
throw error;
}
}
}
//# sourceMappingURL=download-manager.js.map