totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

Download file with F.routeDownload #685

Closed fgnm closed 5 years ago

fgnm commented 5 years ago

Hello, I'm trying to download files from an external site, in particular from my Amazon S3 where all static files are stored (i.e. pdfs), this is the controller:

exports.install = function () {
    FILE('/download/file', download_file, ['.pdf', 'authorize']);
}

function download_file(req, res) {
    var filename = U.getName(req.url);
    console.log(F.routeDownload('pdfs/' + filename));
    res.file(F.routeDownload('pdfs/' + filename));
}

Then I've set static_url_download in config file with the right s3 path.

Console output the right s3 link (I can open it correctly with browser), but the page output 404: http://127.0.0.1:8080/download/file/test_file.pdf.

If I move the file inside public total.js folder res.file works without problem. What I've wrong? Is there something I should enable to allow total send amazon s3 files? Thank you

petersirka commented 5 years ago

If you want to read a file outside of public directory then you can use ~ for example ~/absolute/path/. Then it will work :-)

Please use Total.js Messenger for small questions.

fgnm commented 5 years ago

Thank you for your answer, but I think that the problem is quite more complex then it appears. I do not want read just outside the public directory, I need to sand in response a path that is different from the local domain. Amazon s3 links are usually: https://s3.eu-west-3.amazonaws.com/elasticbeanstalk-eu-west-3-XXXX/static/pdfs/test_file.pdf, so the ~ isn't the solution. I've made this workaround

exports.install = function () {
    ROUTE('/download/file/*', download_file, ['authorize']);
}
function download_contract() {
    var self = this;

    var filename = U.getName(self.req.url);
    self.redirect(F.routeDownload('pdfs/' + filename + ".pdf"));
}

I've switched to normal ROUTE instead of FILE, but I don't think this is the best solution anyway.

petersirka commented 5 years ago

This is a bit complicated. Try something like this:

function download_contract() {  
    var self = this;
    var filename = U.getName(self.req.url);
    U.download(F.routeDownload('pdfs/' + filename + '.pdf'), ['get'], function(err, response) {
        if (err)
            self.throw404(err);
        else
            self.stream(response.headers['content-type'], response);
    });
}
fgnm commented 5 years ago

Many Many Thanks! This is exactly what I'm looking for, S3 url is finally hidden :)