file.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import 'dart:io';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:logging/logging.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/file/file_type.dart';
  9. import 'package:photos/models/location/location.dart';
  10. import "package:photos/models/metadata/file_magic.dart";
  11. import 'package:photos/services/feature_flag_service.dart';
  12. import 'package:photos/utils/date_time_util.dart';
  13. import 'package:photos/utils/exif_util.dart';
  14. import 'package:photos/utils/file_uploader_util.dart';
  15. class EnteFile {
  16. int? generatedID;
  17. int? uploadedFileID;
  18. int? ownerID;
  19. int? collectionID;
  20. String? localID;
  21. String? title;
  22. String? deviceFolder;
  23. int? creationTime;
  24. int? modificationTime;
  25. int? updationTime;
  26. int? addedTime;
  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. EnteFile();
  58. static Future<EnteFile> fromAsset(String pathName, AssetEntity asset) async {
  59. final EnteFile file = EnteFile();
  60. file.localID = asset.id;
  61. file.title = asset.title;
  62. file.deviceFolder = pathName;
  63. file.location =
  64. Location(latitude: asset.latitude, longitude: asset.longitude);
  65. file.fileType = fileTypeFromAsset(asset);
  66. file.creationTime = parseFileCreationTime(file.title, asset);
  67. file.modificationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  68. file.fileSubType = asset.subtype;
  69. file.metadataVersion = kCurrentMetadataVersion;
  70. return file;
  71. }
  72. static int parseFileCreationTime(String? fileTitle, AssetEntity asset) {
  73. int creationTime = asset.createDateTime.microsecondsSinceEpoch;
  74. if (creationTime >= jan011981Time) {
  75. // assuming that fileSystem is returning correct creationTime.
  76. // During upload, this might get overridden with exif Creation time
  77. return creationTime;
  78. } else {
  79. if (asset.modifiedDateTime.microsecondsSinceEpoch >= jan011981Time) {
  80. creationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  81. } else {
  82. creationTime = DateTime.now().toUtc().microsecondsSinceEpoch;
  83. }
  84. try {
  85. final parsedDateTime = parseDateTimeFromFileNameV2(
  86. basenameWithoutExtension(fileTitle ?? ""),
  87. );
  88. if (parsedDateTime != null) {
  89. creationTime = parsedDateTime.microsecondsSinceEpoch;
  90. }
  91. } catch (e) {
  92. // ignore
  93. }
  94. }
  95. return creationTime;
  96. }
  97. Future<AssetEntity?> get getAsset {
  98. if (localID == null) {
  99. return Future.value(null);
  100. }
  101. return AssetEntity.fromId(localID!);
  102. }
  103. void applyMetadata(Map<String, dynamic> metadata) {
  104. localID = metadata["localID"];
  105. title = metadata["title"];
  106. deviceFolder = metadata["deviceFolder"];
  107. creationTime = metadata["creationTime"] ?? 0;
  108. modificationTime = metadata["modificationTime"] ?? creationTime;
  109. final latitude = double.tryParse(metadata["latitude"].toString());
  110. final longitude = double.tryParse(metadata["longitude"].toString());
  111. if (latitude == null || longitude == null) {
  112. location = null;
  113. } else {
  114. location = Location(latitude: latitude, longitude: longitude);
  115. }
  116. fileType = getFileType(metadata["fileType"] ?? -1);
  117. fileSubType = metadata["subType"] ?? -1;
  118. duration = metadata["duration"] ?? 0;
  119. exif = metadata["exif"];
  120. hash = metadata["hash"];
  121. // handle past live photos upload from web client
  122. if (hash == null &&
  123. fileType == FileType.livePhoto &&
  124. metadata.containsKey('imageHash') &&
  125. metadata.containsKey('videoHash')) {
  126. // convert to imgHash:vidHash
  127. hash =
  128. '${metadata['imageHash']}$kLivePhotoHashSeparator${metadata['videoHash']}';
  129. }
  130. metadataVersion = metadata["version"] ?? 0;
  131. }
  132. Future<Map<String, dynamic>> getMetadataForUpload(
  133. MediaUploadData mediaUploadData,
  134. ) async {
  135. final asset = await getAsset;
  136. // asset can be null for files shared to app
  137. if (asset != null) {
  138. fileSubType = asset.subtype;
  139. if (fileType == FileType.video) {
  140. duration = asset.duration;
  141. }
  142. }
  143. bool hasExifTime = false;
  144. if ((fileType == FileType.image || fileType == FileType.video) &&
  145. mediaUploadData.sourceFile != null) {
  146. final exifData = await getExifFromSourceFile(mediaUploadData.sourceFile!);
  147. if (exifData != null) {
  148. if (fileType == FileType.image) {
  149. final exifTime = await getCreationTimeFromEXIF(null, exifData);
  150. if (exifTime != null) {
  151. hasExifTime = true;
  152. creationTime = exifTime.microsecondsSinceEpoch;
  153. }
  154. }
  155. if (Platform.isAndroid) {
  156. //Fix for missing location data in lower android versions.
  157. final Location? exifLocation = locationFromExif(exifData);
  158. if (Location.isValidLocation(exifLocation)) {
  159. location = exifLocation;
  160. }
  161. }
  162. }
  163. }
  164. // Try to get the timestamp from fileName. In case of iOS, file names are
  165. // generic IMG_XXXX, so only parse it on Android devices
  166. if (!hasExifTime && Platform.isAndroid && title != null) {
  167. final timeFromFileName = parseDateTimeFromFileNameV2(title!);
  168. if (timeFromFileName != null) {
  169. // only use timeFromFileName if the existing creationTime and
  170. // timeFromFilename belongs to different date.
  171. // This is done because many times the fileTimeStamp will only give us
  172. // the date, not time value but the photo_manager's creation time will
  173. // contain the time.
  174. final bool useFileTimeStamp = creationTime == null ||
  175. !areFromSameDay(
  176. creationTime!,
  177. timeFromFileName.microsecondsSinceEpoch,
  178. );
  179. if (useFileTimeStamp) {
  180. creationTime = timeFromFileName.microsecondsSinceEpoch;
  181. }
  182. }
  183. }
  184. hash = mediaUploadData.hashData?.fileHash;
  185. return metadata;
  186. }
  187. Map<String, dynamic> get metadata {
  188. final metadata = <String, dynamic>{};
  189. metadata["localID"] = isSharedMediaToAppSandbox ? null : localID;
  190. metadata["title"] = title;
  191. metadata["deviceFolder"] = deviceFolder;
  192. metadata["creationTime"] = creationTime;
  193. metadata["modificationTime"] = modificationTime;
  194. metadata["fileType"] = fileType.index;
  195. if (location != null &&
  196. location!.latitude != null &&
  197. location!.longitude != null) {
  198. metadata["latitude"] = location!.latitude;
  199. metadata["longitude"] = location!.longitude;
  200. }
  201. if (fileSubType != null) {
  202. metadata["subType"] = fileSubType;
  203. }
  204. if (duration != null) {
  205. metadata["duration"] = duration;
  206. }
  207. if (hash != null) {
  208. metadata["hash"] = hash;
  209. }
  210. if (metadataVersion != null) {
  211. metadata["version"] = metadataVersion;
  212. }
  213. return metadata;
  214. }
  215. String get downloadUrl {
  216. final endpoint = Configuration.instance.getHttpEndpoint();
  217. if (endpoint != kDefaultProductionEndpoint ||
  218. FeatureFlagService.instance.disableCFWorker()) {
  219. return endpoint + "/files/download/" + uploadedFileID.toString();
  220. } else {
  221. return "https://files.ente.io/?fileID=" + uploadedFileID.toString();
  222. }
  223. }
  224. String? get caption {
  225. return pubMagicMetadata?.caption;
  226. }
  227. String get thumbnailUrl {
  228. final endpoint = Configuration.instance.getHttpEndpoint();
  229. if (endpoint != kDefaultProductionEndpoint ||
  230. FeatureFlagService.instance.disableCFWorker()) {
  231. return endpoint + "/files/preview/" + uploadedFileID.toString();
  232. } else {
  233. return "https://thumbnails.ente.io/?fileID=" + uploadedFileID.toString();
  234. }
  235. }
  236. String get displayName {
  237. if (pubMagicMetadata != null && pubMagicMetadata!.editedName != null) {
  238. return pubMagicMetadata!.editedName!;
  239. }
  240. if (title == null && kDebugMode) _logger.severe('File title is null');
  241. return title ?? '';
  242. }
  243. // return 0 if the height is not available
  244. int get height {
  245. return pubMagicMetadata?.h ?? 0;
  246. }
  247. int get width {
  248. return pubMagicMetadata?.w ?? 0;
  249. }
  250. bool get hasDimensions {
  251. return height != 0 && width != 0;
  252. }
  253. // returns true if the file isn't available in the user's gallery
  254. bool get isRemoteFile {
  255. return localID == null && uploadedFileID != null;
  256. }
  257. bool get isUploaded {
  258. return uploadedFileID != null;
  259. }
  260. bool get isSharedMediaToAppSandbox {
  261. return localID != null &&
  262. (localID!.startsWith(oldSharedMediaIdentifier) ||
  263. localID!.startsWith(sharedMediaIdentifier));
  264. }
  265. bool get hasLocation {
  266. return location != null &&
  267. ((location!.longitude ?? 0) != 0 || (location!.latitude ?? 0) != 0);
  268. }
  269. @override
  270. String toString() {
  271. return '''File(generatedID: $generatedID, localID: $localID, title: $title,
  272. uploadedFileId: $uploadedFileID, modificationTime: $modificationTime,
  273. ownerID: $ownerID, collectionID: $collectionID, updationTime: $updationTime)''';
  274. }
  275. @override
  276. bool operator ==(Object o) {
  277. if (identical(this, o)) return true;
  278. return o is EnteFile &&
  279. o.generatedID == generatedID &&
  280. o.uploadedFileID == uploadedFileID &&
  281. o.localID == localID;
  282. }
  283. @override
  284. int get hashCode {
  285. return generatedID.hashCode ^ uploadedFileID.hashCode ^ localID.hashCode;
  286. }
  287. String get tag {
  288. return "local_" +
  289. localID.toString() +
  290. ":remote_" +
  291. uploadedFileID.toString() +
  292. ":generated_" +
  293. generatedID.toString();
  294. }
  295. String cacheKey() {
  296. // todo: Neeraj: 19thJuly'22: evaluate and add fileHash as the key?
  297. return localID ?? uploadedFileID?.toString() ?? generatedID.toString();
  298. }
  299. }