file.dart 10 KB

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