[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[ecf-dev] File name of retrieved file
|
When obtaining a file from a remote source it is often good to know the
intended filename. It might be used in progress reporting and sometimes
also as a source of information when naming the resulting local file.
The URL in itself is sometimes very cryptic and the path might contain a
name in some numeric form (calculated UUID perhaps). In some cases, the
path doesn't contain a name at all (ends with download.php for instance)
and the actual path is hidden in one of the parameters. When using HTTP,
the returned "Content-Disposition" response header field is often a much
better source for the filename the the actual URL.
Using ECF, I cannot obtain this header field and that's OK. There's a
lot of transfer implementations where use of response headers isn't
applicable. I would however like a
IIncomingFileTransfer.getRemoteFileName() method. The HTTP transfer
could use the Content-Disposition and other file transfer implementation
could do a best effort based on whatever algorithm that would be
appropriate for them. I'm attaching the code that Buckminster uses to
extract the file name from the Content-Disposition header below.
What do you think? Could something like this be make available in ECF
incoming file transfer?
Regards,
Thomas Hallgren
/**
* This regular expression is a simple Content-Disposition header
parser.
* Content-Disposition grammar is quite complex, this is really
simplified.
* It should be refactored in future versions using proper grammar.
*/
private final static Pattern s_contentDispositionPattern =
Pattern.compile(
".*;\\s*filename\\s*=\\s*(\"(?:[^\"\\\\]|\\\\.)*\"|[^;\"\\s]+)\\s*(?:;|$)");
private static String parseContentDisposition(String contentDisposition)
{
//Context-Dispositon syntax:
attachment|inline[;filename="<filename>"]
//Try to extract the filename form it (and strip quotes if
they're there)
if (contentDisposition == null)
return null;
String filename = null;
Matcher m = s_contentDispositionPattern.matcher(contentDisposition);
if (m.matches()) {
filename = m.group(1);
if (filename.startsWith("\"") && filename.endsWith("\"")) {
filename = filename.substring(1,
filename.length()-1).replaceAll("\\\\(.)", "$1");
}
}
return filename;
}