How to extract filename from response-content-disposition parameter? (Url.downloadToFile)


#1

Hi all,

I’m having an issue saving files that I’m downloading from Amazon S3. On S3, files are stored at random URLs (e.g. https://sb-browser-test.s3.amazonaws.com/A6nDk8C5U6ST5hYbos4qcurr). The original names of these files are available in the response-content-disposition parameter of the HTTP request.

So, essentially, I have a URL that looks like this:

URL url("https://sb-browser-test.s3.amazonaws.com/A6nDk8C5U6ST5hYbos4qcurr?response-content-disposition=inline%3B+filename%3D%22AMS-NL2-9.9.flac%22%3B+filename*%3DUTF-8%27%27AMS-NL2-9.9.flac&response-content-type=audio%2Fflac");

… and I want to save the contents to a file named AMS-NL2-9.9.flac.

I know I can extract the response-content-disposition with url.getParameterValues(). The extracted parameter value looks like this

inline; filename="AMS-NL2-9.9.flac"; filename*=UTF-8''AMS-NL2-9.9.flac

My question is: how do I get the filename ( AMS-NL2-9.9.flac ) out of this parameter value? Does anyone know a reliable method to extract the filename from the string above? Does JUCE have a built-in function to do this? Or would I be better off writing my own Regexp, or looking in another library, like Boost? Any advice is most welcome.

Thanks,

Dave


#2

I think StringArray::fromTokens() should get you started:

auto pairs = StringArray::fromTokens (url.getParameterValues(), ";", "\"");
for (auto& p : pairs)
    if (p.startsWith ("filename="))
        return p.substring (9);

Does that work?
Probably there is some more refinement, like getting rid of quotes around the filename etc…


#3

This looks very promising - I didn’t know about JUCE’s StringArray::fromTokens. Thanks!