diff --git a/lib/ui/zoomable_image.dart b/lib/ui/zoomable_image.dart index e74688d68..03dfdb9ab 100644 --- a/lib/ui/zoomable_image.dart +++ b/lib/ui/zoomable_image.dart @@ -1,19 +1,15 @@ -import 'dart:io' as io; import 'package:cached_network_image/cached_network_image.dart'; -import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; -import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:logging/logging.dart'; import 'package:photos/core/cache/image_cache.dart'; import 'package:photos/core/cache/thumbnail_cache.dart'; -import 'package:photos/core/configuration.dart'; import 'package:photos/models/file.dart'; import 'package:photos/ui/loading_widget.dart'; import 'package:photo_view/photo_view.dart'; import 'package:photos/core/constants.dart'; -import 'package:photos/utils/crypto_util.dart'; +import 'package:photos/utils/file_util.dart'; class ZoomableImage extends StatefulWidget { final File photo; @@ -82,19 +78,12 @@ class _ZoomableImageState extends State void _loadNetworkImage() { if (!_photo.isEncrypted) { - _loadUnencryptedImage(); + _loadUnencryptedThumbnail(); } else { - _loadEncryptedImage(); - } - } - - void _loadUnencryptedImage() { - if (!_loadedSmallThumbnail && !_loadedFinalImage) { - _imageProvider = CachedNetworkImageProvider(_photo.getThumbnailUrl()); - _loadedSmallThumbnail = true; + _loadEncryptedThumbnail(); } if (!_loadedFinalImage) { - DefaultCacheManager().getSingleFile(_photo.getDownloadUrl()).then((file) { + getFileFromServer(_photo).then((file) { _onFinalImageLoaded( Image.file( file, @@ -105,7 +94,14 @@ class _ZoomableImageState extends State } } - void _loadEncryptedImage() { + void _loadUnencryptedThumbnail() { + if (!_loadedSmallThumbnail && !_loadedFinalImage) { + _imageProvider = CachedNetworkImageProvider(_photo.getThumbnailUrl()); + _loadedSmallThumbnail = true; + } + } + + void _loadEncryptedThumbnail() { if (!_loadedSmallThumbnail && !_loadedFinalImage) { if (ThumbnailFileLruCache.get(_photo) != null) { _imageProvider = Image.file( @@ -114,35 +110,6 @@ class _ZoomableImageState extends State _loadedSmallThumbnail = true; } } - if (!_loadedFinalImage) { - final url = _photo.getDownloadUrl(); - DefaultCacheManager().getFileFromCache(url).then((info) { - if (info == null) { - final temporaryPath = Configuration.instance.getTempDirectory() + - _photo.generatedID.toString() + - ".aes"; - Dio().download(url, temporaryPath).then((_) async { - final data = await CryptoUtil.decryptFileToData( - temporaryPath, Configuration.instance.getKey()); - io.File(temporaryPath).deleteSync(); - DefaultCacheManager().putFile(url, data); - _onFinalImageLoaded( - Image.memory( - data, - gaplessPlayback: true, - ).image, - context); - }); - } else { - _onFinalImageLoaded( - Image.memory( - info.file.readAsBytesSync(), - gaplessPlayback: true, - ).image, - context); - } - }); - } } void _loadLocalImage(BuildContext context) { diff --git a/lib/utils/file_util.dart b/lib/utils/file_util.dart index 7bdee59d6..e37e95a64 100644 --- a/lib/utils/file_util.dart +++ b/lib/utils/file_util.dart @@ -32,7 +32,7 @@ void preloadFile(File file) { return; } if (file.localID == null) { - _getBytesFromServer(file); + getFileFromServer(file); } else { if (FileLruCache.get(file) == null) { file.getAsset().then((asset) { @@ -60,13 +60,13 @@ void preloadLocalFileThumbnail(File file) { Future getBytes(File file, {int quality = 100}) async { if (file.localID == null) { - return _getBytesFromServer(file); + return getFileFromServer(file).then((file) => file.readAsBytesSync()); } else { - return await _getBytesFromDisk(file, quality); + return await getBytesFromDisk(file, quality); } } -Future _getBytesFromDisk(File file, int quality) async { +Future getBytesFromDisk(File file, int quality) async { final originalBytes = (await file.getAsset()).originBytes; if (extension(file.title) == ".HEIC" || quality != 100) { return originalBytes.then((bytes) { @@ -80,11 +80,9 @@ Future _getBytesFromDisk(File file, int quality) async { } } -Future _getBytesFromServer(File file) async { +Future getFileFromServer(File file) async { if (!file.isEncrypted) { - return DefaultCacheManager() - .getSingleFile(file.getDownloadUrl()) - .then((file) => file.readAsBytesSync()); + return DefaultCacheManager().getSingleFile(file.getDownloadUrl()); } else { return DefaultCacheManager() .getFileFromCache(file.getDownloadUrl()) @@ -92,13 +90,13 @@ Future _getBytesFromServer(File file) async { if (info == null) { return _downloadAndDecrypt(file); } else { - return info.file.readAsBytesSync(); + return info.file; } }); } } -Future _downloadAndDecrypt(File file) async { +Future _downloadAndDecrypt(File file) async { final temporaryPath = Configuration.instance.getTempDirectory() + file.generatedID.toString() + ".aes"; @@ -106,7 +104,6 @@ Future _downloadAndDecrypt(File file) async { final data = await CryptoUtil.decryptFileToData( temporaryPath, Configuration.instance.getKey()); io.File(temporaryPath).deleteSync(); - DefaultCacheManager().putFile(file.getDownloadUrl(), data); - return data; + return DefaultCacheManager().putFile(file.getDownloadUrl(), data); }); }