781flyingdutchman / background_downloader

Flutter plugin for file downloads and uploads
Other
121 stars 53 forks source link

Add support for submitting data without creating files #304

Closed ekuleshov closed 1 week ago

ekuleshov commented 1 week ago

Is your feature request related to a problem? Please describe. Currently there are task types that can be enqueued to upload or download files. There is also a request method for sending other server requests, but in my understanding it is executed immediately, for example it does not wait for wifi availability.

It would be also handy to be able to enqueue regular server requests to submit some data to server and it would be great if we won't have to create files for that.

Describe the solution you'd like Basically it would be really handy to have another task variant similar to UploadTask, but without having to provide a filename, directory and baseDirectory parameters and instead be able to pass the request data (binary or string) in the Task.post parameter, similar to DownloadTask. And when task is completed, the server response body would be available in the TaskStatusUpdate.responseBody similar to the UploadTask.

Describe alternatives you've considered Currently I have to use either UploadTask or DownloadTask and have to create or delete temporary files.

Additional context N/A

781flyingdutchman commented 1 week ago

Good idea and not the first one to request it. The issue is iOS, but I'll consider this for a future update as I do think there's a workaround.

ekuleshov commented 1 week ago

I figured the iOS one could be a problem. But perhaps you could hide file manipulations from that special task. E.g. create a temp file for uploading using provided post payload and then clean it when task is completed.

781flyingdutchman commented 1 week ago

Yes that's exactly what I'm thinking of. It would be a subclass of UploadTask that doesn't take the file etc parameters but only post. On iOS we'd have to create a temp file from the post data and keep track of that, then treat it similar to an UploadTask.

ekuleshov commented 1 week ago

Do you think it will be possible to keep track of task updates/failures even if/when app goes to background? If I'm not mistaken the download task is looking at the downloaded files to update task statuses when getting back to foregorund.

781flyingdutchman commented 1 week ago

If a upload fails (and this would essentially be an upload, we just mask the temp file handling) then you would get a .failed status update when you resume your app and call resumeFromBackground, so if that's what you're asking then yes, it will. Let me know if I misunderstood the question

781flyingdutchman commented 1 week ago

Hi, can you help test this? Change your pubspec.yaml to:

background_downloader:
    git:
      url: https://github.com/781flyingdutchman/background_downloader.git
      ref: DataTask

and test creating and enqueueing a DataTask, which does what we described here (also takes a json argument and contentType if necessary. Also added a method backgroundRequest to the FileDownloader if you want to await the result, though then you might also simply use the request method directly.

ekuleshov commented 1 week ago

I'm on it!

By the way, how do you recommend to query pending/failed tasks? E.g. FileDownloader.allTasks() returns the tasks, but not the state of each of them.

Also, when having persistent storage enabled, what is the recommended way to clear completed tasks? E.g. once task is completed I need to clear it out immediately. But failed tasks (including tasks failed for business logic reasons) could stay there for X number of days and then cleared out after that.

781flyingdutchman commented 1 week ago

If you want to keep state around, you need to add persistent storage and use the database object. That also lets you delete tasks as needed

ekuleshov commented 1 week ago

If you want to keep state around, you need to add persistent storage and use the database object. That also lets you delete tasks as needed

I enabled persistent store. Though I only need to keep track of pending tasks and failures (including the failures based on response content even when http status is 200). And for that purpose the exposed Database api is rather limiting.

Was wondering if persistent store has anything built in to do such cleanup. If not I'd have to run some kind of periodic job to clean it up, which would make things more complicated...

Or if there some other mechanism I could use to keep track of pending and failed tasks? Even after restart of the app.

781flyingdutchman commented 1 week ago

You'd have to implement a persistent storage yourself, or take a look at the backfround_downloader_sql package that has a sqlite based storage that supports regular SQL queries.

On Thu, May 9, 2024, 4:49 PM Eugene Kuleshov @.***> wrote:

If you want to keep state around, you need to add persistent storage and use the database object. That also lets you delete tasks as needed

I enabled persistent store. Though I only need to keep track of pending tasks and failures (including the failures based on response content even when http status is 200). And for that purpose the exposed Database api is rather limiting.

Was wondering if persistent store has anything built in to do such cleanup. If not I'd have to run some kind of periodic job to clean it up, which would make things more complicated...

Or if there some other mechanism I could use to keep track of pending and failed tasks? Even after restart of the app.

— Reply to this email directly, view it on GitHub https://github.com/781flyingdutchman/background_downloader/issues/304#issuecomment-2103470478, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF5VCHZUAVR53V5SOMKN4HDZBPVPNAVCNFSM6AAAAABHNKZRN6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMBTGQ3TANBXHA . You are receiving this because you commented.Message ID: @.***>

ekuleshov commented 1 week ago

You'd have to implement a persistent storage yourself, or take a look at the backfround_downloader_sql package that has a sqlite based storage that supports regular SQL queries.

I was hoping there is some simpler way. Currently it is unclear how persistent store interact with notification listeners. E.g. is it safe to delete/update persistent tasks in the store from within listener? And if app goes from foreground, will registered listeners be called on updated/loaded tasks from the resumeFromBackground call?

ekuleshov commented 1 week ago

Hi, can you help test this?...

I verified on Android, iOS and MacOS with this branch. The new DataTask is working great, including awaiting for network/wifi availability.

Sorry for derailing conversation in this request. I will try to submit some separate issues...

781flyingdutchman commented 1 week ago

Implemented in V8.5.0 with minor changes