mirror of
https://github.com/ImranR98/Obtainium.git
synced 2025-07-13 13:26:43 +02:00
Adjustments
This commit is contained in:
@ -347,19 +347,20 @@ Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
|
|||||||
var received = 0;
|
var received = 0;
|
||||||
double? progress;
|
double? progress;
|
||||||
DateTime? lastProgressUpdate; // Track last progress update time
|
DateTime? lastProgressUpdate; // Track last progress update time
|
||||||
const throttleDuration = Duration(milliseconds: 100); // Throttle interval
|
|
||||||
if (rangeStart > 0 && fullContentLength != null) {
|
if (rangeStart > 0 && fullContentLength != null) {
|
||||||
received = rangeStart;
|
received = rangeStart;
|
||||||
}
|
}
|
||||||
const bufferSizeThreshold = 64 * 1024; // 64KB
|
const downloadUIUpdateInterval = Duration(milliseconds: 500);
|
||||||
final buffer = BytesBuilder(); // Efficiently accumulates bytes
|
const downloadBufferSize = 32 * 1024; // 32KB
|
||||||
|
final downloadBuffer = BytesBuilder();
|
||||||
await response.stream
|
await response.stream
|
||||||
.map((chunk) {
|
.map((chunk) {
|
||||||
received += chunk.length;
|
received += chunk.length;
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
if (onProgress != null &&
|
if (onProgress != null &&
|
||||||
(lastProgressUpdate == null ||
|
(lastProgressUpdate == null ||
|
||||||
now.difference(lastProgressUpdate!) >= throttleDuration)) {
|
now.difference(lastProgressUpdate!) >=
|
||||||
|
downloadUIUpdateInterval)) {
|
||||||
progress = fullContentLength != null
|
progress = fullContentLength != null
|
||||||
? (received / fullContentLength) * 100
|
? (received / fullContentLength) * 100
|
||||||
: 30;
|
: 30;
|
||||||
@ -369,17 +370,17 @@ Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
|
|||||||
return chunk;
|
return chunk;
|
||||||
})
|
})
|
||||||
.transform(StreamTransformer<List<int>, List<int>>.fromHandlers(
|
.transform(StreamTransformer<List<int>, List<int>>.fromHandlers(
|
||||||
handleData: (List<int> data, EventSink<List<int>> sink) {
|
handleData: (List<int> data, EventSink<List<int>> s) {
|
||||||
buffer.add(data);
|
downloadBuffer.add(data);
|
||||||
if (buffer.length >= bufferSizeThreshold) {
|
if (downloadBuffer.length >= downloadBufferSize) {
|
||||||
sink.add(buffer.takeBytes());
|
s.add(downloadBuffer.takeBytes());
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handleDone: (EventSink<List<int>> sink) {
|
handleDone: (EventSink<List<int>> s) {
|
||||||
if (buffer.isNotEmpty) {
|
if (downloadBuffer.isNotEmpty) {
|
||||||
sink.add(buffer.takeBytes());
|
s.add(downloadBuffer.takeBytes());
|
||||||
}
|
}
|
||||||
sink.close();
|
s.close();
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
.pipe(sink);
|
.pipe(sink);
|
||||||
|
Reference in New Issue
Block a user