2024-08-01 15:10:19 +00:00
|
|
|
import 'dart:async';
|
2024-08-21 01:33:34 +00:00
|
|
|
import 'dart:collection';
|
2024-08-01 14:13:08 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2024-08-20 17:53:16 +00:00
|
|
|
import 'package:cross_file/cross_file.dart';
|
2024-08-01 14:13:08 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-08-20 17:53:16 +00:00
|
|
|
import 'package:path/path.dart' show basename;
|
2024-08-01 14:13:08 +00:00
|
|
|
import 'package:solian/models/attachment.dart';
|
|
|
|
import 'package:solian/providers/content/attachment.dart';
|
|
|
|
|
|
|
|
class AttachmentUploadTask {
|
2024-08-20 17:53:16 +00:00
|
|
|
XFile file;
|
|
|
|
String pool;
|
2024-08-01 14:13:08 +00:00
|
|
|
Map<String, dynamic>? metadata;
|
2024-08-20 17:53:16 +00:00
|
|
|
Map<String, int>? chunkFiles;
|
2024-08-01 14:13:08 +00:00
|
|
|
|
2024-08-21 02:01:09 +00:00
|
|
|
double? progress;
|
2024-08-01 14:13:08 +00:00
|
|
|
bool isUploading = false;
|
|
|
|
bool isCompleted = false;
|
2024-08-03 13:29:48 +00:00
|
|
|
dynamic error;
|
2024-08-01 14:13:08 +00:00
|
|
|
|
|
|
|
AttachmentUploadTask({
|
|
|
|
required this.file,
|
2024-08-20 17:53:16 +00:00
|
|
|
required this.pool,
|
2024-08-01 14:13:08 +00:00
|
|
|
this.metadata,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class AttachmentUploaderController extends GetxController {
|
|
|
|
RxBool isUploading = false.obs;
|
|
|
|
RxDouble progressOfUpload = 0.0.obs;
|
|
|
|
RxList<AttachmentUploadTask> queueOfUpload = RxList.empty(growable: true);
|
|
|
|
|
2024-08-01 15:10:19 +00:00
|
|
|
Timer? _progressSyncTimer;
|
|
|
|
double _progressOfUpload = 0.0;
|
|
|
|
|
|
|
|
void _syncProgress() {
|
|
|
|
progressOfUpload.value = _progressOfUpload;
|
|
|
|
queueOfUpload.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _startProgressSyncTimer() {
|
|
|
|
if (_progressSyncTimer != null) {
|
|
|
|
_progressSyncTimer!.cancel();
|
|
|
|
}
|
|
|
|
_progressSyncTimer = Timer.periodic(
|
|
|
|
const Duration(milliseconds: 500),
|
|
|
|
(_) => _syncProgress(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _stopProgressSyncTimer() {
|
|
|
|
if (_progressSyncTimer == null) return;
|
|
|
|
_progressSyncTimer!.cancel();
|
|
|
|
_progressSyncTimer = null;
|
|
|
|
}
|
|
|
|
|
2024-08-01 14:13:08 +00:00
|
|
|
void enqueueTask(AttachmentUploadTask task) {
|
|
|
|
if (isUploading.value) throw Exception('uploading blocked');
|
|
|
|
queueOfUpload.add(task);
|
|
|
|
}
|
|
|
|
|
2024-08-01 15:10:19 +00:00
|
|
|
void enqueueTaskBatch(Iterable<AttachmentUploadTask> tasks) {
|
|
|
|
if (isUploading.value) throw Exception('uploading blocked');
|
|
|
|
queueOfUpload.addAll(tasks);
|
|
|
|
}
|
|
|
|
|
2024-08-01 14:13:08 +00:00
|
|
|
void dequeueTask(AttachmentUploadTask task) {
|
|
|
|
if (isUploading.value) throw Exception('uploading blocked');
|
|
|
|
queueOfUpload.remove(task);
|
|
|
|
}
|
|
|
|
|
2024-08-03 13:29:48 +00:00
|
|
|
Future<Attachment?> performSingleTask(int queueIndex) async {
|
2024-08-01 14:13:08 +00:00
|
|
|
isUploading.value = true;
|
|
|
|
progressOfUpload.value = 0;
|
|
|
|
|
2024-08-01 15:10:19 +00:00
|
|
|
_startProgressSyncTimer();
|
2024-08-01 14:13:08 +00:00
|
|
|
queueOfUpload[queueIndex].isUploading = true;
|
2024-08-21 02:01:09 +00:00
|
|
|
queueOfUpload[queueIndex].progress = 0;
|
2024-08-01 14:13:08 +00:00
|
|
|
|
|
|
|
final task = queueOfUpload[queueIndex];
|
2024-08-20 17:53:16 +00:00
|
|
|
try {
|
|
|
|
final result = await _chunkedUploadAttachment(
|
|
|
|
task.file,
|
|
|
|
task.pool,
|
|
|
|
null,
|
|
|
|
onData: (_) {},
|
|
|
|
onProgress: (progress) {
|
|
|
|
queueOfUpload[queueIndex].progress = progress;
|
|
|
|
_progressOfUpload = progress;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return result;
|
|
|
|
} catch (err) {
|
|
|
|
queueOfUpload[queueIndex].error = err;
|
|
|
|
queueOfUpload[queueIndex].isUploading = false;
|
|
|
|
} finally {
|
|
|
|
_progressOfUpload = 1;
|
|
|
|
if (queueOfUpload[queueIndex].error == null) {
|
|
|
|
queueOfUpload.removeAt(queueIndex);
|
|
|
|
}
|
|
|
|
_stopProgressSyncTimer();
|
|
|
|
_syncProgress();
|
2024-08-01 14:13:08 +00:00
|
|
|
|
2024-08-20 17:53:16 +00:00
|
|
|
isUploading.value = false;
|
2024-08-03 13:29:48 +00:00
|
|
|
}
|
2024-08-01 14:13:08 +00:00
|
|
|
|
2024-08-20 17:53:16 +00:00
|
|
|
return null;
|
2024-08-01 14:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> performUploadQueue({
|
|
|
|
required Function(Attachment item) onData,
|
|
|
|
}) async {
|
|
|
|
isUploading.value = true;
|
|
|
|
progressOfUpload.value = 0;
|
|
|
|
|
2024-08-01 15:10:19 +00:00
|
|
|
_startProgressSyncTimer();
|
|
|
|
|
2024-08-01 14:13:08 +00:00
|
|
|
for (var idx = 0; idx < queueOfUpload.length; idx++) {
|
2024-08-03 13:29:48 +00:00
|
|
|
if (queueOfUpload[idx].isUploading || queueOfUpload[idx].error != null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-01 14:13:08 +00:00
|
|
|
queueOfUpload[idx].isUploading = true;
|
2024-08-21 02:01:09 +00:00
|
|
|
queueOfUpload[idx].progress = 0;
|
2024-08-01 14:13:08 +00:00
|
|
|
|
|
|
|
final task = queueOfUpload[idx];
|
2024-08-20 17:53:16 +00:00
|
|
|
try {
|
|
|
|
final result = await _chunkedUploadAttachment(
|
|
|
|
task.file,
|
|
|
|
task.pool,
|
|
|
|
null,
|
|
|
|
onData: (_) {},
|
|
|
|
onProgress: (progress) {
|
|
|
|
queueOfUpload[idx].progress = progress;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (result != null) onData(result);
|
|
|
|
} catch (err) {
|
|
|
|
queueOfUpload[idx].error = err;
|
|
|
|
queueOfUpload[idx].isUploading = false;
|
|
|
|
} finally {
|
|
|
|
_progressOfUpload = (idx + 1) / queueOfUpload.length;
|
|
|
|
}
|
2024-08-01 14:13:08 +00:00
|
|
|
|
|
|
|
queueOfUpload[idx].isUploading = false;
|
2024-08-03 13:29:48 +00:00
|
|
|
queueOfUpload[idx].isCompleted = true;
|
2024-08-01 14:13:08 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 17:17:31 +00:00
|
|
|
queueOfUpload.removeWhere((x) => x.error == null);
|
2024-08-01 15:10:19 +00:00
|
|
|
_stopProgressSyncTimer();
|
|
|
|
_syncProgress();
|
|
|
|
|
2024-08-01 14:13:08 +00:00
|
|
|
isUploading.value = false;
|
|
|
|
}
|
|
|
|
|
2024-08-20 17:53:16 +00:00
|
|
|
Future<Attachment?> uploadAttachmentFromData(
|
2024-08-01 14:13:08 +00:00
|
|
|
Uint8List data,
|
|
|
|
String path,
|
2024-08-18 14:51:52 +00:00
|
|
|
String pool,
|
2024-08-01 14:13:08 +00:00
|
|
|
Map<String, dynamic>? metadata,
|
|
|
|
) async {
|
|
|
|
if (isUploading.value) throw Exception('uploading blocked');
|
|
|
|
isUploading.value = true;
|
|
|
|
|
2024-08-20 17:53:16 +00:00
|
|
|
final AttachmentProvider attach = Get.find();
|
2024-08-01 14:13:08 +00:00
|
|
|
|
|
|
|
try {
|
2024-08-20 17:53:16 +00:00
|
|
|
final result = await attach.createAttachmentDirectly(
|
2024-08-01 14:13:08 +00:00
|
|
|
data,
|
|
|
|
path,
|
2024-08-18 14:51:52 +00:00
|
|
|
pool,
|
2024-08-01 14:13:08 +00:00
|
|
|
metadata,
|
|
|
|
);
|
|
|
|
return result;
|
2024-08-20 17:53:16 +00:00
|
|
|
} catch (_) {
|
2024-08-03 13:29:48 +00:00
|
|
|
return null;
|
2024-08-20 17:53:16 +00:00
|
|
|
} finally {
|
|
|
|
isUploading.value = false;
|
2024-08-01 14:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-20 17:53:16 +00:00
|
|
|
|
|
|
|
Future<Attachment?> _chunkedUploadAttachment(
|
|
|
|
XFile file,
|
|
|
|
String pool,
|
|
|
|
Map<String, dynamic>? metadata, {
|
|
|
|
required Function(AttachmentPlaceholder) onData,
|
|
|
|
required Function(double) onProgress,
|
|
|
|
}) async {
|
|
|
|
final AttachmentProvider attach = Get.find();
|
|
|
|
|
|
|
|
final holder = await attach.createAttachmentMultipartPlaceholder(
|
|
|
|
await file.length(),
|
|
|
|
file.path,
|
|
|
|
pool,
|
|
|
|
metadata,
|
|
|
|
);
|
|
|
|
onData(holder);
|
|
|
|
|
|
|
|
onProgress(0);
|
|
|
|
|
|
|
|
final filename = basename(file.path);
|
|
|
|
final chunks = holder.meta.fileChunks ?? {};
|
|
|
|
var currentTask = 0;
|
2024-08-21 01:33:34 +00:00
|
|
|
|
|
|
|
final queue = Queue<Future<void>>();
|
|
|
|
final activeTasks = <Future<void>>[];
|
|
|
|
|
2024-08-20 17:53:16 +00:00
|
|
|
for (final entry in chunks.entries) {
|
2024-08-21 01:33:34 +00:00
|
|
|
queue.add(() async {
|
|
|
|
final beginCursor = entry.value * holder.chunkSize;
|
|
|
|
final endCursor = (entry.value + 1) * holder.chunkSize;
|
|
|
|
final data = Uint8List.fromList(await file
|
|
|
|
.openRead(beginCursor, endCursor)
|
|
|
|
.expand((chunk) => chunk)
|
|
|
|
.toList());
|
|
|
|
|
|
|
|
final out = await attach.uploadAttachmentMultipartChunk(
|
|
|
|
data,
|
|
|
|
filename,
|
|
|
|
holder.meta.rid,
|
|
|
|
entry.key,
|
|
|
|
);
|
|
|
|
holder.meta = out;
|
2024-08-20 17:53:16 +00:00
|
|
|
|
2024-08-21 01:33:34 +00:00
|
|
|
currentTask++;
|
|
|
|
onProgress(currentTask / chunks.length);
|
|
|
|
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);
|
|
|
|
}
|
2024-08-20 17:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return holder.meta;
|
|
|
|
}
|
2024-08-01 14:13:08 +00:00
|
|
|
}
|