From b7cb57b62c4c586d0075b6f08dbcc8419d7ab88e Mon Sep 17 00:00:00 2001 From: Norman-w <85535885@qq.com> Date: Tue, 19 Sep 2023 17:30:18 +0800 Subject: [PATCH] #10 Check for disk space before adding a download item #10 --- lib/util/download_addition_ui_util.dart | 12 +- lib/util/file_util.dart | 15 + macos/Flutter/Flutter-Debug.xcconfig | 1 + macos/Flutter/Flutter-Release.xcconfig | 1 + macos/Runner.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/xcschemes/Runner.xcscheme | 2 +- pubspec.lock | 574 +++++++++--------- pubspec.yaml | 1 + 8 files changed, 329 insertions(+), 279 deletions(-) diff --git a/lib/util/download_addition_ui_util.dart b/lib/util/download_addition_ui_util.dart index 9ca1b9a..2286224 100644 --- a/lib/util/download_addition_ui_util.dart +++ b/lib/util/download_addition_ui_util.dart @@ -27,8 +27,8 @@ class DownloadAdditionUiUtil { context.loaderOverlay.hide(); } - static void handleDownloadAddition(BuildContext context, String url, - {bool updateDialog = false, int? downloadId, additionalPop = false}) { + static Future handleDownloadAddition(BuildContext context, String url, + {bool updateDialog = false, int? downloadId, additionalPop = false}) async { if (!isUrlValid(url)) { showDialog( context: context, @@ -36,6 +36,14 @@ class DownloadAdditionUiUtil { ); } else { final item = DownloadItem.fromUrl(url); + var isValidDiskSpace = await FileUtil.checkDiskSpace(item.contentLength,item.filePath); + if(!isValidDiskSpace){ + showDialog( + context: context, + builder: (_) => const ErrorDialog(text: 'Not enough disk space'), + ); + return; + } _spawnFileInfoRetrieverIsolate(item).then((rPort) { context.loaderOverlay.show(); retrieveFileInfo(rPort).then((fileInfo) { diff --git a/lib/util/file_util.dart b/lib/util/file_util.dart index c67f36a..b3e031d 100644 --- a/lib/util/file_util.dart +++ b/lib/util/file_util.dart @@ -6,6 +6,7 @@ import 'package:brisk/util/file_extensions.dart'; import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; import 'package:path/path.dart'; +import 'package:universal_disk_space/universal_disk_space.dart'; import '../constants/file_type.dart'; import '../model/isolate/isolate_args_pair.dart'; @@ -202,4 +203,18 @@ class FileUtil { final filePath = join(SettingsCache.saveDir.path, subDir, fileName); return File(filePath).existsSync(); } + + static Future checkDiskSpace(int contentLength, String fileName) async { + var dir = Directory(SettingsCache.saveDir.path); + var disk = await DiskSpace().getDisk(dir); + var availableSpace = disk.availableSpace; + var fileSize = contentLength; + var fileNameSize = fileName.length; + var totalSize = fileSize + fileNameSize; + if (availableSpace > totalSize) { + return true; + } else { + return false; + } + } } diff --git a/macos/Flutter/Flutter-Debug.xcconfig b/macos/Flutter/Flutter-Debug.xcconfig index c2efd0b..4b81f9b 100644 --- a/macos/Flutter/Flutter-Debug.xcconfig +++ b/macos/Flutter/Flutter-Debug.xcconfig @@ -1 +1,2 @@ +#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" #include "ephemeral/Flutter-Generated.xcconfig" diff --git a/macos/Flutter/Flutter-Release.xcconfig b/macos/Flutter/Flutter-Release.xcconfig index c2efd0b..5caa9d1 100644 --- a/macos/Flutter/Flutter-Release.xcconfig +++ b/macos/Flutter/Flutter-Release.xcconfig @@ -1 +1,2 @@ +#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" #include "ephemeral/Flutter-Generated.xcconfig" diff --git a/macos/Runner.xcodeproj/project.pbxproj b/macos/Runner.xcodeproj/project.pbxproj index c4211fc..431d345 100644 --- a/macos/Runner.xcodeproj/project.pbxproj +++ b/macos/Runner.xcodeproj/project.pbxproj @@ -182,7 +182,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0920; - LastUpgradeCheck = 1300; + LastUpgradeCheck = 1430; ORGANIZATIONNAME = ""; TargetAttributes = { 33CC10EC2044A3C60003C045 = { diff --git a/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index 371ccb3..59df8c6 100644 --- a/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -1,6 +1,6 @@ =3.0.0-134.0.dev <4.0.0" - flutter: ">=3.3.0" + dart: ">=3.1.0 <4.0.0" + flutter: ">=3.13.0" diff --git a/pubspec.yaml b/pubspec.yaml index 0d905e6..68500c1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -58,6 +58,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 + universal_disk_space: ^0.2.3 dev_dependencies: flutter_test: