95 lines
3.0 KiB
Dart
Executable File
95 lines
3.0 KiB
Dart
Executable File
import 'dart:io';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:image_gallery_saver/image_gallery_saver.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:star_lock/main/lockDetail/videoLog/videoLog/videoLog_entity.dart';
|
|
import 'package:star_lock/network/api_repository.dart';
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
import 'package:star_lock/versionUndate/versionUndate_entity.dart';
|
|
import 'editVideoLog_state.dart';
|
|
|
|
class EditVideoLogLogic extends BaseGetXController {
|
|
EditVideoLogState state = EditVideoLogState();
|
|
|
|
Future<void> deleteLockCloudStorageList() async {
|
|
final VersionUndateEntity entity =
|
|
await ApiRepository.to.deleteLockCloudStorageList(
|
|
recordIds: state.selectVideoLogList.value.map((e) => e.recordId).toList(),
|
|
);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
showToast('删除成功'.tr);
|
|
await getLockCloudStorageList();
|
|
state.selectVideoLogList.clear();
|
|
}
|
|
}
|
|
|
|
Future<void> getLockCloudStorageList() async {
|
|
final VideoLogEntity entity =
|
|
await ApiRepository.to.getLockCloudStorageList(
|
|
lockId: state.getLockId.value,
|
|
);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
state.videoLogList.value = entity.data!;
|
|
state.videoLogList.refresh();
|
|
}
|
|
}
|
|
|
|
Future<void> downloadAndSaveToGallery(String url, String fileName) async {
|
|
try {
|
|
// 请求存储权限
|
|
if (await _requestPermission()) {
|
|
final dio = Dio();
|
|
final directory = await getTemporaryDirectory();
|
|
final filePath = '${directory.path}/$fileName';
|
|
|
|
// 下载文件
|
|
await dio.download(
|
|
url,
|
|
filePath,
|
|
onReceiveProgress: (received, total) {
|
|
if (total != -1) {
|
|
final progress = (received / total) * 100;
|
|
print('下载进度: $progress%');
|
|
}
|
|
},
|
|
);
|
|
|
|
// 保存到图库
|
|
if (fileName.endsWith('.jpg') || fileName.endsWith('.png')) {
|
|
// 保存图片
|
|
final result = await ImageGallerySaver.saveFile(filePath);
|
|
if (result['isSuccess']) {
|
|
print('图片已保存到图库');
|
|
} else {
|
|
print('图片保存失败');
|
|
}
|
|
} else if (fileName.endsWith('.mp4')) {
|
|
// 保存视频
|
|
final result = await ImageGallerySaver.saveFile(filePath);
|
|
if (result['isSuccess']) {
|
|
print('视频已保存到图库');
|
|
} else {
|
|
print('视频保存失败');
|
|
}
|
|
}
|
|
|
|
// 删除临时文件
|
|
await File(filePath).delete();
|
|
} else {
|
|
print('存储权限被拒绝');
|
|
}
|
|
} catch (e) {
|
|
print('下载或保存文件失败: $e');
|
|
}
|
|
}
|
|
|
|
// 请求存储权限
|
|
Future<bool> _requestPermission() async {
|
|
final status = await Permission.storage.request();
|
|
return status.isGranted;
|
|
}
|
|
}
|