file.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import 'package:flutter/foundation.dart';
  2. import 'package:path/path.dart';
  3. import 'package:photo_manager/photo_manager.dart';
  4. import 'package:photos/core/configuration.dart';
  5. import 'package:photos/core/constants.dart';
  6. import 'package:photos/models/ente_file.dart';
  7. import 'package:photos/models/file_type.dart';
  8. import 'package:photos/models/location.dart';
  9. import 'package:photos/models/magic_metadata.dart';
  10. import 'package:photos/services/feature_flag_service.dart';
  11. import 'package:photos/utils/exif_util.dart';
  12. import 'package:photos/utils/file_uploader_util.dart';
  13. class File extends EnteFile {
  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. );
  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(
  127. MediaUploadData mediaUploadData,
  128. ) async {
  129. final asset = await getAsset();
  130. // asset can be null for files shared to app
  131. if (asset != null) {
  132. fileSubType = asset.subtype;
  133. if (fileType == FileType.video) {
  134. duration = asset.duration;
  135. }
  136. }
  137. if (fileType == FileType.image) {
  138. final exifTime =
  139. await getCreationTimeFromEXIF(mediaUploadData.sourceFile);
  140. if (exifTime != null) {
  141. creationTime = exifTime.microsecondsSinceEpoch;
  142. }
  143. }
  144. // in metadataVersion V1, the hash for livePhoto is the hash of the
  145. // zipped file.
  146. // web uploads files without MetadataVersion and upload image hash as 'ha
  147. // sh' key and video as 'vidHash'
  148. hash = (fileType == FileType.livePhoto)
  149. ? mediaUploadData.zipHash
  150. : mediaUploadData.fileHash;
  151. return getMetadata();
  152. }
  153. Map<String, dynamic> getMetadata() {
  154. final metadata = <String, dynamic>{};
  155. metadata["localID"] = isSharedMediaToAppSandbox() ? null : localID;
  156. metadata["title"] = title;
  157. metadata["deviceFolder"] = deviceFolder;
  158. metadata["creationTime"] = creationTime;
  159. metadata["modificationTime"] = modificationTime;
  160. metadata["fileType"] = fileType.index;
  161. if (location != null &&
  162. location.latitude != null &&
  163. location.longitude != null) {
  164. metadata["latitude"] = location.latitude;
  165. metadata["longitude"] = location.longitude;
  166. }
  167. if (fileSubType != null) {
  168. metadata["subType"] = fileSubType;
  169. }
  170. if (duration != null) {
  171. metadata["duration"] = duration;
  172. }
  173. if (hash != null) {
  174. metadata["hash"] = hash;
  175. }
  176. if (metadataVersion != null) {
  177. metadata["version"] = metadataVersion;
  178. }
  179. return metadata;
  180. }
  181. String getDownloadUrl() {
  182. if (kDebugMode || FeatureFlagService.instance.disableCFWorker()) {
  183. return Configuration.instance.getHttpEndpoint() +
  184. "/files/download/" +
  185. uploadedFileID.toString();
  186. } else {
  187. return "https://files.ente.workers.dev/?fileID=" +
  188. uploadedFileID.toString();
  189. }
  190. }
  191. String getThumbnailUrl() {
  192. if (kDebugMode || FeatureFlagService.instance.disableCFWorker()) {
  193. return Configuration.instance.getHttpEndpoint() +
  194. "/files/preview/" +
  195. uploadedFileID.toString();
  196. } else {
  197. return "https://thumbnails.ente.workers.dev/?fileID=" +
  198. uploadedFileID.toString();
  199. }
  200. }
  201. String getDisplayName() {
  202. if (pubMagicMetadata != null && pubMagicMetadata.editedName != null) {
  203. return pubMagicMetadata.editedName;
  204. }
  205. return title;
  206. }
  207. // returns true if the file isn't available in the user's gallery
  208. bool isRemoteFile() {
  209. return localID == null && uploadedFileID != null;
  210. }
  211. bool isSharedMediaToAppSandbox() {
  212. return localID != null &&
  213. (localID.startsWith(kOldSharedMediaIdentifier) ||
  214. localID.startsWith(kSharedMediaIdentifier));
  215. }
  216. bool hasLocation() {
  217. return location != null &&
  218. (location.longitude != 0 || location.latitude != 0);
  219. }
  220. @override
  221. String toString() {
  222. return '''File(generatedID: $generatedID, localID: $localID, title: $title,
  223. uploadedFileId: $uploadedFileID, modificationTime: $modificationTime,
  224. ownerID: $ownerID, collectionID: $collectionID, updationTime: $updationTime)''';
  225. }
  226. @override
  227. bool operator ==(Object o) {
  228. if (identical(this, o)) return true;
  229. return o is File &&
  230. o.generatedID == generatedID &&
  231. o.uploadedFileID == uploadedFileID &&
  232. o.localID == localID;
  233. }
  234. @override
  235. int get hashCode {
  236. return generatedID.hashCode ^ uploadedFileID.hashCode ^ localID.hashCode;
  237. }
  238. String tag() {
  239. return "local_" +
  240. localID.toString() +
  241. ":remote_" +
  242. uploadedFileID.toString() +
  243. ":generated_" +
  244. generatedID.toString();
  245. }
  246. @override
  247. String cacheKey() {
  248. // todo: Neeraj: 19thJuly'22: evaluate and add fileHash as the key?
  249. return localID ?? uploadedFileID?.toString() ?? generatedID?.toString();
  250. }
  251. @override
  252. String localIdentifier() {
  253. return localID;
  254. }
  255. }