file.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import 'dart:io' as io;
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter_sodium/flutter_sodium.dart';
  4. import 'package:path/path.dart';
  5. import 'package:photo_manager/photo_manager.dart';
  6. import 'package:photos/core/configuration.dart';
  7. import 'package:photos/core/constants.dart';
  8. import 'package:photos/models/magic_metadata.dart';
  9. import 'package:photos/models/file_type.dart';
  10. import 'package:photos/models/location.dart';
  11. import 'package:photos/services/feature_flag_service.dart';
  12. import 'package:photos/utils/crypto_util.dart';
  13. class File {
  14. int generatedID;
  15. int uploadedFileID;
  16. int ownerID;
  17. int collectionID;
  18. String localID;
  19. String title;
  20. String deviceFolder;
  21. int creationTime;
  22. int modificationTime;
  23. int updationTime;
  24. Location location;
  25. FileType fileType;
  26. int fileSubType;
  27. int duration;
  28. String exif;
  29. String hash;
  30. int metadataVersion;
  31. String encryptedKey;
  32. String keyDecryptionNonce;
  33. String fileDecryptionHeader;
  34. String thumbnailDecryptionHeader;
  35. String metadataDecryptionHeader;
  36. String mMdEncodedJson;
  37. int mMdVersion = 0;
  38. MagicMetadata _mmd;
  39. MagicMetadata get magicMetadata =>
  40. _mmd ?? MagicMetadata.fromEncodedJson(mMdEncodedJson ?? '{}');
  41. set magicMetadata (val) => _mmd = val;
  42. static const kCurrentMetadataVersion = 1;
  43. File();
  44. static Future<File> fromAsset(String pathName, AssetEntity asset) async {
  45. File file = File();
  46. file.localID = asset.id;
  47. file.title = asset.title;
  48. file.deviceFolder = pathName;
  49. file.location = Location(asset.latitude, asset.longitude);
  50. file.fileType = _fileTypeFromAsset(asset);
  51. file.creationTime = asset.createDateTime.microsecondsSinceEpoch;
  52. if (file.creationTime == 0) {
  53. try {
  54. final parsedDateTime = DateTime.parse(
  55. basenameWithoutExtension(file.title)
  56. .replaceAll("IMG_", "")
  57. .replaceAll("DCIM_", "")
  58. .replaceAll("_", " "));
  59. file.creationTime = parsedDateTime.microsecondsSinceEpoch;
  60. } catch (e) {
  61. file.creationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  62. }
  63. }
  64. file.modificationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  65. file.fileSubType = asset.subTypes;
  66. file.metadataVersion = kCurrentMetadataVersion;
  67. return file;
  68. }
  69. static FileType _fileTypeFromAsset(AssetEntity asset) {
  70. FileType type = FileType.image;
  71. switch (asset.type) {
  72. case AssetType.image:
  73. type = FileType.image;
  74. // PHAssetMediaSubtype.photoLive.rawValue is 8
  75. // This hack should go away once photos_manager support livePhotos
  76. if (asset.subTypes != null &&
  77. asset.subTypes > -1 &&
  78. (asset.subTypes & 8) != 0) {
  79. type = FileType.livePhoto;
  80. }
  81. break;
  82. case AssetType.video:
  83. type = FileType.video;
  84. break;
  85. default:
  86. type = FileType.other;
  87. break;
  88. }
  89. return type;
  90. }
  91. Future<AssetEntity> getAsset() {
  92. if (localID == null) {
  93. return Future.value(null);
  94. }
  95. return AssetEntity.fromId(localID);
  96. }
  97. void applyMetadata(Map<String, dynamic> metadata) {
  98. localID = metadata["localID"];
  99. title = metadata["title"];
  100. deviceFolder = metadata["deviceFolder"];
  101. creationTime = metadata["creationTime"] ?? 0;
  102. modificationTime = metadata["modificationTime"] ?? creationTime;
  103. final latitude = double.tryParse(metadata["latitude"].toString());
  104. final longitude = double.tryParse(metadata["longitude"].toString());
  105. if (latitude == null || longitude == null) {
  106. location = null;
  107. } else {
  108. location = Location(latitude, longitude);
  109. }
  110. fileType = getFileType(metadata["fileType"]);
  111. fileSubType = metadata["subType"] ?? -1;
  112. duration = metadata["duration"] ?? 0;
  113. exif = metadata["exif"];
  114. hash = metadata["hash"];
  115. metadataVersion = metadata["version"] ?? 0;
  116. }
  117. Future<Map<String, dynamic>> getMetadata(io.File sourceFile) async {
  118. final metadata = <String, dynamic>{};
  119. metadata["localID"] = isSharedMediaToAppSandbox() ? null : localID;
  120. metadata["title"] = title;
  121. metadata["deviceFolder"] = deviceFolder;
  122. metadata["creationTime"] = creationTime;
  123. metadata["modificationTime"] = modificationTime;
  124. if (location != null &&
  125. location.latitude != null &&
  126. location.longitude != null) {
  127. metadata["latitude"] = location.latitude;
  128. metadata["longitude"] = location.longitude;
  129. }
  130. metadata["fileType"] = fileType.index;
  131. final asset = await getAsset();
  132. // asset can be null for files shared to app
  133. if (asset != null) {
  134. fileSubType = asset.subTypes;
  135. metadata["subType"] = fileSubType;
  136. if (fileType == FileType.video) {
  137. duration = asset.duration;
  138. metadata["duration"] = duration;
  139. }
  140. }
  141. hash = Sodium.bin2base64(await CryptoUtil.getHash(sourceFile));
  142. metadata["hash"] = hash;
  143. metadata["version"] = metadataVersion;
  144. return metadata;
  145. }
  146. String getDownloadUrl() {
  147. if (kDebugMode || FeatureFlagService.instance.disableCFWorker()) {
  148. return Configuration.instance.getHttpEndpoint() +
  149. "/files/download/" +
  150. uploadedFileID.toString();
  151. } else {
  152. return "https://files.ente.workers.dev/?fileID=" +
  153. uploadedFileID.toString();
  154. }
  155. }
  156. String getThumbnailUrl() {
  157. if (kDebugMode || FeatureFlagService.instance.disableCFWorker()) {
  158. return Configuration.instance.getHttpEndpoint() +
  159. "/files/preview/" +
  160. uploadedFileID.toString();
  161. } else {
  162. return "https://thumbnails.ente.workers.dev/?fileID=" +
  163. uploadedFileID.toString();
  164. }
  165. }
  166. // returns true if the file isn't available in the user's gallery
  167. bool isRemoteFile() {
  168. return localID == null && uploadedFileID != null;
  169. }
  170. bool isSharedMediaToAppSandbox() {
  171. return localID != null && localID.startsWith(kSharedMediaIdentifier);
  172. }
  173. bool hasLocation() {
  174. return location != null &&
  175. (location.longitude != 0 || location.latitude != 0);
  176. }
  177. @override
  178. String toString() {
  179. return '''File(generatedID: $generatedID, localID: $localID,
  180. uploadedFileId: $uploadedFileID, modificationTime: $modificationTime,
  181. ownerID: $ownerID, collectionID: $collectionID, updationTime: $updationTime)''';
  182. }
  183. @override
  184. bool operator ==(Object o) {
  185. if (identical(this, o)) return true;
  186. return o is File &&
  187. o.generatedID == generatedID &&
  188. o.uploadedFileID == uploadedFileID &&
  189. o.localID == localID;
  190. }
  191. @override
  192. int get hashCode {
  193. return generatedID.hashCode ^ uploadedFileID.hashCode ^ localID.hashCode;
  194. }
  195. String tag() {
  196. return "local_" +
  197. localID.toString() +
  198. ":remote_" +
  199. uploadedFileID.toString() +
  200. ":generated_" +
  201. generatedID.toString();
  202. }
  203. }