Move file decrypting logic out of ZoomableImage
This commit is contained in:
parent
44dddb818f
commit
c3265b91ab
2 changed files with 21 additions and 57 deletions
|
@ -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<ZoomableImage>
|
|||
|
||||
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<ZoomableImage>
|
|||
}
|
||||
}
|
||||
|
||||
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<ZoomableImage>
|
|||
_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) {
|
||||
|
|
|
@ -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<Uint8List> 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<Uint8List> _getBytesFromDisk(File file, int quality) async {
|
||||
Future<Uint8List> 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<Uint8List> _getBytesFromDisk(File file, int quality) async {
|
|||
}
|
||||
}
|
||||
|
||||
Future<Uint8List> _getBytesFromServer(File file) async {
|
||||
Future<io.File> 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<Uint8List> _getBytesFromServer(File file) async {
|
|||
if (info == null) {
|
||||
return _downloadAndDecrypt(file);
|
||||
} else {
|
||||
return info.file.readAsBytesSync();
|
||||
return info.file;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<Uint8List> _downloadAndDecrypt(File file) async {
|
||||
Future<io.File> _downloadAndDecrypt(File file) async {
|
||||
final temporaryPath = Configuration.instance.getTempDirectory() +
|
||||
file.generatedID.toString() +
|
||||
".aes";
|
||||
|
@ -106,7 +104,6 @@ Future<Uint8List> _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);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue