Files
flutter_common/lib/upload_video/video_batch_upload_task.dart
T

177 lines
4.1 KiB
Dart
Raw Normal View History

2026-05-29 16:17:26 +08:00
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,
);
2026-05-29 18:26:16 +08:00
typedef VideoBatchUploadBatchSaveTask = Future<bool> Function(
List<VideoBatchUploadTask> tasks,
);
2026-05-29 17:58:44 +08:00
enum VideoBatchUploadAssetType {
image,
video,
}
extension VideoBatchUploadAssetTypeText on VideoBatchUploadAssetType {
String get label {
switch (this) {
case VideoBatchUploadAssetType.image:
return '图片';
case VideoBatchUploadAssetType.video:
return '视频';
}
}
String get unit {
switch (this) {
case VideoBatchUploadAssetType.image:
return '';
case VideoBatchUploadAssetType.video:
return '';
}
}
String get localLabel {
switch (this) {
case VideoBatchUploadAssetType.image:
return '本地图片';
case VideoBatchUploadAssetType.video:
return '本地视频';
}
}
String get defaultFileName {
switch (this) {
case VideoBatchUploadAssetType.image:
return 'image.jpg';
case VideoBatchUploadAssetType.video:
return 'video.mp4';
}
}
}
2026-05-29 16:17:26 +08:00
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,
2026-05-29 17:58:44 +08:00
this.assetType = VideoBatchUploadAssetType.video,
2026-05-29 16:17:26 +08:00
required this.file,
required this.fileName,
required this.fileSize,
required this.duration,
this.thumbnail,
});
final String id;
2026-05-29 17:58:44 +08:00
final VideoBatchUploadAssetType assetType;
2026-05-29 16:17:26 +08:00
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();
}
2026-05-29 17:58:44 +08:00
String toLibraryImageURL() {
return uploadUrl;
}
2026-05-29 16:17:26 +08:00
}