file.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import 'dart:io' as io;
  2. import 'package:flutter_sodium/flutter_sodium.dart';
  3. import 'package:path/path.dart';
  4. import 'package:photo_manager/photo_manager.dart';
  5. import 'package:photos/core/configuration.dart';
  6. import 'package:photos/core/constants.dart';
  7. import 'package:photos/models/ente_file.dart';
  8. import 'package:photos/models/file_type.dart';
  9. import 'package:photos/models/location.dart';
  10. import 'package:photos/models/magic_metadata.dart';
  11. import 'package:photos/services/feature_flag_service.dart';
  12. import 'package:photos/utils/crypto_util.dart';
  13. import 'package:photos/utils/exif_util.dart';
  14. class File extends EnteFile {
  15. int generatedID;
  16. int uploadedFileID;
  17. int ownerID;
  18. int collectionID;
  19. String localID;
  20. String title;
  21. String deviceFolder;
  22. int creationTime;
  23. int modificationTime;
  24. int updationTime;
  25. Location location;
  26. FileType fileType;
  27. int fileSubType;
  28. int duration;
  29. String exif;
  30. String hash;
  31. int metadataVersion;
  32. String encryptedKey;
  33. String keyDecryptionNonce;
  34. String fileDecryptionHeader;
  35. String thumbnailDecryptionHeader;
  36. String metadataDecryptionHeader;
  37. String mMdEncodedJson;
  38. int mMdVersion = 0;
  39. MagicMetadata _mmd;
  40. MagicMetadata get magicMetadata =>
  41. _mmd ?? MagicMetadata.fromEncodedJson(mMdEncodedJson ?? '{}');
  42. set magicMetadata(val) => _mmd = val;
  43. // public magic metadata is shared if during file/album sharing
  44. String pubMmdEncodedJson;
  45. int pubMmdVersion = 0;
  46. PubMagicMetadata _pubMmd;
  47. PubMagicMetadata get pubMagicMetadata =>
  48. _pubMmd ?? PubMagicMetadata.fromEncodedJson(pubMmdEncodedJson ?? '{}');
  49. set pubMagicMetadata(val) => _pubMmd = val;
  50. static const kCurrentMetadataVersion = 1;
  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. metadataVersion = metadata["version"] ?? 0;
  126. }
  127. Future<Map<String, dynamic>> getMetadataForUpload(io.File sourceFile) async {
  128. final asset = await getAsset();
  129. // asset can be null for files shared to app
  130. if (asset != null) {
  131. fileSubType = asset.subtype;
  132. if (fileType == FileType.video) {
  133. duration = asset.duration;
  134. }
  135. }
  136. if (fileType == FileType.image) {
  137. final exifTime = await getCreationTimeFromEXIF(sourceFile);
  138. if (exifTime != null) {
  139. creationTime = exifTime.microsecondsSinceEpoch;
  140. }
  141. }
  142. hash = Sodium.bin2base64(await CryptoUtil.getHash(sourceFile));
  143. return getMetadata();
  144. }
  145. Map<String, dynamic> getMetadata() {
  146. final metadata = <String, dynamic>{};
  147. metadata["localID"] = isSharedMediaToAppSandbox() ? null : localID;
  148. metadata["title"] = title;
  149. metadata["deviceFolder"] = deviceFolder;
  150. metadata["creationTime"] = creationTime;
  151. metadata["modificationTime"] = modificationTime;
  152. metadata["fileType"] = fileType.index;
  153. if (location != null &&
  154. location.latitude != null &&
  155. location.longitude != null) {
  156. metadata["latitude"] = location.latitude;
  157. metadata["longitude"] = location.longitude;
  158. }
  159. if (fileSubType != null) {
  160. metadata["subType"] = fileSubType;
  161. }
  162. if (duration != null) {
  163. metadata["duration"] = duration;
  164. }
  165. if (hash != null) {
  166. metadata["hash"] = hash;
  167. }
  168. if (metadataVersion != null) {
  169. metadata["version"] = metadataVersion;
  170. }
  171. return metadata;
  172. }
  173. String getDownloadUrl() {
  174. final endpoint = Configuration.instance.getHttpEndpoint();
  175. if (endpoint != kDefaultProductionEndpoint ||
  176. FeatureFlagService.instance.disableCFWorker()) {
  177. return endpoint + "/files/download/" + uploadedFileID.toString();
  178. } else {
  179. return "https://files.ente.io/?fileID=" + uploadedFileID.toString();
  180. }
  181. }
  182. String getThumbnailUrl() {
  183. final endpoint = Configuration.instance.getHttpEndpoint();
  184. if (endpoint != kDefaultProductionEndpoint ||
  185. FeatureFlagService.instance.disableCFWorker()) {
  186. return endpoint + "/files/preview/" + uploadedFileID.toString();
  187. } else {
  188. return "https://thumbnails.ente.io/?fileID=" + uploadedFileID.toString();
  189. }
  190. }
  191. String getDisplayName() {
  192. if (pubMagicMetadata != null && pubMagicMetadata.editedName != null) {
  193. return pubMagicMetadata.editedName;
  194. }
  195. return title;
  196. }
  197. // returns true if the file isn't available in the user's gallery
  198. bool isRemoteFile() {
  199. return localID == null && uploadedFileID != null;
  200. }
  201. bool isSharedMediaToAppSandbox() {
  202. return localID != null &&
  203. (localID.startsWith(kOldSharedMediaIdentifier) ||
  204. localID.startsWith(kSharedMediaIdentifier));
  205. }
  206. bool hasLocation() {
  207. return location != null &&
  208. (location.longitude != 0 || location.latitude != 0);
  209. }
  210. @override
  211. String toString() {
  212. return '''File(generatedID: $generatedID, localID: $localID, title: $title,
  213. uploadedFileId: $uploadedFileID, modificationTime: $modificationTime,
  214. ownerID: $ownerID, collectionID: $collectionID, updationTime: $updationTime)''';
  215. }
  216. @override
  217. bool operator ==(Object o) {
  218. if (identical(this, o)) return true;
  219. return o is File &&
  220. o.generatedID == generatedID &&
  221. o.uploadedFileID == uploadedFileID &&
  222. o.localID == localID;
  223. }
  224. @override
  225. int get hashCode {
  226. return generatedID.hashCode ^ uploadedFileID.hashCode ^ localID.hashCode;
  227. }
  228. String tag() {
  229. return "local_" +
  230. localID.toString() +
  231. ":remote_" +
  232. uploadedFileID.toString() +
  233. ":generated_" +
  234. generatedID.toString();
  235. }
  236. @override
  237. String cacheKey() {
  238. // todo: Neeraj: 19thJuly'22: evaluate and add fileHash as the key?
  239. return localID ?? uploadedFileID?.toString() ?? generatedID?.toString();
  240. }
  241. @override
  242. String localIdentifier() {
  243. return localID;
  244. }
  245. }