save_to_album_handler.dart 1.1 KB
import 'package:appframe/services/dispatcher.dart';
import 'package:gallery_saver_plus/gallery_saver.dart';

class SaveToAlbumHandler implements MessageHandler {
  @override
  Future<dynamic> handleMessage(dynamic params) async {
    if (params is! Map<String, dynamic>) {
      throw Exception('参数错误');
    }

    var filePath = params['filePath'] as String?;
    if (filePath == null || filePath.isEmpty) {
      return false;
    }

    bool isVideo =
        filePath.endsWith('.mp4') ||
        filePath.endsWith('.avi') ||
        filePath.endsWith('.mov') ||
        filePath.endsWith('.mkv') ||
        filePath.endsWith('.wmv') ||
        filePath.endsWith('.flv') ||
        filePath.endsWith('.webm') ||
        filePath.endsWith('.m4v') ||
        filePath.endsWith('.3gp');

    try {
      if (isVideo) {
        final bool? success = await GallerySaver.saveVideo(filePath);
        return success ?? false;
      } else {
        final bool? success = await GallerySaver.saveImage(filePath);
        return success ?? false;
      }
    } catch (e) {
      print(e);
      return false;
    }
  }
}