file.dart 7.7 KB

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