Skip to content

Commit

Permalink
to download redirec
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed May 1, 2024
1 parent c41dca4 commit 777a39b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 25 deletions.
38 changes: 28 additions & 10 deletions adapters/mediafile/default/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
49 changes: 34 additions & 15 deletions adapters/mediafile/default/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,30 +151,49 @@ export default class LocalMediaFileAdapter extends MediaFileAdapter {
}
}


protected async download(loadMediaFilesProcess: LoadMediaFilesProcess): Promise<void> {
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
Expand Down

0 comments on commit 777a39b

Please sign in to comment.