file.dart 8.1 KB

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