2020-08-13 01:18:41 +00:00
|
|
|
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';
|
2020-07-29 07:53:13 +00:00
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
2020-08-13 01:24:48 +00:00
|
|
|
import 'package:photos/core/cache/image_cache.dart';
|
2020-07-29 19:07:23 +00:00
|
|
|
import 'package:photos/core/cache/thumbnail_cache.dart';
|
2020-08-13 21:33:31 +00:00
|
|
|
import 'package:photos/core/cache/thumbnail_cache_manager.dart';
|
2020-08-13 20:03:29 +00:00
|
|
|
import 'package:photos/core/cache/video_cache_manager.dart';
|
2020-08-13 01:18:41 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
2020-07-29 19:07:23 +00:00
|
|
|
import 'package:photos/core/constants.dart';
|
2020-07-29 07:53:13 +00:00
|
|
|
import 'package:photos/db/files_db.dart';
|
2020-09-25 19:07:32 +00:00
|
|
|
import 'package:photos/models/encrypted_file_attributes.dart';
|
|
|
|
import 'package:photos/models/encryption_attribute.dart';
|
2020-07-29 07:53:13 +00:00
|
|
|
import 'package:photos/models/file.dart';
|
2020-08-13 01:24:48 +00:00
|
|
|
import 'package:photos/models/file_type.dart';
|
2020-07-29 07:53:13 +00:00
|
|
|
|
2020-08-13 01:18:41 +00:00
|
|
|
import 'crypto_util.dart';
|
|
|
|
|
2020-07-29 07:55:29 +00:00
|
|
|
Future<void> deleteFiles(List<File> files,
|
2020-07-29 07:53:13 +00:00
|
|
|
{bool deleteEveryWhere = false}) async {
|
|
|
|
await PhotoManager.editor
|
2020-08-09 22:34:59 +00:00
|
|
|
.deleteWithIds(files.map((file) => file.localID).toList());
|
2020-07-29 07:53:13 +00:00
|
|
|
for (File file in files) {
|
|
|
|
deleteEveryWhere
|
|
|
|
? await FilesDB.instance.markForDeletion(file)
|
|
|
|
: await FilesDB.instance.delete(file);
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 15:48:13 +00:00
|
|
|
|
|
|
|
void preloadFile(File file) {
|
2020-08-13 01:24:48 +00:00
|
|
|
if (file.fileType == FileType.video) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-13 01:18:41 +00:00
|
|
|
if (file.localID == null) {
|
2020-08-13 01:35:57 +00:00
|
|
|
getFileFromServer(file);
|
2020-08-13 01:18:41 +00:00
|
|
|
} else {
|
2020-08-13 01:24:48 +00:00
|
|
|
if (FileLruCache.get(file) == null) {
|
|
|
|
file.getAsset().then((asset) {
|
|
|
|
asset.file.then((assetFile) {
|
|
|
|
FileLruCache.put(file, assetFile);
|
2020-08-13 01:18:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:07:23 +00:00
|
|
|
void preloadLocalFileThumbnail(File file) {
|
2020-08-09 22:34:59 +00:00
|
|
|
if (file.localID == null ||
|
2020-07-29 19:07:23 +00:00
|
|
|
ThumbnailLruCache.get(file, THUMBNAIL_SMALL_SIZE) != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
file.getAsset().then((asset) {
|
|
|
|
asset
|
|
|
|
.thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE)
|
|
|
|
.then((data) {
|
|
|
|
ThumbnailLruCache.put(file, THUMBNAIL_SMALL_SIZE, data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-08-13 01:24:48 +00:00
|
|
|
|
2020-08-24 23:59:57 +00:00
|
|
|
Future<io.File> getNativeFile(File file) async {
|
|
|
|
if (file.localID == null) {
|
|
|
|
return getFileFromServer(file);
|
|
|
|
} else {
|
|
|
|
return file.getAsset().then((asset) => asset.file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 01:24:48 +00:00
|
|
|
Future<Uint8List> getBytes(File file, {int quality = 100}) async {
|
|
|
|
if (file.localID == null) {
|
2020-08-13 01:35:57 +00:00
|
|
|
return getFileFromServer(file).then((file) => file.readAsBytesSync());
|
2020-08-13 01:24:48 +00:00
|
|
|
} else {
|
2020-08-13 22:31:37 +00:00
|
|
|
return await getBytesFromDisk(file, quality: quality);
|
2020-08-13 01:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 22:31:37 +00:00
|
|
|
Future<Uint8List> getBytesFromDisk(File file, {int quality = 100}) async {
|
2020-08-13 01:24:48 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 00:01:37 +00:00
|
|
|
Future<io.File> getFileFromServer(File file,
|
|
|
|
{ProgressCallback progressCallback}) async {
|
2020-08-13 20:03:29 +00:00
|
|
|
final cacheManager = file.fileType == FileType.video
|
|
|
|
? VideoCacheManager()
|
|
|
|
: DefaultCacheManager();
|
2020-08-13 01:24:48 +00:00
|
|
|
if (!file.isEncrypted) {
|
2020-08-13 20:03:29 +00:00
|
|
|
return cacheManager.getSingleFile(file.getDownloadUrl());
|
2020-08-13 01:24:48 +00:00
|
|
|
} else {
|
2020-08-13 20:03:29 +00:00
|
|
|
return cacheManager.getFileFromCache(file.getDownloadUrl()).then((info) {
|
2020-08-13 01:24:48 +00:00
|
|
|
if (info == null) {
|
2020-08-14 00:01:37 +00:00
|
|
|
return _downloadAndDecrypt(
|
|
|
|
file,
|
|
|
|
cacheManager,
|
|
|
|
progressCallback: progressCallback,
|
|
|
|
);
|
2020-08-13 01:24:48 +00:00
|
|
|
} else {
|
2020-08-13 01:35:57 +00:00
|
|
|
return info.file;
|
2020-08-13 01:24:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 21:33:31 +00:00
|
|
|
Future<io.File> getThumbnailFromServer(File file) async {
|
|
|
|
if (!file.isEncrypted) {
|
2020-08-13 23:47:44 +00:00
|
|
|
return ThumbnailCacheManager()
|
|
|
|
.getSingleFile(file.getThumbnailUrl())
|
|
|
|
.then((data) {
|
|
|
|
ThumbnailFileLruCache.put(file, data);
|
|
|
|
return data;
|
|
|
|
});
|
2020-08-13 21:33:31 +00:00
|
|
|
} else {
|
|
|
|
return ThumbnailCacheManager()
|
|
|
|
.getFileFromCache(file.getThumbnailUrl())
|
|
|
|
.then((info) {
|
|
|
|
if (info == null) {
|
2020-08-13 23:47:44 +00:00
|
|
|
return _downloadAndDecryptThumbnail(file).then((data) {
|
|
|
|
ThumbnailFileLruCache.put(file, data);
|
|
|
|
return data;
|
|
|
|
});
|
2020-08-13 21:33:31 +00:00
|
|
|
} else {
|
2020-08-13 23:47:44 +00:00
|
|
|
ThumbnailFileLruCache.put(file, info.file);
|
2020-08-13 21:33:31 +00:00
|
|
|
return info.file;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 00:01:37 +00:00
|
|
|
Future<io.File> _downloadAndDecrypt(File file, BaseCacheManager cacheManager,
|
|
|
|
{ProgressCallback progressCallback}) async {
|
2020-09-25 19:07:32 +00:00
|
|
|
final encryptedFilePath = Configuration.instance.getTempDirectory() +
|
|
|
|
file.generatedID.toString() +
|
|
|
|
".encrypted";
|
|
|
|
final decryptedFilePath = Configuration.instance.getTempDirectory() +
|
2020-08-13 01:24:48 +00:00
|
|
|
file.generatedID.toString() +
|
2020-09-25 19:07:32 +00:00
|
|
|
".decrypted";
|
|
|
|
final encryptedFile = io.File(encryptedFilePath);
|
|
|
|
final decryptedFile = io.File(decryptedFilePath);
|
2020-08-14 00:01:37 +00:00
|
|
|
return Dio()
|
|
|
|
.download(
|
|
|
|
file.getDownloadUrl(),
|
2020-09-25 19:07:32 +00:00
|
|
|
encryptedFilePath,
|
2020-08-14 00:01:37 +00:00
|
|
|
onReceiveProgress: progressCallback,
|
|
|
|
)
|
|
|
|
.then((_) async {
|
2020-09-25 19:07:32 +00:00
|
|
|
var attributes = ChaChaAttributes(
|
|
|
|
EncryptionAttribute(base64: file.fileDecryptionParams.header),
|
|
|
|
EncryptionAttribute(
|
|
|
|
bytes: await CryptoUtil.decrypt(
|
|
|
|
file.fileDecryptionParams.encryptedKey,
|
|
|
|
Configuration.instance.getBase64EncodedKey(),
|
|
|
|
file.fileDecryptionParams.nonce,
|
|
|
|
)));
|
|
|
|
await CryptoUtil.chachaDecrypt(encryptedFile, decryptedFile, attributes);
|
|
|
|
encryptedFile.deleteSync();
|
|
|
|
decryptedFile.deleteSync();
|
2020-09-17 14:29:42 +00:00
|
|
|
final fileExtension = extension(file.title).substring(1).toLowerCase();
|
2020-09-17 18:41:14 +00:00
|
|
|
return cacheManager.putFile(
|
|
|
|
file.getDownloadUrl(),
|
2020-09-25 19:07:32 +00:00
|
|
|
decryptedFile.readAsBytesSync(),
|
2020-09-17 18:41:14 +00:00
|
|
|
eTag: file.getDownloadUrl(),
|
|
|
|
maxAge: Duration(days: 365),
|
|
|
|
fileExtension: fileExtension,
|
|
|
|
);
|
2020-08-13 01:24:48 +00:00
|
|
|
});
|
|
|
|
}
|
2020-08-13 21:33:31 +00:00
|
|
|
|
|
|
|
Future<io.File> _downloadAndDecryptThumbnail(File file) async {
|
|
|
|
final temporaryPath = Configuration.instance.getTempDirectory() +
|
|
|
|
file.generatedID.toString() +
|
2020-09-25 19:07:32 +00:00
|
|
|
"_thumbnail.decrypted";
|
2020-09-17 18:41:14 +00:00
|
|
|
return Dio().download(file.getThumbnailUrl(), temporaryPath).then((_) async {
|
2020-09-25 19:07:32 +00:00
|
|
|
final encryptedFile = io.File(temporaryPath);
|
|
|
|
final data = await CryptoUtil.decryptWithDecryptionParams(
|
|
|
|
encryptedFile.readAsBytesSync(),
|
|
|
|
file.thumbnailDecryptionParams,
|
|
|
|
Configuration.instance.getBase64EncodedKey());
|
|
|
|
encryptedFile.deleteSync();
|
2020-09-17 18:41:14 +00:00
|
|
|
return ThumbnailCacheManager().putFile(
|
|
|
|
file.getThumbnailUrl(),
|
|
|
|
data,
|
|
|
|
eTag: file.getThumbnailUrl(),
|
|
|
|
maxAge: Duration(days: 365),
|
|
|
|
);
|
2020-08-13 21:33:31 +00:00
|
|
|
});
|
|
|
|
}
|