177 lines
4.1 KiB
Dart
177 lines
4.1 KiB
Dart
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,
|
|
);
|
|
|
|
typedef VideoBatchUploadBatchSaveTask = Future<bool> Function(
|
|
List<VideoBatchUploadTask> tasks,
|
|
);
|
|
|
|
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';
|
|
}
|
|
}
|
|
}
|
|
|
|
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,
|
|
this.assetType = VideoBatchUploadAssetType.video,
|
|
required this.file,
|
|
required this.fileName,
|
|
required this.fileSize,
|
|
required this.duration,
|
|
this.thumbnail,
|
|
});
|
|
|
|
final String id;
|
|
final VideoBatchUploadAssetType assetType;
|
|
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();
|
|
}
|
|
|
|
String toLibraryImageURL() {
|
|
return uploadUrl;
|
|
}
|
|
}
|