2026-05-29 16:17:26 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
|
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
|
|
|
|
|
|
|
|
|
|
import 'video_batch_upload_config.dart';
|
|
|
|
|
import 'video_batch_upload_task.dart';
|
|
|
|
|
|
|
|
|
|
class VideoBatchUploadPicker {
|
|
|
|
|
const VideoBatchUploadPicker._();
|
|
|
|
|
|
|
|
|
|
static Future<List<VideoBatchUploadTask>> pickVideos(
|
|
|
|
|
BuildContext context, {
|
|
|
|
|
int maxAssets = VideoBatchUploadConfig.maxAssets,
|
2026-05-29 17:58:44 +08:00
|
|
|
}) async {
|
|
|
|
|
return pickMedia(
|
|
|
|
|
context,
|
|
|
|
|
maxAssets: maxAssets,
|
|
|
|
|
assetType: VideoBatchUploadAssetType.video,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Future<List<VideoBatchUploadTask>> pickImages(
|
|
|
|
|
BuildContext context, {
|
|
|
|
|
int maxAssets = VideoBatchUploadConfig.maxAssets,
|
|
|
|
|
}) async {
|
|
|
|
|
return pickMedia(
|
|
|
|
|
context,
|
|
|
|
|
maxAssets: maxAssets,
|
|
|
|
|
assetType: VideoBatchUploadAssetType.image,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Future<List<VideoBatchUploadTask>> pickMedia(
|
|
|
|
|
BuildContext context, {
|
|
|
|
|
int maxAssets = VideoBatchUploadConfig.maxAssets,
|
|
|
|
|
VideoBatchUploadAssetType assetType = VideoBatchUploadAssetType.video,
|
2026-05-29 16:17:26 +08:00
|
|
|
}) async {
|
|
|
|
|
final List<AssetEntity>? assets = await AssetPicker.pickAssets(
|
|
|
|
|
context,
|
|
|
|
|
pickerConfig: AssetPickerConfig(
|
|
|
|
|
maxAssets: maxAssets,
|
2026-05-29 17:58:44 +08:00
|
|
|
requestType: _requestType(assetType),
|
2026-05-29 16:17:26 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
if (assets == null || assets.isEmpty) {
|
|
|
|
|
return <VideoBatchUploadTask>[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final List<VideoBatchUploadTask> tasks = <VideoBatchUploadTask>[];
|
|
|
|
|
for (final AssetEntity asset in assets) {
|
|
|
|
|
final File? file = await asset.file;
|
|
|
|
|
if (file == null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
final Uint8List? thumbnail = await asset.thumbnailDataWithSize(
|
|
|
|
|
const ThumbnailSize.square(220),
|
|
|
|
|
quality: 72,
|
|
|
|
|
);
|
|
|
|
|
final int fileSize = await file.length();
|
|
|
|
|
tasks.add(
|
|
|
|
|
VideoBatchUploadTask(
|
|
|
|
|
id: '${asset.id}_${DateTime.now().microsecondsSinceEpoch}',
|
2026-05-29 17:58:44 +08:00
|
|
|
assetType: assetType,
|
2026-05-29 16:17:26 +08:00
|
|
|
file: file,
|
2026-05-29 17:58:44 +08:00
|
|
|
fileName: _normalizeFileName(asset.title, file.path, assetType),
|
2026-05-29 16:17:26 +08:00
|
|
|
fileSize: fileSize,
|
2026-05-29 17:58:44 +08:00
|
|
|
duration:
|
|
|
|
|
assetType == VideoBatchUploadAssetType.video ? asset.duration : 0,
|
2026-05-29 16:17:26 +08:00
|
|
|
thumbnail: thumbnail,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return tasks;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 17:58:44 +08:00
|
|
|
static RequestType _requestType(VideoBatchUploadAssetType assetType) {
|
|
|
|
|
switch (assetType) {
|
|
|
|
|
case VideoBatchUploadAssetType.image:
|
|
|
|
|
return RequestType.image;
|
|
|
|
|
case VideoBatchUploadAssetType.video:
|
|
|
|
|
return RequestType.video;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static String _normalizeFileName(
|
|
|
|
|
String? assetTitle,
|
|
|
|
|
String path,
|
|
|
|
|
VideoBatchUploadAssetType assetType,
|
|
|
|
|
) {
|
2026-05-29 16:17:26 +08:00
|
|
|
final String title = (assetTitle ?? '').trim();
|
|
|
|
|
if (title.isNotEmpty) {
|
|
|
|
|
return title;
|
|
|
|
|
}
|
|
|
|
|
final List<String> segments = path.split(Platform.pathSeparator);
|
|
|
|
|
final String fileName = segments.isEmpty ? '' : segments.last.trim();
|
2026-05-29 17:58:44 +08:00
|
|
|
return fileName.isEmpty ? assetType.defaultFileName : fileName;
|
2026-05-29 16:17:26 +08:00
|
|
|
}
|
|
|
|
|
}
|