Closed 838 closed 1 year ago
PS. Thank you for the excellent work on this project.
We are in the process of migrating our application from using 'flutter_downloader' to 'background_downloader'. In the testing phase, we have found that everything is working smoothly and efficiently. Your efforts are much appreciated.
Interesting idea - let me think about how this could be added.
HI, implemented this for Android and iOS only (doesn't seem that relevant on Desktop). Can you please help test this on both platforms? Change your pubspec.yaml to:
background_downloader:
git:
url: https://github.com/781flyingdutchman/background_downloader.git
ref: space
This is implemented as part of a new 'configuration' approach (that isn't published yet). To configure the downloader to do a space check:
final configResult = await FileDownloader()
.configure(globalConfig: ('checkAvailableSpace', 1000)); // ensures at least 1000MB space remains
Note the argument to .configure is a record (and can be a list of records), hence the brackets. The configResult is a list containing records where the first item is the configuration string used, followed by an empty string if all went well. Please read the CONFIG.md file in the github repo (on the space
branch) to see how to use the new config functionality. There are also several config examples used in the example app.
To test, set the config value to an impossibly high value for required free space, and you should see your tasks fail, with an error stating insufficient space. Setting the config value to 'false' removes the check altogether (as it was before). Note that the space check is actually 'intelligent' in that it does take into account the file sizes of currently running tasks to determine if there is enough space left for the task that is about to start. This is harder to test, but I'd appreciate if you could confirm it works for you!
Let me know if you have any questions.
Nice work 👍 🚀
I have started testing on iOS and it looks really good 💪 .
I'm just a bit uncertain about the way we get the available space on the device.
I have 28.9GB available space and the configuration is:
_downloader = FileDownloader()
..configure(
iOSConfig: [
('checkAvailableSpace', 7000),
],
androidConfig: [
('checkAvailableSpace', 12000),
],
)
..resumeFromBackground()
..registerCallbacks(
taskStatusCallback: (final update) {
_downloadProgressCallback(update.task);
},
taskProgressCallback: (final update) {
_downloadProgressCallback(update.task);
},
);
After downloading a 1.2GB file i got this console [Downloader] available= -686620672
, and started getting the Insufficient space to store the file to be downloaded
exception even there are still lots of availible space.
Shouldn't we do something like this instead to get the available space on the device?
var freeDiskSpaceInBytes:Int64 {
if #available(iOS 11.0, *) {
if let space = try? URL(fileURLWithPath: NSHomeDirectory() as String).resourceValues(forKeys: [URLResourceKey.volumeAvailableCapacityForImportantUsageKey]).volumeAvailableCapacityForImportantUsage {
return space ?? 0
} else {
return 0
}
} else {
if let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String),
let freeSpace = (systemAttributes[FileAttributeKey.systemFreeSize] as? NSNumber)?.int64Value {
return freeSpace
} else {
return 0
}
}
}
Hi, thanks for testing, and yes that looks a little suspicious. The iOS code to determine if there is sufficient space is in the space
branch, here. I've taken the approach given to me by StackOverflow, but perhaps can you try alternative ways by forking the repo, and submitting a PR on space
when you have found what works best? There is a similar function in the Android code, if you run into issues there.
Hi @838 , I changed the way to calculate available space, which I think fixes the problem. Can you test again? Thanks
Included in V7.9.0
@781flyingdutchman Nice job 👍 , I try to se if I could manage a time to test it with our app.
Is it possible that the plugin manages the size of downloads that have been queued?
For example, let's say the device has 3GB of available download space, and we are attempting to queue four downloads that will take up a total of 2GB. However, if a 5th download is attempted that requires an additional 2GB while other downloads are still running, an exception should be thrown, indicating that there is not enough space available.