file.dart 8.5 KB

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