Skip to content

An open-source cross-platform alternative to AirDrop

fix: avoid division by zero on the progress page when a file fails in the millisecond the session starts (#3736)

Tien Nam Do8e9997366435
diff --git a/app/lib/pages/progress_page.dart b/app/lib/pages/progress_page.dart
index 204de92c..fcd78f76 100644
--- a/app/lib/pages/progress_page.dart
+++ b/app/lib/pages/progress_page.dart
@@ -238,17 +238,13 @@ class _ProgressPageState extends State<ProgressPage> with Refena {
final title = receiveSession != null ? t.progressPage.titleReceiving : t.progressPage.titleSending;
final startTime = commonSessionState.startTime;
final endTime = commonSessionState.endTime;
- final int? speedInBytes;
- if (startTime != null && currBytes >= 500 * 1024) {
- speedInBytes = getFileSpeed(start: startTime, end: endTime ?? DateTime.now().millisecondsSinceEpoch, bytes: currBytes);
-
+ final speedInBytes = measureSpeed(startTime: startTime, endTime: endTime, bytes: currBytes);
+ if (speedInBytes != null) {
final now = DateTime.now().millisecondsSinceEpoch;
if (now - _lastRemainingTimeUpdate >= 1000) {
_remainingTime = getRemainingTime(bytesPerSeconds: speedInBytes, remainingBytes: _totalBytes - currBytes, strings: notificationStrings);
_lastRemainingTimeUpdate = now;
}
- } else {
- speedInBytes = null;
}
final finishedCount = transferNotifier.getStatuses(widget.sessionId).where((s) => s == FileStatus.finished).length;
diff --git a/app/test/unit/util/file_speed_helper_test.dart b/app/test/unit/util/file_speed_helper_test.dart
index b40d1a1c..04702b6f 100644
--- a/app/test/unit/util/file_speed_helper_test.dart
+++ b/app/test/unit/util/file_speed_helper_test.dart
@@ -43,11 +43,29 @@ void main() {
});
});
- group('getFileSpeed', () {
+ group('measureSpeed', () {
test('calculates bytes per second correctly', () {
- expect(getFileSpeed(start: 0, end: 1000, bytes: 1000), 1000);
- expect(getFileSpeed(start: 0, end: 2000, bytes: 4000), 2000);
- expect(getFileSpeed(start: 1000, end: 3000, bytes: 10000), 5000);
+ expect(measureSpeed(startTime: 0, endTime: 1000, bytes: 1000000), 1000000);
+ expect(measureSpeed(startTime: 0, endTime: 2000, bytes: 4000000), 2000000);
+ expect(measureSpeed(startTime: 1000, endTime: 3000, bytes: 10000000), 5000000);
+ });
+
+ test('returns null before the transfer started', () {
+ expect(measureSpeed(startTime: null, endTime: null, bytes: 1000000), null);
+ });
+
+ test('returns null below 500 KB', () {
+ expect(measureSpeed(startTime: 0, endTime: 1000, bytes: 500 * 1024 - 1), null);
+ expect(measureSpeed(startTime: 0, endTime: 1000, bytes: 500 * 1024), 512000);
+ });
+
+ test('returns null when the transfer ended in the millisecond it started', () {
+ expect(measureSpeed(startTime: 1000, endTime: 1000, bytes: 20000000), null);
+ });
+
+ test('uses the current time while the transfer is running', () {
+ final startTime = DateTime.now().millisecondsSinceEpoch - 10000;
+ expect(measureSpeed(startTime: startTime, endTime: null, bytes: 10000000), inInclusiveRange(900000, 1000000));
});
});
}
diff --git a/packages/localsend_isolates/lib/util/file_speed_helper.dart b/packages/localsend_isolates/lib/util/file_speed_helper.dart
index 4a0c73bd..fd94151e 100644
--- a/packages/localsend_isolates/lib/util/file_speed_helper.dart
+++ b/packages/localsend_isolates/lib/util/file_speed_helper.dart
@@ -4,13 +4,28 @@ const _millisecondsPerSecond = 1000;
const _secondsPerMinute = 60;
const _secondsPerHour = 3600;
-int getFileSpeed({
- required int start,
- required int end,
+/// A transfer only becomes fast enough to measure after a while; the first chunks are not representative.
+const _minBytesForSpeed = 500 * 1024;
+
+/// The transfer speed in bytes per second, or null while it cannot be measured yet.
+/// [endTime] is null while the transfer is running, then the current time is used.
+int? measureSpeed({
+ required int? startTime,
+ required int? endTime,
required int bytes,
}) {
- final deltaTime = end - start;
- return (_millisecondsPerSecond * bytes) ~/ deltaTime;
+ if (startTime == null || bytes < _minBytesForSpeed) {
+ return null;
+ }
+
+ final end = endTime ?? DateTime.now().millisecondsSinceEpoch;
+ if (end <= startTime) {
+ // E.g. a file that fails right away (a rejected file name) counts as fully transferred
+ // and ends the session in the millisecond it started.
+ return null;
+ }
+
+ return (_millisecondsPerSecond * bytes) ~/ (end - startTime);
}
String getRemainingTime({
diff --git a/packages/localsend_isolates/lib/util/transfer_notification.dart b/packages/localsend_isolates/lib/util/transfer_notification.dart
index 3dca0bab..5272b4c2 100644
--- a/packages/localsend_isolates/lib/util/transfer_notification.dart
+++ b/packages/localsend_isolates/lib/util/transfer_notification.dart
@@ -3,10 +3,6 @@ import 'package:localsend_isolates/util/file_speed_helper.dart';
import 'package:localsend_isolates/util/foreground_service.dart';
import 'package:localsend_isolates/util/notification_strings.dart';
-/// A transfer only becomes fast enough to measure after a while; the first chunks are not
-/// representative. Same threshold as `ProgressPage`.
-const _minBytesForSpeed = 500 * 1024;
-
/// Drives the single foreground service notification on behalf of all running transfers.
///
/// There is one notification but there can be several transfers: receiving is limited to one
@@ -70,7 +66,7 @@ class TransferNotification {
transfer.currentBytes = currentBytes;
transfer.totalBytes = totalBytes;
- transfer.speedInBytes = _speed(currentBytes: currentBytes, startTime: startTime, endTime: endTime);
+ transfer.speedInBytes = measureSpeed(startTime: startTime, endTime: endTime, bytes: currentBytes);
ForegroundService.updateNotification(title: _title(), text: _text());
}
@@ -88,21 +84,6 @@ class TransferNotification {
}
}
- /// The transfer speed in bytes per second, or null while it cannot be measured yet.
- static int? _speed({required int currentBytes, required int? startTime, required int? endTime}) {
- if (startTime == null || currentBytes < _minBytesForSpeed) {
- return null;
- }
-
- final end = endTime ?? DateTime.now().millisecondsSinceEpoch;
- if (end <= startTime) {
- // guards the division in [getFileSpeed]
- return null;
- }
-
- return getFileSpeed(start: startTime, end: end, bytes: currentBytes);
- }
-
static NotificationStrings get _requiredStrings {
final strings = _strings;
if (strings == null) {