⚡ Run upload chunks at the same time (max 3)
This commit is contained in:
parent
aa94dfcfe0
commit
498bb0e5fb
@ -1,4 +1,5 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:collection';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:cross_file/cross_file.dart';
|
import 'package:cross_file/cross_file.dart';
|
||||||
@ -199,25 +200,44 @@ class AttachmentUploaderController extends GetxController {
|
|||||||
final filename = basename(file.path);
|
final filename = basename(file.path);
|
||||||
final chunks = holder.meta.fileChunks ?? {};
|
final chunks = holder.meta.fileChunks ?? {};
|
||||||
var currentTask = 0;
|
var currentTask = 0;
|
||||||
|
|
||||||
|
final queue = Queue<Future<void>>();
|
||||||
|
final activeTasks = <Future<void>>[];
|
||||||
|
|
||||||
for (final entry in chunks.entries) {
|
for (final entry in chunks.entries) {
|
||||||
final beginCursor = entry.value * holder.chunkSize;
|
queue.add(() async {
|
||||||
final endCursor = (entry.value + 1) * holder.chunkSize;
|
final beginCursor = entry.value * holder.chunkSize;
|
||||||
final data = Uint8List.fromList(await file
|
final endCursor = (entry.value + 1) * holder.chunkSize;
|
||||||
.openRead(beginCursor, endCursor)
|
final data = Uint8List.fromList(await file
|
||||||
.expand((chunk) => chunk)
|
.openRead(beginCursor, endCursor)
|
||||||
.toList());
|
.expand((chunk) => chunk)
|
||||||
|
.toList());
|
||||||
|
|
||||||
final out = await attach.uploadAttachmentMultipartChunk(
|
final out = await attach.uploadAttachmentMultipartChunk(
|
||||||
data,
|
data,
|
||||||
filename,
|
filename,
|
||||||
holder.meta.rid,
|
holder.meta.rid,
|
||||||
entry.key,
|
entry.key,
|
||||||
);
|
);
|
||||||
holder.meta = out;
|
holder.meta = out;
|
||||||
|
|
||||||
currentTask++;
|
currentTask++;
|
||||||
onProgress(currentTask / chunks.length);
|
onProgress(currentTask / chunks.length);
|
||||||
onData(holder);
|
onData(holder);
|
||||||
|
}());
|
||||||
|
}
|
||||||
|
|
||||||
|
while (queue.isNotEmpty || activeTasks.isNotEmpty) {
|
||||||
|
while (activeTasks.length < 3 && queue.isNotEmpty) {
|
||||||
|
final task = queue.removeFirst();
|
||||||
|
activeTasks.add(task);
|
||||||
|
|
||||||
|
task.then((_) => activeTasks.remove(task));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeTasks.isNotEmpty) {
|
||||||
|
await Future.any(activeTasks);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return holder.meta;
|
return holder.meta;
|
||||||
|
Loading…
Reference in New Issue
Block a user