file.dart 9.0 KB

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