Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File download? #85

Open
spotUP opened this issue Feb 19, 2021 · 8 comments
Open

File download? #85

spotUP opened this issue Feb 19, 2021 · 8 comments

Comments

@spotUP
Copy link

spotUP commented Feb 19, 2021

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

Is support for file download planned?

@tenox7
Copy link
Owner

tenox7 commented Feb 22, 2021

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

@tenox7
Copy link
Owner

tenox7 commented Oct 19, 2021

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

@Distortions81
Copy link

Distortions81 commented May 18, 2022

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
Copy link
Owner

tenox7 commented May 18, 2022

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

@andresvettori
Copy link

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.TotalBytes
100.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
Copy link

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

@tenox7
Copy link
Owner

tenox7 commented Oct 17, 2022

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
Copy link
Owner

tenox7 commented Dec 8, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants