residuum/PuRestJson

binary downloads

Closed this issue · 6 comments

Hi. We are using PuRESTJson for the Freesound API:
https://github.com/g-roma/freesound_pd

Unfortunately we still need to resort to [shell] + curl for downloading files, which means it won't work on windows. Handling binary downloads (e.g. to file) in PuRESTJson would be a great improvement.

I have started thinking about the most idiomatic way to implement that feature, and have come to no conclusion yet, so maybe the Pd-dev mailinglist can help:
http://lists.puredata.info/pipermail/pd-dev/2013-07/019585.html

Thinking about it some more, I came up with following design:

  • I will implement another message [folder path_to_folder(, that will let the user set a download folder. If the download folder is not set, then data will be output as now. Cleaning the download folder will be possible with an empty [folder( message. That way, no MIME type sniffing will be necessary, and text data can be downloaded as well.
  • After a successful download, the right outlet will output the path to the file, probable as another parameter after bang.
  • I am unsure whether to overwrite existing files, and what to do, if the URL points to a directory and returns either the index.html, Default.aspx, directory listing etc.
  • I am also unsure what to do about binary uploads for POST or PUT requests.

Would this approach work for you?

I have begun implementing binary downloads in a seperate branch: https://github.com/residuum/PuRestJson/tree/binary

Please try building the binary and try the example from examples/binary-test.pd. The [folder( message for now does not set the folder, but the path, where the downloaded file will be stored.

I tried and it worked. Then I set folder to an actual "folder" and it crashed. The name could be misleading. Maybe it could be called "file"? The approach would wok for me I think, but I'd really need to set the name of the downloaded file, because the URL for downloading sounds does not contain the file name.
With respect to existing files it could be an option like in curl ... then I guess you could try to support text files (including json files), but this can also be done in pd. Looking at the content-type and content-disposition would be handy, but I guess it's more work. Binary uploads are not supported in Freesound, but could be used for uploading to soundcloud for example. You can use the same flag but in reverse, if the user sends a POST or PUT with the "file" or "folder" flag set, you upload the file (i.e. --data-binary in curl)

It currently is just a dummy implementation, just to see if downloads work. The method will implement downloads to a folder and create the folder, if it does not exist.

Binary uploads will be implement independently, later.

I have merged the implementation to the master branch.

Downloading should work now: set the location by using the new [write( message with the file location as a parameter. To clear the location and output data to the left output again, send an empty write message to the object again.