Fetching a file hosted on Filebrowser via Private Network

So I currently have a file that I am hosting using Filebrowser template. The file has a download link that can be accessed via public URL without any problem. But whenever I try to download the file from another service in the same project using internal private network, the file couldn't seem to be downloaded. What I did was that I simply changed the public URL into the private one, like so: http://heroncopier-download.railway.internal:3000/api/public/dl/xxxxx/xxxxx.zip As you can see in my above example, I did already changed the https into http and I also added the port after the internal URL. Does anyone have any clue on how to solve this? Thanks!
No description
No description
Solution:
Yes, it works really well. In case anyone would like to use the same approach here's the code snippet for express/nodejs: ```...
Jump to solution
11 Replies
Percy
Percy3mo ago
Project ID: a764e147-c28b-4388-85d2-547de92d2fd0
Celengan Babi
Celengan Babi3mo ago
a764e147-c28b-4388-85d2-547de92d2fd0
Brody
Brody3mo ago
please provide any errors you are getting
Celengan Babi
Celengan Babi3mo ago
Hi @Brody, nevermind. I got mixed up between the native fetch() function and the one that is provided by node-fetch npm package. This post gave me the clue: https://stackoverflow.com/a/74611064
Stack Overflow
Npm - fetch vs node-fetch?
These package names are pretty confusing, they seem like they do the same thing yet 'fetch' looks to be abandoned yet not marked as deprecated (last commit 3 years ago). Judging from the download c...
Brody
Brody3mo ago
So you where able to successfully download the file over the private network?
Solution
Celengan Babi
Celengan Babi3mo ago
Yes, it works really well. In case anyone would like to use the same approach here's the code snippet for express/nodejs:
const url = 'http://xxxxx.railway.internal:3000/api/public/dl/C_izechY/data/xxxx.zip'; // This is the URL of a file hosted on Railway internal network using Filebrowser template (make sure to share the file first and set long expiry date)
const response = await fetch(url);

if (!response.ok) {
res.sendStatus(500);
return;
}

// Set headers
res.setHeader('Content-Type', response.headers.get('Content-Type'));
res.setHeader('Content-Length', response.headers.get('Content-Length'));
res.setHeader('Content-Disposition', 'attachment; filename=xxxx.zip'); // replace with your desired filename

// Pipe the response stream directly to res
response.body.pipe(res);
const url = 'http://xxxxx.railway.internal:3000/api/public/dl/C_izechY/data/xxxx.zip'; // This is the URL of a file hosted on Railway internal network using Filebrowser template (make sure to share the file first and set long expiry date)
const response = await fetch(url);

if (!response.ok) {
res.sendStatus(500);
return;
}

// Set headers
res.setHeader('Content-Type', response.headers.get('Content-Type'));
res.setHeader('Content-Length', response.headers.get('Content-Length'));
res.setHeader('Content-Disposition', 'attachment; filename=xxxx.zip'); // replace with your desired filename

// Pipe the response stream directly to res
response.body.pipe(res);
Above code lets your server fetches the file from other service in the same project via Railway's private network. You can also install npm package such as express-rate-limit to prevent anyone from abusing the download endpoint (so not to let them waste your egress bills, just make sure to setup x-forwarded-for header config properly on the limiter) Using this approach, the file can only be downloaded via endpoint that you can control. It is not maybe state-of-the-art solution but it works for my use-case for now.
Brody
Brody3mo ago
i might even go as far as to turn http://xxxxx.railway.internal:3000/api/public/dl/C_izechY/data/xxxx.zip into an environment variable like DOWNLOAD_URL just in case you need to change it then you wont have to edit code
Celengan Babi
Celengan Babi3mo ago
oh true, that's a neat approach. I'll do that. so at least whenever i need to change something, I don't have to push any changes in the code 👍
Brody
Brody3mo ago
exactly
Celengan Babi
Celengan Babi3mo ago
Thank you Brody!
Brody
Brody3mo ago
no problem!