ente/lib/utils/file_util.dart

204 lines
6.5 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:io' as io;
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
2021-07-21 20:47:43 +00:00
import 'package:flutter_sodium/flutter_sodium.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart';
2020-08-13 01:24:48 +00:00
import 'package:photos/core/cache/image_cache.dart';
2020-08-13 20:03:29 +00:00
import 'package:photos/core/cache/video_cache_manager.dart';
import 'package:photos/core/configuration.dart';
2020-07-29 19:07:23 +00:00
import 'package:photos/core/constants.dart';
2020-11-19 18:22:30 +00:00
import 'package:photos/core/network.dart';
2021-05-29 21:58:42 +00:00
import 'package:photos/models/file.dart' as ente;
2020-08-13 01:24:48 +00:00
import 'package:photos/models/file_type.dart';
import 'package:photos/services/collections_service.dart';
2021-05-02 20:52:56 +00:00
import 'package:photos/utils/thumbnail_util.dart';
import 'crypto_util.dart';
import 'file_uploader_util.dart';
final _logger = Logger("FileUtil");
2020-09-28 16:16:04 +00:00
2021-05-29 21:58:42 +00:00
void preloadFile(ente.File file) {
2020-08-13 01:24:48 +00:00
if (file.fileType == FileType.video) {
return;
}
2021-06-11 07:19:19 +00:00
getFile(file);
2021-06-02 13:54:31 +00:00
}
2021-06-11 07:19:19 +00:00
Future<io.File> getFile(ente.File file) async {
2021-07-19 05:37:34 +00:00
if (file.isRemoteFile()) {
2021-06-02 13:54:31 +00:00
return getFileFromServer(file);
} else {
2021-06-02 13:54:31 +00:00
final cachedFile = FileLruCache.get(file);
if (cachedFile == null) {
final diskFile = await _getLocalDiskFile(file);
2021-06-02 13:54:31 +00:00
FileLruCache.put(file, diskFile);
return diskFile;
}
2021-06-02 13:54:31 +00:00
return cachedFile;
}
}
Future<io.File> _getLocalDiskFile(ente.File file) async {
2021-07-24 17:33:59 +00:00
if (file.isSharedMediaToAppSandbox()) {
2021-07-24 13:38:47 +00:00
var localFile = io.File(getSharedMediaFilePath(file));
return localFile.exists().then((exist) {
return exist ? localFile : null;
});
} else {
return file.getAsset().then((asset) async {
if (asset == null || !(await asset.exists)) {
return null;
}
return asset.file;
});
}
}
String getSharedMediaFilePath(ente.File file) {
return Configuration.instance.getSharedMediaCacheDirectory()
+ "/" + file.localID.replaceAll(kSharedMediaIdentifier, '');
}
2021-05-29 21:58:42 +00:00
void preloadThumbnail(ente.File file) {
2021-07-19 05:37:34 +00:00
if (file.isRemoteFile()) {
2021-04-27 16:29:58 +00:00
getThumbnailFromServer(file);
} else {
getThumbnailFromLocal(file);
2021-04-27 16:29:58 +00:00
}
2020-07-29 19:07:23 +00:00
}
2020-08-13 01:24:48 +00:00
final Map<int, Future<io.File>> fileDownloadsInProgress =
Map<int, Future<io.File>>();
2021-05-29 21:58:42 +00:00
Future<io.File> getFileFromServer(ente.File file,
{ProgressCallback progressCallback}) async {
2020-08-13 20:03:29 +00:00
final cacheManager = file.fileType == FileType.video
2021-05-02 16:05:36 +00:00
? VideoCacheManager.instance
2020-08-13 20:03:29 +00:00
: DefaultCacheManager();
return cacheManager.getFileFromCache(file.getDownloadUrl()).then((info) {
if (info == null) {
if (!fileDownloadsInProgress.containsKey(file.uploadedFileID)) {
fileDownloadsInProgress[file.uploadedFileID] = _downloadAndDecrypt(
file,
cacheManager,
progressCallback: progressCallback,
);
2020-08-13 01:24:48 +00:00
}
return fileDownloadsInProgress[file.uploadedFileID];
} else {
return info.file;
}
});
2020-08-13 01:24:48 +00:00
}
2021-05-29 21:58:42 +00:00
Future<io.File> _downloadAndDecrypt(
ente.File file, BaseCacheManager cacheManager,
{ProgressCallback progressCallback}) async {
_logger.info("Downloading file " + file.uploadedFileID.toString());
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() +
".decrypted";
final encryptedFile = io.File(encryptedFilePath);
final decryptedFile = io.File(decryptedFilePath);
2020-10-18 21:05:32 +00:00
final startTime = DateTime.now().millisecondsSinceEpoch;
2020-11-19 18:22:30 +00:00
return Network.instance
.getDio()
.download(
2020-11-19 18:22:30 +00:00
file.getDownloadUrl(),
encryptedFilePath,
options: Options(
headers: {"X-Auth-Token": Configuration.instance.getToken()},
),
2020-11-19 18:22:30 +00:00
onReceiveProgress: progressCallback,
)
2020-09-28 16:15:21 +00:00
.then((response) async {
if (response.statusCode != 200) {
_logger.warning("Could not download file: ", response.toString());
2020-09-28 16:15:21 +00:00
return null;
} else if (!encryptedFile.existsSync()) {
_logger.warning("File was not downloaded correctly.");
return null;
2020-09-28 16:15:21 +00:00
}
_logger.info("File downloaded: " + file.uploadedFileID.toString());
_logger.info("Download speed: " +
2020-10-18 21:05:32 +00:00
(io.File(encryptedFilePath).lengthSync() /
(DateTime.now().millisecondsSinceEpoch - startTime))
.toString() +
"kBps");
2020-10-10 22:40:18 +00:00
await CryptoUtil.decryptFile(encryptedFilePath, decryptedFilePath,
Sodium.base642bin(file.fileDecryptionHeader), decryptFileKey(file));
_logger.info("File decrypted: " + file.uploadedFileID.toString());
2020-11-20 11:05:17 +00:00
encryptedFile.deleteSync();
var fileExtension = "unknown";
try {
fileExtension = extension(file.title).substring(1).toLowerCase();
} catch (e) {
2021-06-29 04:10:24 +00:00
_logger.severe("Could not capture file extension");
}
2020-11-20 11:05:17 +00:00
var outputFile = decryptedFile;
if ((fileExtension == "unknown" && file.fileType == FileType.image) ||
2021-06-29 04:10:24 +00:00
(io.Platform.isAndroid && fileExtension == "heic")) {
2020-11-20 11:05:17 +00:00
outputFile = await FlutterImageCompress.compressAndGetFile(
decryptedFilePath,
decryptedFilePath + ".jpg",
keepExif: true,
);
decryptedFile.deleteSync();
}
2020-09-25 20:25:03 +00:00
final cachedFile = await cacheManager.putFile(
file.getDownloadUrl(),
2020-11-20 11:05:17 +00:00
outputFile.readAsBytesSync(),
eTag: file.getDownloadUrl(),
maxAge: Duration(days: 365),
fileExtension: fileExtension,
);
2020-11-20 11:05:17 +00:00
outputFile.deleteSync();
fileDownloadsInProgress.remove(file.uploadedFileID);
2020-09-25 20:25:03 +00:00
return cachedFile;
2020-10-06 23:56:37 +00:00
}).catchError((e) {
fileDownloadsInProgress.remove(file.uploadedFileID);
2020-08-13 01:24:48 +00:00
});
}
2020-08-13 21:33:31 +00:00
2021-05-29 21:58:42 +00:00
Uint8List decryptFileKey(ente.File file) {
final encryptedKey = Sodium.base642bin(file.encryptedKey);
final nonce = Sodium.base642bin(file.keyDecryptionNonce);
2020-10-12 20:35:33 +00:00
final collectionKey =
CollectionsService.instance.getCollectionKey(file.collectionID);
return CryptoUtil.decryptSync(encryptedKey, collectionKey, nonce);
}
Future<Uint8List> compressThumbnail(Uint8List thumbnail) {
return FlutterImageCompress.compressWithList(
thumbnail,
2021-07-21 20:47:43 +00:00
minHeight: kCompressedThumbnailResolution,
minWidth: kCompressedThumbnailResolution,
quality: 25,
);
}
2021-05-29 21:58:42 +00:00
void clearCache(ente.File file) {
if (file.fileType == FileType.video) {
2021-05-02 16:05:36 +00:00
VideoCacheManager.instance.removeFile(file.getDownloadUrl());
} else {
DefaultCacheManager().removeFile(file.getDownloadUrl());
}
final cachedThumbnail = io.File(
Configuration.instance.getThumbnailCacheDirectory() +
"/" +
file.uploadedFileID.toString());
if (cachedThumbnail.existsSync()) {
cachedThumbnail.deleteSync();
}
}