file_download_util.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'dart:io' as io;
  2. import 'dart:typed_data';
  3. import 'package:dio/dio.dart';
  4. import 'package:logging/logging.dart';
  5. import 'package:photos/core/configuration.dart';
  6. import 'package:photos/core/network/network.dart';
  7. import 'package:photos/models/file.dart' as ente;
  8. import 'package:photos/services/collections_service.dart';
  9. import 'package:photos/utils/crypto_util.dart';
  10. final _logger = Logger("file_download_util");
  11. Future<io.File?> downloadAndDecrypt(
  12. ente.File file, {
  13. ProgressCallback? progressCallback,
  14. }) {
  15. _logger.info("Downloading file " + file.uploadedFileID.toString());
  16. final encryptedFilePath = Configuration.instance.getTempDirectory() +
  17. file.generatedID.toString() +
  18. ".encrypted";
  19. final encryptedFile = io.File(encryptedFilePath);
  20. final startTime = DateTime.now().millisecondsSinceEpoch;
  21. return NetworkClient.instance
  22. .getDio()
  23. .download(
  24. file.downloadUrl,
  25. encryptedFilePath,
  26. options: Options(
  27. headers: {"X-Auth-Token": Configuration.instance.getToken()},
  28. ),
  29. onReceiveProgress: progressCallback,
  30. )
  31. .then((response) async {
  32. if (response.statusCode != 200) {
  33. _logger.warning("Could not download file: ", response.toString());
  34. return null;
  35. } else if (!encryptedFile.existsSync()) {
  36. _logger.warning("File was not downloaded correctly.");
  37. return null;
  38. }
  39. _logger.info("File downloaded: " + file.uploadedFileID.toString());
  40. _logger.info(
  41. "Download speed: " +
  42. (await io.File(encryptedFilePath).length() /
  43. (DateTime.now().millisecondsSinceEpoch - startTime))
  44. .toString() +
  45. "kBps",
  46. );
  47. final decryptedFilePath = Configuration.instance.getTempDirectory() +
  48. file.generatedID.toString() +
  49. ".decrypted";
  50. final decryptedFile = io.File(decryptedFilePath);
  51. await CryptoUtil.decryptFile(
  52. encryptedFilePath,
  53. decryptedFilePath,
  54. CryptoUtil.base642bin(file.fileDecryptionHeader!),
  55. getFileKey(file),
  56. );
  57. _logger.info("File decrypted: " + file.uploadedFileID.toString());
  58. await encryptedFile.delete();
  59. return decryptedFile;
  60. });
  61. }
  62. Uint8List getFileKey(ente.File file) {
  63. final encryptedKey = CryptoUtil.base642bin(file.encryptedKey!);
  64. final nonce = CryptoUtil.base642bin(file.keyDecryptionNonce!);
  65. final collectionKey =
  66. CollectionsService.instance.getCollectionKey(file.collectionID!);
  67. return CryptoUtil.decryptSync(encryptedKey, collectionKey, nonce);
  68. }