Skip to content

🎥 Make videos programmatically with React

follow redirects while downloading assets

Jonny Burger697465318de9
diff --git a/packages/renderer/src/assets/read-file.ts b/packages/renderer/src/assets/read-file.ts
index 545857a72a..89ee401dab 100644
--- a/packages/renderer/src/assets/read-file.ts
+++ b/packages/renderer/src/assets/read-file.ts
@@ -13,7 +13,9 @@ const getClient = (url: string) => {
throw new Error('Can only download URLs starting with http:// or https://');
};
-export const readFile = (url: string): Promise<http.IncomingMessage> => {
+const readFileWithoutRedirect = (
+ url: string
+): Promise<http.IncomingMessage> => {
return new Promise<http.IncomingMessage>((resolve, reject) => {
getClient(url)(url, (res) => {
resolve(res);
@@ -22,3 +24,25 @@ export const readFile = (url: string): Promise<http.IncomingMessage> => {
});
});
};
+
+export const readFile = async (
+ url: string,
+ redirectsSoFar = 0
+): Promise<http.IncomingMessage> => {
+ if (redirectsSoFar > 10) {
+ throw new Error(`Too many redirects while downloading ${url}`);
+ }
+
+ const file = await readFileWithoutRedirect(url);
+ if (file.statusCode === 302 || file.statusCode === 301) {
+ if (!file.headers.location) {
+ throw new Error(
+ `Received a status code ${file.statusCode} but no "Location" header while calling ${file.headers.location}`
+ );
+ }
+
+ return readFile(file.headers.location, redirectsSoFar + 1);
+ }
+
+ return file;
+};