Augment the getBytes function to take care of encrypted files
This commit is contained in:
parent
fa53398887
commit
600fd58a46
3 changed files with 56 additions and 29 deletions
|
@ -1,8 +1,3 @@
|
|||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:photo_manager/photo_manager.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:photos/core/configuration.dart';
|
||||
|
@ -77,28 +72,6 @@ class File {
|
|||
return AssetEntity.fromId(localID);
|
||||
}
|
||||
|
||||
Future<Uint8List> getBytes({int quality = 100}) async {
|
||||
if (localID == null) {
|
||||
return HttpClient().getUrl(Uri.parse(getDownloadUrl())).then((request) {
|
||||
return request.close().then((response) {
|
||||
return consolidateHttpClientResponseBytes(response);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
final originalBytes = (await getAsset()).originBytes;
|
||||
if (extension(title) == ".HEIC" || quality != 100) {
|
||||
return originalBytes.then((bytes) {
|
||||
return FlutterImageCompress.compressWithList(bytes, quality: quality)
|
||||
.then((converted) {
|
||||
return Uint8List.fromList(converted);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return originalBytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void applyMetadata(Map<String, dynamic> metadata) {
|
||||
localID = metadata["localID"];
|
||||
title = metadata["title"];
|
||||
|
|
|
@ -1,9 +1,19 @@
|
|||
import 'dart:io' as io;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:path/path.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:photo_manager/photo_manager.dart';
|
||||
import 'package:photos/core/cache/thumbnail_cache.dart';
|
||||
import 'package:photos/core/configuration.dart';
|
||||
import 'package:photos/core/constants.dart';
|
||||
import 'package:photos/db/files_db.dart';
|
||||
import 'package:photos/models/file.dart';
|
||||
|
||||
import 'crypto_util.dart';
|
||||
|
||||
Future<void> deleteFiles(List<File> files,
|
||||
{bool deleteEveryWhere = false}) async {
|
||||
await PhotoManager.editor
|
||||
|
@ -19,6 +29,49 @@ void preloadFile(File file) {
|
|||
// TODO
|
||||
}
|
||||
|
||||
Future<Uint8List> getBytes(File file, {int quality = 100}) async {
|
||||
if (file.localID == null) {
|
||||
if (!file.isEncrypted) {
|
||||
return DefaultCacheManager()
|
||||
.getSingleFile(file.getDownloadUrl())
|
||||
.then((file) => file.readAsBytesSync());
|
||||
} else {
|
||||
return DefaultCacheManager()
|
||||
.getFileFromCache(file.getDownloadUrl())
|
||||
.then((info) {
|
||||
if (info == null) {
|
||||
final temporaryPath = Configuration.instance.getTempDirectory() +
|
||||
file.generatedID.toString() +
|
||||
".aes";
|
||||
return Dio()
|
||||
.download(file.getDownloadUrl(), temporaryPath)
|
||||
.then((_) async {
|
||||
final data = await CryptoUtil.decryptFileToData(
|
||||
temporaryPath, Configuration.instance.getKey());
|
||||
io.File(temporaryPath).deleteSync();
|
||||
DefaultCacheManager().putFile(file.getDownloadUrl(), data);
|
||||
return data;
|
||||
});
|
||||
} else {
|
||||
return info.file.readAsBytesSync();
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
final originalBytes = (await file.getAsset()).originBytes;
|
||||
if (extension(file.title) == ".HEIC" || quality != 100) {
|
||||
return originalBytes.then((bytes) {
|
||||
return FlutterImageCompress.compressWithList(bytes, quality: quality)
|
||||
.then((converted) {
|
||||
return Uint8List.fromList(converted);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return originalBytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void preloadLocalFileThumbnail(File file) {
|
||||
if (file.localID == null ||
|
||||
ThumbnailLruCache.get(file, THUMBNAIL_SMALL_SIZE) != null) {
|
||||
|
|
|
@ -5,11 +5,12 @@ import 'package:flutter/widgets.dart';
|
|||
import 'package:photos/models/file.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:photos/utils/dialog_util.dart';
|
||||
import 'package:photos/utils/file_util.dart';
|
||||
|
||||
Future<void> share(BuildContext context, File file) async {
|
||||
final dialog = createProgressDialog(context, "Preparing...");
|
||||
await dialog.show();
|
||||
final bytes = await file.getBytes();
|
||||
final bytes = await getBytes(file);
|
||||
final filename = _getFilename(file.title);
|
||||
final ext = extension(file.title);
|
||||
final shareExt = file.title.endsWith(".HEIC")
|
||||
|
@ -24,7 +25,7 @@ Future<void> shareMultiple(BuildContext context, List<File> files) async {
|
|||
await dialog.show();
|
||||
final shareContent = Map<String, Uint8List>();
|
||||
for (File file in files) {
|
||||
shareContent[_getFilename(file.title)] = await file.getBytes();
|
||||
shareContent[_getFilename(file.title)] = await getBytes(file);
|
||||
}
|
||||
await dialog.hide();
|
||||
return Share.files("images", shareContent, "*/*");
|
||||
|
|
Loading…
Add table
Reference in a new issue