file.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/file_type.dart';
  7. import 'package:photos/models/location.dart';
  8. class File {
  9. int generatedID;
  10. int uploadedFileID;
  11. int ownerID;
  12. int collectionID;
  13. String localID;
  14. String title;
  15. String deviceFolder;
  16. int creationTime;
  17. int modificationTime;
  18. int updationTime;
  19. Location location;
  20. FileType fileType;
  21. String encryptedKey;
  22. String keyDecryptionNonce;
  23. String fileDecryptionHeader;
  24. String thumbnailDecryptionHeader;
  25. String metadataDecryptionHeader;
  26. File();
  27. static Future<File> fromAsset(String pathName, AssetEntity asset) async {
  28. File file = File();
  29. file.localID = asset.id;
  30. file.title = asset.title;
  31. file.deviceFolder = pathName;
  32. file.location = Location(asset.latitude, asset.longitude);
  33. switch (asset.type) {
  34. case AssetType.image:
  35. file.fileType = FileType.image;
  36. break;
  37. case AssetType.video:
  38. file.fileType = FileType.video;
  39. break;
  40. default:
  41. file.fileType = FileType.other;
  42. break;
  43. }
  44. file.creationTime = asset.createDateTime.microsecondsSinceEpoch;
  45. if (file.creationTime == 0) {
  46. try {
  47. final parsedDateTime = DateTime.parse(
  48. basenameWithoutExtension(file.title)
  49. .replaceAll("IMG_", "")
  50. .replaceAll("DCIM_", "")
  51. .replaceAll("_", " "));
  52. file.creationTime = parsedDateTime.microsecondsSinceEpoch;
  53. } catch (e) {
  54. file.creationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  55. }
  56. }
  57. file.modificationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
  58. return file;
  59. }
  60. Future<AssetEntity> getAsset() {
  61. if (localID == null) {
  62. return Future.value(null);
  63. }
  64. return AssetEntity.fromId(localID);
  65. }
  66. void applyMetadata(Map<String, dynamic> metadata) {
  67. localID = metadata["localID"];
  68. title = metadata["title"];
  69. deviceFolder = metadata["deviceFolder"];
  70. creationTime = metadata["creationTime"] ?? 0;
  71. modificationTime = metadata["modificationTime"] ?? creationTime;
  72. final latitude = double.tryParse(metadata["latitude"].toString());
  73. final longitude = double.tryParse(metadata["longitude"].toString());
  74. if (latitude == null || longitude == null) {
  75. location = null;
  76. } else {
  77. location = Location(latitude, longitude);
  78. }
  79. fileType = getFileType(metadata["fileType"]);
  80. }
  81. Map<String, dynamic> getMetadata() {
  82. final metadata = <String, dynamic>{};
  83. metadata["localID"] = isCachedInAppSandbox() ? null : localID;
  84. metadata["title"] = title;
  85. metadata["deviceFolder"] = deviceFolder;
  86. metadata["creationTime"] = creationTime;
  87. metadata["modificationTime"] = modificationTime;
  88. if (location != null &&
  89. location.latitude != null &&
  90. location.longitude != null) {
  91. metadata["latitude"] = location.latitude;
  92. metadata["longitude"] = location.longitude;
  93. }
  94. metadata["fileType"] = fileType.index;
  95. return metadata;
  96. }
  97. String getDownloadUrl() {
  98. if (kDebugMode) {
  99. return Configuration.instance.getHttpEndpoint() +
  100. "/files/download/" +
  101. uploadedFileID.toString();
  102. } else {
  103. return "https://files.ente.workers.dev/?fileID=" +
  104. uploadedFileID.toString();
  105. }
  106. }
  107. // Passing token within the URL due to https://github.com/flutter/flutter/issues/16466
  108. String getStreamUrl() {
  109. return Configuration.instance.getHttpEndpoint() +
  110. "/streams/" +
  111. Configuration.instance.getToken() +
  112. "/" +
  113. uploadedFileID.toString() +
  114. "/index.m3u8";
  115. }
  116. String getThumbnailUrl() {
  117. if (kDebugMode) {
  118. return Configuration.instance.getHttpEndpoint() +
  119. "/files/preview/" +
  120. uploadedFileID.toString();
  121. } else {
  122. return "https://thumbnails.ente.workers.dev/?fileID=" +
  123. uploadedFileID.toString();
  124. }
  125. }
  126. // returns true if the file isn't available in the user's gallery
  127. bool isRemoteFile() {
  128. return localID == null && uploadedFileID != null;
  129. }
  130. bool isCachedInAppSandbox() {
  131. return localID != null && localID.startsWith(kSharedMediaIdentifier);
  132. }
  133. bool hasLocation() {
  134. return location != null &&
  135. (location.longitude != 0 || location.latitude != 0);
  136. }
  137. @override
  138. String toString() {
  139. return '''File(generatedId: $generatedID, uploadedFileId: $uploadedFileID,
  140. localID: $localID, ownerID: $ownerID, collectionID: $collectionID,
  141. fileType: $fileType, creationTime: $creationTime,
  142. modificationTime: $modificationTime, updationTime: $updationTime)''';
  143. }
  144. @override
  145. bool operator ==(Object o) {
  146. if (identical(this, o)) return true;
  147. return o is File &&
  148. o.generatedID == generatedID &&
  149. o.uploadedFileID == uploadedFileID &&
  150. o.localID == localID;
  151. }
  152. @override
  153. int get hashCode {
  154. return generatedID.hashCode ^ uploadedFileID.hashCode ^ localID.hashCode;
  155. }
  156. String tag() {
  157. return "local_" +
  158. localID.toString() +
  159. ":remote_" +
  160. uploadedFileID.toString() +
  161. ":generated_" +
  162. generatedID.toString();
  163. }
  164. }