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! <= jan011971Time)) {
  68. try {
  69. final parsedDateTime = parseDateTimeFromFileNameV2(
  70. basenameWithoutExtension(file.title ?? ""),
  71. );
  72. file.creationTime = parsedDateTime?.microsecondsSinceEpoch ??
  73. DateTime.now().millisecondsSinceEpoch;
  74. } catch (e) {
  75. file.creationTime = DateTime.now().millisecondsSinceEpoch;
  76. }
  77. }
  78. file.modificationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  79. file.fileSubType = asset.subtype;
  80. file.metadataVersion = kCurrentMetadataVersion;
  81. return file;
  82. }
  83. static FileType _fileTypeFromAsset(AssetEntity asset) {
  84. FileType type = FileType.image;
  85. switch (asset.type) {
  86. case AssetType.image:
  87. type = FileType.image;
  88. // PHAssetMediaSubtype.photoLive.rawValue is 8
  89. // This hack should go away once photos_manager support livePhotos
  90. if (asset.subtype > -1 && (asset.subtype & 8) != 0) {
  91. type = FileType.livePhoto;
  92. }
  93. break;
  94. case AssetType.video:
  95. type = FileType.video;
  96. break;
  97. default:
  98. type = FileType.other;
  99. break;
  100. }
  101. return type;
  102. }
  103. Future<AssetEntity?> get getAsset {
  104. if (localID == null) {
  105. return Future.value(null);
  106. }
  107. return AssetEntity.fromId(localID!);
  108. }
  109. void applyMetadata(Map<String, dynamic> metadata) {
  110. localID = metadata["localID"];
  111. title = metadata["title"];
  112. deviceFolder = metadata["deviceFolder"];
  113. creationTime = metadata["creationTime"] ?? 0;
  114. modificationTime = metadata["modificationTime"] ?? creationTime;
  115. final latitude = double.tryParse(metadata["latitude"].toString());
  116. final longitude = double.tryParse(metadata["longitude"].toString());
  117. if (latitude == null || longitude == null) {
  118. location = null;
  119. } else {
  120. location = Location(latitude, longitude);
  121. }
  122. fileType = getFileType(metadata["fileType"]);
  123. fileSubType = metadata["subType"] ?? -1;
  124. duration = metadata["duration"] ?? 0;
  125. exif = metadata["exif"];
  126. hash = metadata["hash"];
  127. // handle past live photos upload from web client
  128. if (hash == null &&
  129. fileType == FileType.livePhoto &&
  130. metadata.containsKey('imgHash') &&
  131. metadata.containsKey('vidHash')) {
  132. // convert to imgHash:vidHash
  133. hash =
  134. '${metadata['imgHash']}$kLivePhotoHashSeparator${metadata['vidHash']}';
  135. }
  136. metadataVersion = metadata["version"] ?? 0;
  137. }
  138. Future<Map<String, dynamic>> getMetadataForUpload(
  139. MediaUploadData mediaUploadData,
  140. ) async {
  141. final asset = await getAsset;
  142. // asset can be null for files shared to app
  143. if (asset != null) {
  144. fileSubType = asset.subtype;
  145. if (fileType == FileType.video) {
  146. duration = asset.duration;
  147. }
  148. }
  149. if (fileType == FileType.image && mediaUploadData.sourceFile != null) {
  150. final exifTime =
  151. await getCreationTimeFromEXIF(mediaUploadData.sourceFile!);
  152. if (exifTime != null) {
  153. creationTime = exifTime.microsecondsSinceEpoch;
  154. }
  155. }
  156. hash = mediaUploadData.hashData?.fileHash;
  157. return metadata;
  158. }
  159. Map<String, dynamic> get metadata {
  160. final metadata = <String, dynamic>{};
  161. metadata["localID"] = isSharedMediaToAppSandbox ? null : localID;
  162. metadata["title"] = title;
  163. metadata["deviceFolder"] = deviceFolder;
  164. metadata["creationTime"] = creationTime;
  165. metadata["modificationTime"] = modificationTime;
  166. metadata["fileType"] = fileType.index;
  167. if (location != null &&
  168. location!.latitude != null &&
  169. location!.longitude != null) {
  170. metadata["latitude"] = location!.latitude;
  171. metadata["longitude"] = location!.longitude;
  172. }
  173. if (fileSubType != null) {
  174. metadata["subType"] = fileSubType;
  175. }
  176. if (duration != null) {
  177. metadata["duration"] = duration;
  178. }
  179. if (hash != null) {
  180. metadata["hash"] = hash;
  181. }
  182. if (metadataVersion != null) {
  183. metadata["version"] = metadataVersion;
  184. }
  185. return metadata;
  186. }
  187. String get downloadUrl {
  188. final endpoint = Configuration.instance.getHttpEndpoint();
  189. if (endpoint != kDefaultProductionEndpoint ||
  190. FeatureFlagService.instance.disableCFWorker()) {
  191. return endpoint + "/files/download/" + uploadedFileID.toString();
  192. } else {
  193. return "https://files.ente.io/?fileID=" + uploadedFileID.toString();
  194. }
  195. }
  196. String? get caption {
  197. return pubMagicMetadata?.caption;
  198. }
  199. String get thumbnailUrl {
  200. final endpoint = Configuration.instance.getHttpEndpoint();
  201. if (endpoint != kDefaultProductionEndpoint ||
  202. FeatureFlagService.instance.disableCFWorker()) {
  203. return endpoint + "/files/preview/" + uploadedFileID.toString();
  204. } else {
  205. return "https://thumbnails.ente.io/?fileID=" + uploadedFileID.toString();
  206. }
  207. }
  208. String get displayName {
  209. if (pubMagicMetadata != null && pubMagicMetadata!.editedName != null) {
  210. return pubMagicMetadata!.editedName!;
  211. }
  212. if (title == null) _logger.severe('File title is null');
  213. return title ?? '';
  214. }
  215. // returns true if the file isn't available in the user's gallery
  216. bool get isRemoteFile {
  217. return localID == null && uploadedFileID != null;
  218. }
  219. bool get isSharedMediaToAppSandbox {
  220. return localID != null &&
  221. (localID!.startsWith(oldSharedMediaIdentifier) ||
  222. localID!.startsWith(sharedMediaIdentifier));
  223. }
  224. bool get hasLocation {
  225. return location != null &&
  226. (location!.longitude != 0 || location!.latitude != 0);
  227. }
  228. @override
  229. String toString() {
  230. return '''File(generatedID: $generatedID, localID: $localID, title: $title,
  231. uploadedFileId: $uploadedFileID, modificationTime: $modificationTime,
  232. ownerID: $ownerID, collectionID: $collectionID, updationTime: $updationTime)''';
  233. }
  234. @override
  235. bool operator ==(Object o) {
  236. if (identical(this, o)) return true;
  237. return o is File &&
  238. o.generatedID == generatedID &&
  239. o.uploadedFileID == uploadedFileID &&
  240. o.localID == localID;
  241. }
  242. @override
  243. int get hashCode {
  244. return generatedID.hashCode ^ uploadedFileID.hashCode ^ localID.hashCode;
  245. }
  246. String get tag {
  247. return "local_" +
  248. localID.toString() +
  249. ":remote_" +
  250. uploadedFileID.toString() +
  251. ":generated_" +
  252. generatedID.toString();
  253. }
  254. @override
  255. String cacheKey() {
  256. // todo: Neeraj: 19thJuly'22: evaluate and add fileHash as the key?
  257. return localID ?? uploadedFileID?.toString() ?? generatedID.toString();
  258. }
  259. }