Use server suggested filename when downloading #169
Replies: 4 comments 5 replies
-
I would suggest to make the file name nullable. I know that is a breaking change which you want to avoid, but you get a very clear compile error that you might need to add question marks. There is basically no migration path required for that, mentioning this is of cause always helpful. My assumption for this suggestion is, that when you don't set the filename then you don't care about it in first place. You just want to have that file. Regarding mutable vs. immutable you have already a state so this is already mutable, when I understood you correctly. You could add Your suggestion of using |
Beta Was this translation helpful? Give feedback.
-
Instead of all the complications, I suggest leaving it as is, because of all the extra getters being added and then it changing midway etc, can get complicated for the user. IMO documentation should be added for filename which states that the filename property will be null at first (unless provided by the user), and upon completion of download, the filename will be set to a random string or the suggested filename if using Although it has this issue: final task = DownloadTask(url: '....');
await FileDownloader().download(task);
task.filename; // filename will be the same upon download finish unless mutated by ".download()" And I also suggest using Then there can be a function of Either way, it does look like there will be some breaking changes. |
Beta Was this translation helpful? Give feedback.
-
Thank both for your reply. @rekire the filename currently is nullable and per your comment "when you don't set the filename then you don't care about it in first place, you just want to have that file." a null filename creates a random one instantly, so the The introduction of '?' as a special filename case gives you the option to get the filename at the time of download instead of trying to obtain it beforehand using Both of you have commented that the approach introduces extra complexity - which is true - and that it would be preferable to set the filename in the original task once obtained from the server. The latter is also true, but impossible: the I've implemented this (along with a few other changes) now for Android and Desktop on the I'd love for you to try it out. Here's how I have descibed it in the readme. |
Beta Was this translation helpful? Give feedback.
-
Implemented in V7.11.0 |
Beta Was this translation helpful? Give feedback.
-
Issue
At the moment (V7.10) the downloader requires the
filename
field in aDownloadTask
to be set to a valid filename for tasks that areenqueued
(or downloaded). Upon request (issue #37) we've added a methodwithSuggestedFilename
to theDownloadTask
class that does a HEAD request to the server to get the Content-Disposition and uses that to create a copy of theDownloadTask
with the filename set to the server suggested filename.This approach works ok, but is not elegant (adds a server request when the Content-Disposition would also be available on download), is complex (see issue #145) and does not handle errors well (issue #167).
Ideally, we offer an option to tell the downloader that the filename needs to be taken from the server upon download. The reason I've hesitated with that is that thus far all fields in a task are essentially
final
, so theTask
object you get back from the downloader in status and progress updates is essentially the same object as the one you fed in (i.e. all fields are the same). That makes a lot of things easier - eg you can use either the one passed to you or the one you stored when callingdownload
. The only field that can be different isretriesRemaining
and that is an 'internal' field, only supposed to be used by the downloader itself to manage retries. If we create aDownloadTask
that does not have a validfilename
and get one passed to us with a validfilename
then this can lead to confusion.Proposal
That said, I think it's worth accepting this issue for this special case. What I am proposing is:
filename
field is set to '?' this indicates that you want the downloader to create that filename based on the server responsetask
passed to you in aTaskStatusUpdate
or aTaskProgressUpdate
will have itsfilename
field set to a proper filename as soon as possible.The condition as soon as possible is important, because, for example, when you get the status update
TaskStatus.enqueued
the filename will still not be known. Similarly, I am concerned that on iOS we may not be able to determine the filename with confidence until the download has completed.You should therefore use the filename field only when it is no longer '?', for which we'll introduce a getter on
DownloadTask
:bool get hasFilename => filename != '?';
Alternatives
task
passed to you in aTaskStatusUpdate
or aTaskProgressUpdate
is to add a fieldfilename
toTaskStatusUpdate
orTaskProgressUpdate
, but I think that is more confusing (because now there are two places where the filename is stored) and doesn't add much.hasFilename
we could usehasSuggestedFilename
(thoughfalse
could be interpreted as 'there is a valid filename, just not a suggested one') orhasValidFilename
(though that suggests we test the actual validity of the filename, and I'd rather not sign up for that)Beta Was this translation helpful? Give feedback.
All reactions