file.dart 8.5 KB

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