feat: add video batch upload queue
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
typedef VideoBatchUploadProgressCallback = void Function(double progress);
|
||||
|
||||
typedef VideoBatchUploadUploadTask = Future<String> Function(
|
||||
VideoBatchUploadTask task,
|
||||
VideoBatchUploadProgressCallback onProgress,
|
||||
);
|
||||
|
||||
typedef VideoBatchUploadSaveTask = Future<bool> Function(
|
||||
VideoBatchUploadTask task,
|
||||
);
|
||||
|
||||
enum VideoBatchUploadStatus {
|
||||
waiting,
|
||||
uploading,
|
||||
uploaded,
|
||||
saving,
|
||||
completed,
|
||||
failed,
|
||||
}
|
||||
|
||||
extension VideoBatchUploadStatusText on VideoBatchUploadStatus {
|
||||
String get label {
|
||||
switch (this) {
|
||||
case VideoBatchUploadStatus.waiting:
|
||||
return '等待中';
|
||||
case VideoBatchUploadStatus.uploading:
|
||||
return '上传中';
|
||||
case VideoBatchUploadStatus.uploaded:
|
||||
return '上传成功';
|
||||
case VideoBatchUploadStatus.saving:
|
||||
return '入库中';
|
||||
case VideoBatchUploadStatus.completed:
|
||||
return '已完成';
|
||||
case VideoBatchUploadStatus.failed:
|
||||
return '失败';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class VideoBatchUploadTask {
|
||||
VideoBatchUploadTask({
|
||||
required this.id,
|
||||
required this.file,
|
||||
required this.fileName,
|
||||
required this.fileSize,
|
||||
required this.duration,
|
||||
this.thumbnail,
|
||||
});
|
||||
|
||||
final String id;
|
||||
final File file;
|
||||
final String fileName;
|
||||
final int fileSize;
|
||||
final int duration;
|
||||
final Uint8List? thumbnail;
|
||||
|
||||
VideoBatchUploadStatus status = VideoBatchUploadStatus.waiting;
|
||||
double progress = 0;
|
||||
String uploadUrl = '';
|
||||
String errorMessage = '';
|
||||
CancelToken? cancelToken;
|
||||
|
||||
String get ossUrl => uploadUrl;
|
||||
|
||||
set ossUrl(String value) {
|
||||
uploadUrl = value;
|
||||
}
|
||||
|
||||
bool get isActive =>
|
||||
status == VideoBatchUploadStatus.waiting ||
|
||||
status == VideoBatchUploadStatus.uploading ||
|
||||
status == VideoBatchUploadStatus.uploaded ||
|
||||
status == VideoBatchUploadStatus.saving;
|
||||
|
||||
bool get canRetry => status == VideoBatchUploadStatus.failed;
|
||||
|
||||
bool get canRemove =>
|
||||
status == VideoBatchUploadStatus.waiting ||
|
||||
status == VideoBatchUploadStatus.completed ||
|
||||
status == VideoBatchUploadStatus.failed;
|
||||
|
||||
String get fileSizeText {
|
||||
if (fileSize <= 0) {
|
||||
return '';
|
||||
}
|
||||
final double mb = fileSize / 1024 / 1024;
|
||||
if (mb >= 1) {
|
||||
return '${mb.toStringAsFixed(1)}MB';
|
||||
}
|
||||
final double kb = fileSize / 1024;
|
||||
return '${kb.toStringAsFixed(0)}KB';
|
||||
}
|
||||
|
||||
String get durationText {
|
||||
if (duration <= 0) {
|
||||
return '';
|
||||
}
|
||||
final int minutes = duration ~/ 60;
|
||||
final int seconds = duration % 60;
|
||||
return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
Map<String, dynamic> toVideoPayload({
|
||||
String urlKey = 'fileURL',
|
||||
String fileNameKey = 'fileName',
|
||||
String durationKey = 'duration',
|
||||
}) {
|
||||
return <String, dynamic>{
|
||||
urlKey: uploadUrl,
|
||||
fileNameKey: fileName,
|
||||
if (duration > 0) durationKey: duration,
|
||||
};
|
||||
}
|
||||
|
||||
Map<String, dynamic> toLibraryVideoPayload() {
|
||||
return toVideoPayload();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user