فهرست منبع

Decrypt and render encrypted videos

Vishnu Mohandas 4 سال پیش
والد
کامیت
7da487cd4e
3فایلهای تغییر یافته به همراه55 افزوده شده و 28 حذف شده
  1. 22 0
      lib/core/cache/video_cache_manager.dart
  2. 23 21
      lib/ui/video_widget.dart
  3. 10 7
      lib/utils/file_util.dart

+ 22 - 0
lib/core/cache/video_cache_manager.dart

@@ -0,0 +1,22 @@
+import 'package:flutter_cache_manager/flutter_cache_manager.dart';
+import 'package:path_provider/path_provider.dart';
+import 'package:path/path.dart' as p;
+
+class VideoCacheManager extends BaseCacheManager {
+  static const key = 'cached-video-data';
+
+  static VideoCacheManager _instance;
+
+  factory VideoCacheManager() {
+    _instance ??= VideoCacheManager._();
+    return _instance;
+  }
+
+  VideoCacheManager._() : super(key, maxNrOfCacheObjects: 50);
+
+  @override
+  Future<String> getFilePath() async {
+    var directory = await getTemporaryDirectory();
+    return p.join(directory.path, key);
+  }
+}

+ 23 - 21
lib/ui/video_widget.dart

@@ -1,4 +1,3 @@
-import 'package:cached_network_image/cached_network_image.dart';
 import 'package:chewie/chewie.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/widgets.dart';
@@ -7,6 +6,7 @@ import 'package:logging/logging.dart';
 import 'package:photos/models/file.dart';
 import 'package:photos/ui/thumbnail_widget.dart';
 import 'package:photos/ui/video_controls.dart';
+import 'package:photos/utils/file_util.dart';
 import 'package:photos/utils/toast_util.dart';
 import 'package:video_player/video_player.dart';
 import 'package:visibility_detector/visibility_detector.dart';
@@ -37,16 +37,24 @@ class _VideoWidgetState extends State<VideoWidget> {
   void initState() {
     super.initState();
     if (widget.file.localID == null) {
-      _setVideoPlayerController(widget.file.getStreamUrl());
-      _videoPlayerController.addListener(() {
-        if (_videoPlayerController.value.hasError) {
-          _logger.warning(_videoPlayerController.value.errorDescription);
-          showToast(
-              "The video has not been processed yet. Downloading the original one...",
-              toastLength: Toast.LENGTH_SHORT);
-          _setVideoPlayerController(widget.file.getDownloadUrl());
-        }
-      });
+      if (!widget.file.isEncrypted) {
+        _setVideoPlayerController(widget.file.getStreamUrl());
+        _videoPlayerController.addListener(() {
+          if (_videoPlayerController.value.hasError) {
+            _logger.warning(_videoPlayerController.value.errorDescription);
+            showToast(
+                "The video has not been processed yet. Downloading the original one...",
+                toastLength: Toast.LENGTH_SHORT);
+            _setVideoPlayerController(widget.file.getDownloadUrl());
+          }
+        });
+      } else {
+        showToast("Downloading and decrypting video...",
+            toastLength: Toast.LENGTH_SHORT);
+        getFileFromServer(widget.file).then((file) {
+          _setVideoPlayerController(file.path);
+        });
+      }
     } else {
       widget.file.getAsset().then((asset) {
         asset.getMediaUrl().then((url) {
@@ -104,17 +112,11 @@ class _VideoWidgetState extends State<VideoWidget> {
   }
 
   Widget _getThumbnail() {
-    final thumbnail = widget.file.localID == null
-        ? CachedNetworkImage(
-            imageUrl: widget.file.getThumbnailUrl(),
-            fit: BoxFit.contain,
-          )
-        : ThumbnailWidget(
-            widget.file,
-            fit: BoxFit.contain,
-          );
     return Container(
-      child: thumbnail,
+      child: ThumbnailWidget(
+        widget.file,
+        fit: BoxFit.contain,
+      ),
       constraints: BoxConstraints.expand(),
     );
   }

+ 10 - 7
lib/utils/file_util.dart

@@ -8,6 +8,7 @@ import 'package:flutter_image_compress/flutter_image_compress.dart';
 import 'package:photo_manager/photo_manager.dart';
 import 'package:photos/core/cache/image_cache.dart';
 import 'package:photos/core/cache/thumbnail_cache.dart';
+import 'package:photos/core/cache/video_cache_manager.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/core/constants.dart';
 import 'package:photos/db/files_db.dart';
@@ -81,14 +82,15 @@ Future<Uint8List> getBytesFromDisk(File file, int quality) async {
 }
 
 Future<io.File> getFileFromServer(File file) async {
+  final cacheManager = file.fileType == FileType.video
+      ? VideoCacheManager()
+      : DefaultCacheManager();
   if (!file.isEncrypted) {
-    return DefaultCacheManager().getSingleFile(file.getDownloadUrl());
+    return cacheManager.getSingleFile(file.getDownloadUrl());
   } else {
-    return DefaultCacheManager()
-        .getFileFromCache(file.getDownloadUrl())
-        .then((info) {
+    return cacheManager.getFileFromCache(file.getDownloadUrl()).then((info) {
       if (info == null) {
-        return _downloadAndDecrypt(file);
+        return _downloadAndDecrypt(file, cacheManager);
       } else {
         return info.file;
       }
@@ -96,7 +98,8 @@ Future<io.File> getFileFromServer(File file) async {
   }
 }
 
-Future<io.File> _downloadAndDecrypt(File file) async {
+Future<io.File> _downloadAndDecrypt(
+    File file, BaseCacheManager cacheManager) async {
   final temporaryPath = Configuration.instance.getTempDirectory() +
       file.generatedID.toString() +
       ".aes";
@@ -104,6 +107,6 @@ Future<io.File> _downloadAndDecrypt(File file) async {
     final data = await CryptoUtil.decryptFileToData(
         temporaryPath, Configuration.instance.getKey());
     io.File(temporaryPath).deleteSync();
-    return DefaultCacheManager().putFile(file.getDownloadUrl(), data);
+    return cacheManager.putFile(file.getDownloadUrl(), data);
   });
 }