file.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/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 =
  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. static FileType _fileTypeFromAsset(AssetEntity asset) {
  98. FileType type = FileType.image;
  99. switch (asset.type) {
  100. case AssetType.image:
  101. type = FileType.image;
  102. // PHAssetMediaSubtype.photoLive.rawValue is 8
  103. // This hack should go away once photos_manager support livePhotos
  104. if (asset.subtype > -1 && (asset.subtype & 8) != 0) {
  105. type = FileType.livePhoto;
  106. }
  107. break;
  108. case AssetType.video:
  109. type = FileType.video;
  110. break;
  111. default:
  112. type = FileType.other;
  113. break;
  114. }
  115. return type;
  116. }
  117. Future<AssetEntity?> get getAsset {
  118. if (localID == null) {
  119. return Future.value(null);
  120. }
  121. return AssetEntity.fromId(localID!);
  122. }
  123. void applyMetadata(Map<String, dynamic> metadata) {
  124. localID = metadata["localID"];
  125. title = metadata["title"];
  126. deviceFolder = metadata["deviceFolder"];
  127. creationTime = metadata["creationTime"] ?? 0;
  128. modificationTime = metadata["modificationTime"] ?? creationTime;
  129. final latitude = double.tryParse(metadata["latitude"].toString());
  130. final longitude = double.tryParse(metadata["longitude"].toString());
  131. if (latitude == null || longitude == null) {
  132. location = null;
  133. } else {
  134. location = Location(latitude: latitude, longitude: longitude);
  135. }
  136. fileType = getFileType(metadata["fileType"] ?? -1);
  137. fileSubType = metadata["subType"] ?? -1;
  138. duration = metadata["duration"] ?? 0;
  139. exif = metadata["exif"];
  140. hash = metadata["hash"];
  141. // handle past live photos upload from web client
  142. if (hash == null &&
  143. fileType == FileType.livePhoto &&
  144. metadata.containsKey('imgHash') &&
  145. metadata.containsKey('vidHash')) {
  146. // convert to imgHash:vidHash
  147. hash =
  148. '${metadata['imgHash']}$kLivePhotoHashSeparator${metadata['vidHash']}';
  149. }
  150. metadataVersion = metadata["version"] ?? 0;
  151. }
  152. Future<Map<String, dynamic>> getMetadataForUpload(
  153. MediaUploadData mediaUploadData,
  154. ) async {
  155. final asset = await getAsset;
  156. // asset can be null for files shared to app
  157. if (asset != null) {
  158. fileSubType = asset.subtype;
  159. if (fileType == FileType.video) {
  160. duration = asset.duration;
  161. }
  162. }
  163. bool hasExifTime = false;
  164. if ((fileType == FileType.image || fileType == FileType.video) &&
  165. mediaUploadData.sourceFile != null) {
  166. final exifData = await getExifFromSourceFile(mediaUploadData.sourceFile!);
  167. if (exifData != null) {
  168. if (fileType == FileType.image) {
  169. final exifTime = await getCreationTimeFromEXIF(null, exifData);
  170. if (exifTime != null) {
  171. hasExifTime = true;
  172. creationTime = exifTime.microsecondsSinceEpoch;
  173. }
  174. }
  175. if (Platform.isAndroid) {
  176. //Fix for missing location data in lower android versions.
  177. final Location? exifLocation = locationFromExif(exifData);
  178. if (Location.isValidLocation(exifLocation)) {
  179. location = exifLocation;
  180. }
  181. }
  182. }
  183. }
  184. // Try to get the timestamp from fileName. In case of iOS, file names are
  185. // generic IMG_XXXX, so only parse it on Android devices
  186. if (!hasExifTime && Platform.isAndroid && title != null) {
  187. final timeFromFileName = parseDateTimeFromFileNameV2(title!);
  188. if (timeFromFileName != null) {
  189. // only use timeFromFileName if the existing creationTime and
  190. // timeFromFilename belongs to different date.
  191. // This is done because many times the fileTimeStamp will only give us
  192. // the date, not time value but the photo_manager's creation time will
  193. // contain the time.
  194. final bool useFileTimeStamp = creationTime == null ||
  195. !areFromSameDay(
  196. creationTime!,
  197. timeFromFileName.microsecondsSinceEpoch,
  198. );
  199. if (useFileTimeStamp) {
  200. creationTime = timeFromFileName.microsecondsSinceEpoch;
  201. }
  202. }
  203. }
  204. hash = mediaUploadData.hashData?.fileHash;
  205. return metadata;
  206. }
  207. Map<String, dynamic> get metadata {
  208. final metadata = <String, dynamic>{};
  209. metadata["localID"] = isSharedMediaToAppSandbox ? null : localID;
  210. metadata["title"] = title;
  211. metadata["deviceFolder"] = deviceFolder;
  212. metadata["creationTime"] = creationTime;
  213. metadata["modificationTime"] = modificationTime;
  214. metadata["fileType"] = fileType.index;
  215. if (location != null &&
  216. location!.latitude != null &&
  217. location!.longitude != null) {
  218. metadata["latitude"] = location!.latitude;
  219. metadata["longitude"] = location!.longitude;
  220. }
  221. if (fileSubType != null) {
  222. metadata["subType"] = fileSubType;
  223. }
  224. if (duration != null) {
  225. metadata["duration"] = duration;
  226. }
  227. if (hash != null) {
  228. metadata["hash"] = hash;
  229. }
  230. if (metadataVersion != null) {
  231. metadata["version"] = metadataVersion;
  232. }
  233. return metadata;
  234. }
  235. String get downloadUrl {
  236. final endpoint = Configuration.instance.getHttpEndpoint();
  237. if (endpoint != kDefaultProductionEndpoint ||
  238. FeatureFlagService.instance.disableCFWorker()) {
  239. return endpoint + "/files/download/" + uploadedFileID.toString();
  240. } else {
  241. return "https://files.ente.io/?fileID=" + uploadedFileID.toString();
  242. }
  243. }
  244. String? get caption {
  245. return pubMagicMetadata?.caption;
  246. }
  247. String get thumbnailUrl {
  248. final endpoint = Configuration.instance.getHttpEndpoint();
  249. if (endpoint != kDefaultProductionEndpoint ||
  250. FeatureFlagService.instance.disableCFWorker()) {
  251. return endpoint + "/files/preview/" + uploadedFileID.toString();
  252. } else {
  253. return "https://thumbnails.ente.io/?fileID=" + uploadedFileID.toString();
  254. }
  255. }
  256. String get displayName {
  257. if (pubMagicMetadata != null && pubMagicMetadata!.editedName != null) {
  258. return pubMagicMetadata!.editedName!;
  259. }
  260. if (title == null && kDebugMode) _logger.severe('File title is null');
  261. return title ?? '';
  262. }
  263. // return 0 if the height is not available
  264. int get height {
  265. return pubMagicMetadata?.h ?? 0;
  266. }
  267. int get width {
  268. return pubMagicMetadata?.w ?? 0;
  269. }
  270. // returns true if the file isn't available in the user's gallery
  271. bool get isRemoteFile {
  272. return localID == null && uploadedFileID != null;
  273. }
  274. bool get isUploaded {
  275. return uploadedFileID != null;
  276. }
  277. bool get isSharedMediaToAppSandbox {
  278. return localID != null &&
  279. (localID!.startsWith(oldSharedMediaIdentifier) ||
  280. localID!.startsWith(sharedMediaIdentifier));
  281. }
  282. bool get hasLocation {
  283. return location != null &&
  284. ((location!.longitude ?? 0) != 0 || (location!.latitude ?? 0) != 0);
  285. }
  286. @override
  287. String toString() {
  288. return '''File(generatedID: $generatedID, localID: $localID, title: $title,
  289. uploadedFileId: $uploadedFileID, modificationTime: $modificationTime,
  290. ownerID: $ownerID, collectionID: $collectionID, updationTime: $updationTime)''';
  291. }
  292. @override
  293. bool operator ==(Object o) {
  294. if (identical(this, o)) return true;
  295. return o is File &&
  296. o.generatedID == generatedID &&
  297. o.uploadedFileID == uploadedFileID &&
  298. o.localID == localID;
  299. }
  300. @override
  301. int get hashCode {
  302. return generatedID.hashCode ^ uploadedFileID.hashCode ^ localID.hashCode;
  303. }
  304. String get tag {
  305. return "local_" +
  306. localID.toString() +
  307. ":remote_" +
  308. uploadedFileID.toString() +
  309. ":generated_" +
  310. generatedID.toString();
  311. }
  312. @override
  313. String cacheKey() {
  314. // todo: Neeraj: 19thJuly'22: evaluate and add fileHash as the key?
  315. return localID ?? uploadedFileID?.toString() ?? generatedID.toString();
  316. }
  317. }