file.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/magic_metadata.dart';
  9. import 'package:photos/models/file_type.dart';
  10. import 'package:photos/models/location.dart';
  11. import 'package:photos/services/feature_flag_service.dart';
  12. import 'package:photos/utils/crypto_util.dart';
  13. class File {
  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. static const kCurrentMetadataVersion = 1;
  43. File();
  44. static Future<File> fromAsset(String pathName, AssetEntity asset) async {
  45. File file = File();
  46. file.localID = asset.id;
  47. file.title = asset.title;
  48. file.deviceFolder = pathName;
  49. file.location = Location(asset.latitude, asset.longitude);
  50. file.fileType = _fileTypeFromAsset(asset);
  51. file.creationTime = asset.createDateTime.microsecondsSinceEpoch;
  52. if (file.creationTime == 0) {
  53. try {
  54. final parsedDateTime = DateTime.parse(
  55. basenameWithoutExtension(file.title)
  56. .replaceAll("IMG_", "")
  57. .replaceAll("VID_", "")
  58. .replaceAll("DCIM_", "")
  59. .replaceAll("_", " "));
  60. file.creationTime = parsedDateTime.microsecondsSinceEpoch;
  61. } catch (e) {
  62. file.creationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  63. }
  64. }
  65. file.modificationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  66. file.fileSubType = asset.subTypes;
  67. file.metadataVersion = kCurrentMetadataVersion;
  68. return file;
  69. }
  70. static FileType _fileTypeFromAsset(AssetEntity asset) {
  71. FileType type = FileType.image;
  72. switch (asset.type) {
  73. case AssetType.image:
  74. type = FileType.image;
  75. // PHAssetMediaSubtype.photoLive.rawValue is 8
  76. // This hack should go away once photos_manager support livePhotos
  77. if (asset.subTypes != null &&
  78. asset.subTypes > -1 &&
  79. (asset.subTypes & 8) != 0) {
  80. type = FileType.livePhoto;
  81. }
  82. break;
  83. case AssetType.video:
  84. type = FileType.video;
  85. break;
  86. default:
  87. type = FileType.other;
  88. break;
  89. }
  90. return type;
  91. }
  92. Future<AssetEntity> getAsset() {
  93. if (localID == null) {
  94. return Future.value(null);
  95. }
  96. return AssetEntity.fromId(localID);
  97. }
  98. void applyMetadata(Map<String, dynamic> metadata) {
  99. localID = metadata["localID"];
  100. title = metadata["title"];
  101. deviceFolder = metadata["deviceFolder"];
  102. creationTime = metadata["creationTime"] ?? 0;
  103. modificationTime = metadata["modificationTime"] ?? creationTime;
  104. final latitude = double.tryParse(metadata["latitude"].toString());
  105. final longitude = double.tryParse(metadata["longitude"].toString());
  106. if (latitude == null || longitude == null) {
  107. location = null;
  108. } else {
  109. location = Location(latitude, longitude);
  110. }
  111. fileType = getFileType(metadata["fileType"]);
  112. fileSubType = metadata["subType"] ?? -1;
  113. duration = metadata["duration"] ?? 0;
  114. exif = metadata["exif"];
  115. hash = metadata["hash"];
  116. metadataVersion = metadata["version"] ?? 0;
  117. }
  118. Future<Map<String, dynamic>> getMetadata(io.File sourceFile) async {
  119. final metadata = <String, dynamic>{};
  120. metadata["localID"] = isSharedMediaToAppSandbox() ? null : localID;
  121. metadata["title"] = title;
  122. metadata["deviceFolder"] = deviceFolder;
  123. metadata["creationTime"] = creationTime;
  124. metadata["modificationTime"] = modificationTime;
  125. if (location != null &&
  126. location.latitude != null &&
  127. location.longitude != null) {
  128. metadata["latitude"] = location.latitude;
  129. metadata["longitude"] = location.longitude;
  130. }
  131. metadata["fileType"] = fileType.index;
  132. final asset = await getAsset();
  133. // asset can be null for files shared to app
  134. if (asset != null) {
  135. fileSubType = asset.subTypes;
  136. metadata["subType"] = fileSubType;
  137. if (fileType == FileType.video) {
  138. duration = asset.duration;
  139. metadata["duration"] = duration;
  140. }
  141. }
  142. hash = Sodium.bin2base64(await CryptoUtil.getHash(sourceFile));
  143. metadata["hash"] = hash;
  144. metadata["version"] = metadataVersion;
  145. return metadata;
  146. }
  147. String getDownloadUrl() {
  148. if (kDebugMode || FeatureFlagService.instance.disableCFWorker()) {
  149. return Configuration.instance.getHttpEndpoint() +
  150. "/files/download/" +
  151. uploadedFileID.toString();
  152. } else {
  153. return "https://files.ente.workers.dev/?fileID=" +
  154. uploadedFileID.toString();
  155. }
  156. }
  157. String getThumbnailUrl() {
  158. if (kDebugMode || FeatureFlagService.instance.disableCFWorker()) {
  159. return Configuration.instance.getHttpEndpoint() +
  160. "/files/preview/" +
  161. uploadedFileID.toString();
  162. } else {
  163. return "https://thumbnails.ente.workers.dev/?fileID=" +
  164. uploadedFileID.toString();
  165. }
  166. }
  167. // returns true if the file isn't available in the user's gallery
  168. bool isRemoteFile() {
  169. return localID == null && uploadedFileID != null;
  170. }
  171. bool isSharedMediaToAppSandbox() {
  172. return localID != null && localID.startsWith(kSharedMediaIdentifier);
  173. }
  174. bool hasLocation() {
  175. return location != null &&
  176. (location.longitude != 0 || location.latitude != 0);
  177. }
  178. @override
  179. String toString() {
  180. return '''File(generatedID: $generatedID, localID: $localID,
  181. uploadedFileId: $uploadedFileID, modificationTime: $modificationTime,
  182. ownerID: $ownerID, collectionID: $collectionID, updationTime: $updationTime)''';
  183. }
  184. @override
  185. bool operator ==(Object o) {
  186. if (identical(this, o)) return true;
  187. return o is File &&
  188. o.generatedID == generatedID &&
  189. o.uploadedFileID == uploadedFileID &&
  190. o.localID == localID;
  191. }
  192. @override
  193. int get hashCode {
  194. return generatedID.hashCode ^ uploadedFileID.hashCode ^ localID.hashCode;
  195. }
  196. String tag() {
  197. return "local_" +
  198. localID.toString() +
  199. ":remote_" +
  200. uploadedFileID.toString() +
  201. ":generated_" +
  202. generatedID.toString();
  203. }
  204. }