Skip to content

Commit

Permalink
add maxRedirects MFlocal
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed May 1, 2024
1 parent 685eab5 commit 393ee6a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 61 deletions.
38 changes: 10 additions & 28 deletions adapters/mediafile/default/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,36 +140,18 @@ class LocalMediaFileAdapter extends MediaFileAdapter_1.default {
const fullPathDl = path.join(prefix, loadMediaFilesProcess.name.origin);
// Check if file exists
if (!fs.existsSync(fullPathDl)) {
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}`);
}
const response = await axios_1.default.get(loadMediaFilesProcess.url, { responseType: 'stream', maxRedirects: 5 });
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);
});
}
else {
console.log(`File ${fullPathDl} already exists. Skipping download.`);
sails.log.silly(`File ${fullPathDl} already exists. Skipping download.`);
}
}
async loadMediaFiles() {
Expand Down
47 changes: 14 additions & 33 deletions adapters/mediafile/default/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,42 +161,23 @@ 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)) {
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}`);
}
const response = await axios.get(loadMediaFilesProcess.url, { responseType: 'stream', maxRedirects: 5});
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);
});
} else {
console.log(`File ${fullPathDl} already exists. Skipping download.`);
sails.log.silly(`File ${fullPathDl} already exists. Skipping download.`);
}
}

Expand Down

0 comments on commit 393ee6a

Please sign in to comment.