withSuggestedFilename method
- dynamic unique = false,
- Future<
DownloadTask> taskWithFilenameBuilder(- DownloadTask task,
- Map<
String, String> headers, - bool unique
Returns a copy of the task with the Task.filename
property changed
to the filename suggested by the server, or derived from the url, or
unchanged.
If unique
is true, the filename is guaranteed not to already exist. This
is accomplished by adding a suffix to the suggested filename with a number,
e.g. "data (2).txt"
If a taskWithFilenameBuilder
is supplied, this is the function called to
convert the task, response headers and unique
values into a new DownloadTask
with the suggested filename. By default, taskWithSuggestedFilename
is used,
which parses the Content-Disposition according to RFC6266, or takes the last
path segment of the URL, or leaves the filename unchanged
The suggested filename is obtained by making a HEAD request to the url represented by the DownloadTask, including urlQueryParameters and headers
Implementation
Future<DownloadTask> withSuggestedFilename(
{unique = false,
Future<DownloadTask> Function(
DownloadTask task, Map<String, String> headers, bool unique)
taskWithFilenameBuilder = taskWithSuggestedFilename}) async {
try {
final response = await DesktopDownloader.httpClient
.head(Uri.parse(url), headers: headers);
if ([200, 201, 202, 203, 204, 205, 206].contains(response.statusCode)) {
return taskWithFilenameBuilder(this, response.headers, unique);
}
} catch (e) {
_log.finer('Error connecting to server');
}
return taskWithFilenameBuilder(this, {}, unique);
}