file.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/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. 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. // public magic metadata is shared if during file/album sharing
  43. String pubMmdEncodedJson;
  44. int pubMmdVersion = 0;
  45. PubMagicMetadata _pubMmd;
  46. PubMagicMetadata get pubMagicMetadata =>
  47. _pubMmd ?? PubMagicMetadata.fromEncodedJson(pubMmdEncodedJson ?? '{}');
  48. set pubMagicMetadata(val) => _pubMmd = val;
  49. static const kCurrentMetadataVersion = 1;
  50. File();
  51. static Future<File> fromAsset(String pathName, AssetEntity asset) async {
  52. File file = File();
  53. file.localID = asset.id;
  54. file.title = asset.title;
  55. file.deviceFolder = pathName;
  56. file.location = Location(asset.latitude, asset.longitude);
  57. file.fileType = _fileTypeFromAsset(asset);
  58. file.creationTime = asset.createDateTime.microsecondsSinceEpoch;
  59. if (file.creationTime == 0) {
  60. try {
  61. final parsedDateTime = DateTime.parse(
  62. basenameWithoutExtension(file.title)
  63. .replaceAll("IMG_", "")
  64. .replaceAll("VID_", "")
  65. .replaceAll("DCIM_", "")
  66. .replaceAll("_", " "));
  67. file.creationTime = parsedDateTime.microsecondsSinceEpoch;
  68. } catch (e) {
  69. file.creationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  70. }
  71. }
  72. file.modificationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  73. file.fileSubType = asset.subTypes;
  74. file.metadataVersion = kCurrentMetadataVersion;
  75. return file;
  76. }
  77. static FileType _fileTypeFromAsset(AssetEntity asset) {
  78. FileType type = FileType.image;
  79. switch (asset.type) {
  80. case AssetType.image:
  81. type = FileType.image;
  82. // PHAssetMediaSubtype.photoLive.rawValue is 8
  83. // This hack should go away once photos_manager support livePhotos
  84. if (asset.subTypes != null &&
  85. asset.subTypes > -1 &&
  86. (asset.subTypes & 8) != 0) {
  87. type = FileType.livePhoto;
  88. }
  89. break;
  90. case AssetType.video:
  91. type = FileType.video;
  92. break;
  93. default:
  94. type = FileType.other;
  95. break;
  96. }
  97. return type;
  98. }
  99. Future<AssetEntity> getAsset() {
  100. if (localID == null) {
  101. return Future.value(null);
  102. }
  103. return AssetEntity.fromId(localID);
  104. }
  105. void applyMetadata(Map<String, dynamic> metadata) {
  106. localID = metadata["localID"];
  107. title = metadata["title"];
  108. deviceFolder = metadata["deviceFolder"];
  109. creationTime = metadata["creationTime"] ?? 0;
  110. modificationTime = metadata["modificationTime"] ?? creationTime;
  111. final latitude = double.tryParse(metadata["latitude"].toString());
  112. final longitude = double.tryParse(metadata["longitude"].toString());
  113. if (latitude == null || longitude == null) {
  114. location = null;
  115. } else {
  116. location = Location(latitude, longitude);
  117. }
  118. fileType = getFileType(metadata["fileType"]);
  119. fileSubType = metadata["subType"] ?? -1;
  120. duration = metadata["duration"] ?? 0;
  121. exif = metadata["exif"];
  122. hash = metadata["hash"];
  123. metadataVersion = metadata["version"] ?? 0;
  124. }
  125. Future<Map<String, dynamic>> getMetadataForUpload(io.File sourceFile) async {
  126. final asset = await getAsset();
  127. // asset can be null for files shared to app
  128. if (asset != null) {
  129. fileSubType = asset.subTypes;
  130. if (fileType == FileType.video) {
  131. duration = asset.duration;
  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. }