file.dart 7.3 KB

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