tenox7 / wrp

Web Rendering Proxy: Use vintage, historical, legacy browsers on modern web
Apache License 2.0
1.08k stars 52 forks source link

File download? #85

Open spotUP opened 3 years ago

spotUP commented 3 years ago

Awesome project! Can’t wait to try it on my Amiga!

Is support for file download planned?

tenox7 commented 3 years ago

Yes it's planned but I do not have any estimates yet. Lets keep the bug open.

tenox7 commented 3 years ago

https://github.com/rusq/chromedl for an example

Distortions81 commented 2 years ago

I second this. If I find time, I might look at how hard it would be to add. This is so perfect for my old machines, just need download.

tenox7 commented 2 years ago

Agreed. I will take a look. WRP fell behind on development a little bit. I need to get back to this.

andresvettori commented 2 years ago

I try to give it a look and understand how it all works and where some code should be added to handle a download file scenario. This is kind of hard because there is no easy way for WRP to know a file should be downloaded as a response for the user interaction. The only way I came up was to add some code in the "action" function where WRP handles the user click and sends an "MouseClickXY" action to chromedp. If the object below the cursor happens to be a download link then I guess Chrome should initiate the file download. At this point the code should block to wait for the download to finish (and obtain the file location & name) and then write back its contents to the client browser with all the proper headers (content-type, content-disposition and others).

The way I though we have to detect if a download have been initiated by some user interaction is to hook up the proper events from chromedp and use them to create and destroy a "chan" object to block.

So, I tried adding the following code inside "action" function:

Inside the IF block for mouse clicks: if w.mouseX > 0 && w.mouseY > 0 {

I added the code to detect file download has completed, could add another for WillBeginDownload I guess:

chromedp.ListenTarget(ctx, func(v interface{}) { if ev, ok := v.(browser.EventDownloadProgress); ok { completed := "(unknown)" if ev.TotalBytes != 0 { completed = fmt.Sprintf("%0.2f%%", ev.ReceivedBytes/ev.TotalBytes100.0) } log.Printf("state: %s, completed: %s\n", ev.State.String(), completed) if ev.State == browser.DownloadProgressStateCompleted { done <- ev.GUID close(done) } } })

Also added this code right below this to enable file downloads:

    browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllowAndName).
        WithDownloadPath(wd).
        WithEventsEnabled(true).Do(ctx)

And the after this the call to MouseClickXY is made.

But the download doesn't never occurs, and don't know what I'm doing wrong (I have another project with this code and the download does happen). I have also replaced the MouseClickXY with a direct call to click the HTML anchor I know points to the download link I want to trigger (var rv = chromedp.Click(//a[text()="Download"], chromedp.BySearch)) but with same results.

One additional thing, I had to update the chromedp reference to a newer version (0.85) to support these newer sample code.

Any thoughs?

Agv

TheTechRobo commented 2 years ago

Some files will also specify that they should be downloaded in the headers, so that needs to be checked for too.

tenox7 commented 2 years ago

I will take a look. To be honest update of chromedp to newer version stopped me from doing any recent updates to wrp. I have started updating but never finished as too many things broke. Let me see if I can retry the process.

tenox7 commented 1 year ago

There is some interesting discussion going on here: https://github.com/chromedp/chromedp/issues/388 with an example here https://github.com/chromedp/examples/blob/master/download_file/main.go - so maybe?