file.dart 6.5 KB

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