From 777a39b3504616bf4a4a4b7f197174768df69154 Mon Sep 17 00:00:00 2001 From: Xziy Date: Wed, 1 May 2024 09:11:26 +0300 Subject: [PATCH] to download redirec --- adapters/mediafile/default/local.js | 38 ++++++++++++++++------ adapters/mediafile/default/local.ts | 49 ++++++++++++++++++++--------- 2 files changed, 62 insertions(+), 25 deletions(-) diff --git a/adapters/mediafile/default/local.js b/adapters/mediafile/default/local.js index a635048c..41129aa5 100644 --- a/adapters/mediafile/default/local.js +++ b/adapters/mediafile/default/local.js @@ -136,18 +136,36 @@ class LocalMediaFileAdapter extends MediaFileAdapter_1.default { const fullPathDl = path.join(prefix, loadMediaFilesProcess.name.origin); // Check if file exists if (!fs.existsSync(fullPathDl)) { - const response = await axios_1.default.get(loadMediaFilesProcess.url, { responseType: 'stream' }); - sails.log.silly(`MF local > download image: ${fullPathDl}, status: ${response.status}`); - fs.mkdirSync(prefix, { recursive: true }); - const writer = fs.createWriteStream(fullPathDl); - response.data.pipe(writer); - await new Promise((resolve, reject) => { - writer.on('finish', resolve); - writer.on('error', reject); - }); + try { + const response = await axios_1.default.get(loadMediaFilesProcess.url, { responseType: 'stream', maxRedirects: 0 }); + if (response.status === 302 && response.headers.location) { + // Redirect detected, follow the new URL + const redirectedResponse = await axios_1.default.get(response.headers.location, { responseType: 'stream' }); + fs.mkdirSync(prefix, { recursive: true }); + const writer = fs.createWriteStream(fullPathDl); + redirectedResponse.data.pipe(writer); + await new Promise((resolve, reject) => { + writer.on('finish', resolve); + writer.on('error', reject); + }); + } + else { + // No redirect, save the data directly + fs.mkdirSync(prefix, { recursive: true }); + const writer = fs.createWriteStream(fullPathDl); + response.data.pipe(writer); + await new Promise((resolve, reject) => { + writer.on('finish', resolve); + writer.on('error', reject); + }); + } + } + catch (error) { + console.error(`Error downloading file: ${error}`); + } } else { - sails.log.silly(`File ${fullPathDl} already exists. Skipping download.`); + console.log(`File ${fullPathDl} already exists. Skipping download.`); } } async loadMediaFiles() { diff --git a/adapters/mediafile/default/local.ts b/adapters/mediafile/default/local.ts index 099e4990..47f5f8e8 100644 --- a/adapters/mediafile/default/local.ts +++ b/adapters/mediafile/default/local.ts @@ -151,30 +151,49 @@ export default class LocalMediaFileAdapter extends MediaFileAdapter { } } + protected async download(loadMediaFilesProcess: LoadMediaFilesProcess): Promise { const prefix = this.getPrefix(loadMediaFilesProcess.type); const fullPathDl = path.join(prefix, loadMediaFilesProcess.name.origin); - + // Check if file exists if (!fs.existsSync(fullPathDl)) { - const response = await axios.get(loadMediaFilesProcess.url, { responseType: 'stream' }); - sails.log.silly(`MF local > download image: ${fullPathDl}, status: ${response.status}`); - - fs.mkdirSync(prefix, { recursive: true }); - - const writer = fs.createWriteStream(fullPathDl); - response.data.pipe(writer); - - await new Promise((resolve, reject) => { - writer.on('finish', resolve); - writer.on('error', reject); - }); + try { + const response = await axios.get(loadMediaFilesProcess.url, { responseType: 'stream', maxRedirects: 0 }); + + if (response.status === 302 && response.headers.location) { + // Redirect detected, follow the new URL + const redirectedResponse = await axios.get(response.headers.location, { responseType: 'stream' }); + + fs.mkdirSync(prefix, { recursive: true }); + + const writer = fs.createWriteStream(fullPathDl); + redirectedResponse.data.pipe(writer); + + await new Promise((resolve, reject) => { + writer.on('finish', resolve); + writer.on('error', reject); + }); + } else { + // No redirect, save the data directly + fs.mkdirSync(prefix, { recursive: true }); + + const writer = fs.createWriteStream(fullPathDl); + response.data.pipe(writer); + + await new Promise((resolve, reject) => { + writer.on('finish', resolve); + writer.on('error', reject); + }); + } + } catch (error) { + console.error(`Error downloading file: ${error}`); + } } else { - sails.log.silly(`File ${fullPathDl} already exists. Skipping download.`); + console.log(`File ${fullPathDl} already exists. Skipping download.`); } } - private async loadMediaFiles() { if (this.processing) { return