file.dart 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import 'package:logging/logging.dart';
  2. import 'package:path/path.dart';
  3. import 'package:photo_manager/photo_manager.dart';
  4. import 'package:photos/core/configuration.dart';
  5. import 'package:photos/core/constants.dart';
  6. import 'package:photos/models/ente_file.dart';
  7. import 'package:photos/models/file_type.dart';
  8. import 'package:photos/models/location.dart';
  9. import 'package:photos/models/magic_metadata.dart';
  10. // ignore: import_of_legacy_library_into_null_safe
  11. import 'package:photos/services/feature_flag_service.dart';
  12. import 'package:photos/utils/date_time_util.dart';
  13. // ignore: import_of_legacy_library_into_null_safe
  14. import 'package:photos/utils/exif_util.dart';
  15. // ignore: import_of_legacy_library_into_null_safe
  16. import 'package:photos/utils/file_uploader_util.dart';
  17. class File extends EnteFile {
  18. int? generatedID;
  19. int? uploadedFileID;
  20. int? ownerID;
  21. int? collectionID;
  22. String? localID;
  23. String? title;
  24. String? deviceFolder;
  25. int? creationTime;
  26. int? modificationTime;
  27. int? updationTime;
  28. Location? location;
  29. late FileType fileType;
  30. int? fileSubType;
  31. int? duration;
  32. String? exif;
  33. String? hash;
  34. int? metadataVersion;
  35. String? encryptedKey;
  36. String? keyDecryptionNonce;
  37. String? fileDecryptionHeader;
  38. String? thumbnailDecryptionHeader;
  39. String? metadataDecryptionHeader;
  40. int? fileSize;
  41. String? mMdEncodedJson;
  42. int mMdVersion = 0;
  43. MagicMetadata? _mmd;
  44. MagicMetadata get magicMetadata =>
  45. _mmd ?? MagicMetadata.fromEncodedJson(mMdEncodedJson ?? '{}');
  46. set magicMetadata(val) => _mmd = val;
  47. // public magic metadata is shared if during file/album sharing
  48. String? pubMmdEncodedJson;
  49. int pubMmdVersion = 0;
  50. PubMagicMetadata? _pubMmd;
  51. PubMagicMetadata? get pubMagicMetadata =>
  52. _pubMmd ?? PubMagicMetadata.fromEncodedJson(pubMmdEncodedJson ?? '{}');
  53. set pubMagicMetadata(val) => _pubMmd = val;
  54. // in Version 1, live photo hash is stored as zip's hash.
  55. // in V2: LivePhoto hash is stored as imgHash:vidHash
  56. static const kCurrentMetadataVersion = 2;
  57. static final _logger = Logger('File');
  58. File();
  59. static Future<File> fromAsset(String pathName, AssetEntity asset) async {
  60. final File file = File();
  61. file.localID = asset.id;
  62. file.title = asset.title;
  63. file.deviceFolder = pathName;
  64. file.location = Location(asset.latitude, asset.longitude);
  65. file.fileType = _fileTypeFromAsset(asset);
  66. file.creationTime = asset.createDateTime.microsecondsSinceEpoch;
  67. if (file.creationTime == null || (file.creationTime! <= jan011991Time)) {
  68. try {
  69. final parsedDateTime = parseDateTimeFromFileNameV2(
  70. basenameWithoutExtension(file.title ?? ""));
  71. file.creationTime = parsedDateTime?.microsecondsSinceEpoch ??
  72. asset.modifiedDateTime.microsecondsSinceEpoch;
  73. } catch (e) {
  74. file.creationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  75. }
  76. }
  77. file.modificationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  78. file.fileSubType = asset.subtype;
  79. file.metadataVersion = kCurrentMetadataVersion;
  80. return file;
  81. }
  82. static FileType _fileTypeFromAsset(AssetEntity asset) {
  83. FileType type = FileType.image;
  84. switch (asset.type) {
  85. case AssetType.image:
  86. type = FileType.image;
  87. // PHAssetMediaSubtype.photoLive.rawValue is 8
  88. // This hack should go away once photos_manager support livePhotos
  89. if (asset.subtype > -1 && (asset.subtype & 8) != 0) {
  90. type = FileType.livePhoto;
  91. }
  92. break;
  93. case AssetType.video:
  94. type = FileType.video;
  95. break;
  96. default:
  97. type = FileType.other;
  98. break;
  99. }
  100. return type;
  101. }
  102. Future<AssetEntity?> get getAsset {
  103. if (localID == null) {
  104. return Future.value(null);
  105. }
  106. return AssetEntity.fromId(localID!);
  107. }
  108. void applyMetadata(Map<String, dynamic> metadata) {
  109. localID = metadata["localID"];
  110. title = metadata["title"];
  111. deviceFolder = metadata["deviceFolder"];
  112. creationTime = metadata["creationTime"] ?? 0;
  113. modificationTime = metadata["modificationTime"] ?? creationTime;
  114. final latitude = double.tryParse(metadata["latitude"].toString());
  115. final longitude = double.tryParse(metadata["longitude"].toString());
  116. if (latitude == null || longitude == null) {
  117. location = null;
  118. } else {
  119. location = Location(latitude, longitude);
  120. }
  121. fileType = getFileType(metadata["fileType"]);
  122. fileSubType = metadata["subType"] ?? -1;
  123. duration = metadata["duration"] ?? 0;
  124. exif = metadata["exif"];
  125. hash = metadata["hash"];
  126. // handle past live photos upload from web client
  127. if (hash == null &&
  128. fileType == FileType.livePhoto &&
  129. metadata.containsKey('imgHash') &&
  130. metadata.containsKey('vidHash')) {
  131. // convert to imgHash:vidHash
  132. hash =
  133. '${metadata['imgHash']}$kLivePhotoHashSeparator${metadata['vidHash']}';
  134. }
  135. metadataVersion = metadata["version"] ?? 0;
  136. }
  137. Future<Map<String, dynamic>> getMetadataForUpload(
  138. MediaUploadData mediaUploadData,
  139. ) async {
  140. final asset = await getAsset;
  141. // asset can be null for files shared to app
  142. if (asset != null) {
  143. fileSubType = asset.subtype;
  144. if (fileType == FileType.video) {
  145. duration = asset.duration;
  146. }
  147. }
  148. if (fileType == FileType.image && mediaUploadData.sourceFile != null) {
  149. final exifTime =
  150. await getCreationTimeFromEXIF(mediaUploadData.sourceFile!);
  151. if (exifTime != null) {
  152. creationTime = exifTime.microsecondsSinceEpoch;
  153. }
  154. }
  155. hash = mediaUploadData.hashData?.fileHash;
  156. return metadata;
  157. }
  158. Map<String, dynamic> get metadata {
  159. final metadata = <String, dynamic>{};
  160. metadata["localID"] = isSharedMediaToAppSandbox ? null : localID;
  161. metadata["title"] = title;
  162. metadata["deviceFolder"] = deviceFolder;
  163. metadata["creationTime"] = creationTime;
  164. metadata["modificationTime"] = modificationTime;
  165. metadata["fileType"] = fileType.index;
  166. if (location != null &&
  167. location!.latitude != null &&
  168. location!.longitude != null) {
  169. metadata["latitude"] = location!.latitude;
  170. metadata["longitude"] = location!.longitude;
  171. }
  172. if (fileSubType != null) {
  173. metadata["subType"] = fileSubType;
  174. }
  175. if (duration != null) {
  176. metadata["duration"] = duration;
  177. }
  178. if (hash != null) {
  179. metadata["hash"] = hash;
  180. }
  181. if (metadataVersion != null) {
  182. metadata["version"] = metadataVersion;
  183. }
  184. return metadata;
  185. }
  186. String get downloadUrl {
  187. final endpoint = Configuration.instance.getHttpEndpoint();
  188. if (endpoint != kDefaultProductionEndpoint ||
  189. FeatureFlagService.instance.disableCFWorker()) {
  190. return endpoint + "/files/download/" + uploadedFileID.toString();
  191. } else {
  192. return "https://files.ente.io/?fileID=" + uploadedFileID.toString();
  193. }
  194. }
  195. String? get caption {
  196. return pubMagicMetadata?.caption;
  197. }
  198. String get thumbnailUrl {
  199. final endpoint = Configuration.instance.getHttpEndpoint();
  200. if (endpoint != kDefaultProductionEndpoint ||
  201. FeatureFlagService.instance.disableCFWorker()) {
  202. return endpoint + "/files/preview/" + uploadedFileID.toString();
  203. } else {
  204. return "https://thumbnails.ente.io/?fileID=" + uploadedFileID.toString();
  205. }
  206. }
  207. String get displayName {
  208. if (pubMagicMetadata != null && pubMagicMetadata!.editedName != null) {
  209. return pubMagicMetadata!.editedName!;
  210. }
  211. if (title == null) _logger.severe('File title is null');
  212. return title ?? '';
  213. }
  214. // returns true if the file isn't available in the user's gallery
  215. bool get isRemoteFile {
  216. return localID == null && uploadedFileID != null;
  217. }
  218. bool get isSharedMediaToAppSandbox {
  219. return localID != null &&
  220. (localID!.startsWith(oldSharedMediaIdentifier) ||
  221. localID!.startsWith(sharedMediaIdentifier));
  222. }
  223. bool get hasLocation {
  224. return location != null &&
  225. (location!.longitude != 0 || location!.latitude != 0);
  226. }
  227. @override
  228. String toString() {
  229. return '''File(generatedID: $generatedID, localID: $localID, title: $title,
  230. uploadedFileId: $uploadedFileID, modificationTime: $modificationTime,
  231. ownerID: $ownerID, collectionID: $collectionID, updationTime: $updationTime)''';
  232. }
  233. @override
  234. bool operator ==(Object o) {
  235. if (identical(this, o)) return true;
  236. return o is File &&
  237. o.generatedID == generatedID &&
  238. o.uploadedFileID == uploadedFileID &&
  239. o.localID == localID;
  240. }
  241. @override
  242. int get hashCode {
  243. return generatedID.hashCode ^ uploadedFileID.hashCode ^ localID.hashCode;
  244. }
  245. String get tag {
  246. return "local_" +
  247. localID.toString() +
  248. ":remote_" +
  249. uploadedFileID.toString() +
  250. ":generated_" +
  251. generatedID.toString();
  252. }
  253. @override
  254. String cacheKey() {
  255. // todo: Neeraj: 19thJuly'22: evaluate and add fileHash as the key?
  256. return localID ?? uploadedFileID?.toString() ?? generatedID.toString();
  257. }
  258. }