asset.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import 'package:hive/hive.dart';
  2. import 'package:immich_mobile/constants/hive_box.dart';
  3. import 'package:immich_mobile/shared/models/exif_info.dart';
  4. import 'package:openapi/api.dart';
  5. import 'package:photo_manager/photo_manager.dart';
  6. import 'package:immich_mobile/utils/builtin_extensions.dart';
  7. import 'package:path/path.dart' as p;
  8. /// Asset (online or local)
  9. class Asset {
  10. Asset.remote(AssetResponseDto remote)
  11. : remoteId = remote.id,
  12. createdAt = DateTime.parse(remote.createdAt),
  13. modifiedAt = DateTime.parse(remote.modifiedAt),
  14. durationInSeconds = remote.duration.toDuration().inSeconds,
  15. fileName = p.basename(remote.originalPath),
  16. height = remote.exifInfo?.exifImageHeight?.toInt(),
  17. width = remote.exifInfo?.exifImageWidth?.toInt(),
  18. livePhotoVideoId = remote.livePhotoVideoId,
  19. deviceAssetId = remote.deviceAssetId,
  20. deviceId = remote.deviceId,
  21. ownerId = remote.ownerId,
  22. latitude = remote.exifInfo?.latitude?.toDouble(),
  23. longitude = remote.exifInfo?.longitude?.toDouble(),
  24. exifInfo =
  25. remote.exifInfo != null ? ExifInfo.fromDto(remote.exifInfo!) : null;
  26. Asset.local(AssetEntity local, String owner)
  27. : localId = local.id,
  28. latitude = local.latitude,
  29. longitude = local.longitude,
  30. durationInSeconds = local.duration,
  31. height = local.height,
  32. width = local.width,
  33. fileName = local.title!,
  34. deviceAssetId = local.id,
  35. deviceId = Hive.box(userInfoBox).get(deviceIdKey),
  36. ownerId = owner,
  37. modifiedAt = local.modifiedDateTime.toUtc(),
  38. createdAt = local.createDateTime.toUtc() {
  39. if (createdAt.year == 1970) {
  40. createdAt = modifiedAt;
  41. }
  42. }
  43. Asset({
  44. this.localId,
  45. this.remoteId,
  46. required this.deviceAssetId,
  47. required this.deviceId,
  48. required this.ownerId,
  49. required this.createdAt,
  50. required this.modifiedAt,
  51. this.latitude,
  52. this.longitude,
  53. required this.durationInSeconds,
  54. this.width,
  55. this.height,
  56. required this.fileName,
  57. this.livePhotoVideoId,
  58. this.exifInfo,
  59. });
  60. AssetEntity? _local;
  61. AssetEntity? get local {
  62. if (isLocal && _local == null) {
  63. _local = AssetEntity(
  64. id: localId!.toString(),
  65. typeInt: isImage ? 1 : 2,
  66. width: width!,
  67. height: height!,
  68. duration: durationInSeconds,
  69. createDateSecond: createdAt.millisecondsSinceEpoch ~/ 1000,
  70. latitude: latitude,
  71. longitude: longitude,
  72. modifiedDateSecond: modifiedAt.millisecondsSinceEpoch ~/ 1000,
  73. title: fileName,
  74. );
  75. }
  76. return _local;
  77. }
  78. String? localId;
  79. String? remoteId;
  80. String deviceAssetId;
  81. String deviceId;
  82. String ownerId;
  83. DateTime createdAt;
  84. DateTime modifiedAt;
  85. double? latitude;
  86. double? longitude;
  87. int durationInSeconds;
  88. int? width;
  89. int? height;
  90. String fileName;
  91. String? livePhotoVideoId;
  92. ExifInfo? exifInfo;
  93. String get id => isLocal ? localId.toString() : remoteId!;
  94. String get name => p.withoutExtension(fileName);
  95. bool get isRemote => remoteId != null;
  96. bool get isLocal => localId != null;
  97. bool get isImage => durationInSeconds == 0;
  98. Duration get duration => Duration(seconds: durationInSeconds);
  99. @override
  100. bool operator ==(other) {
  101. if (other is! Asset) return false;
  102. return id == other.id && isLocal == other.isLocal;
  103. }
  104. @override
  105. int get hashCode => id.hashCode;
  106. // methods below are only required for caching as JSON
  107. Map<String, dynamic> toJson() {
  108. final json = <String, dynamic>{};
  109. json["localId"] = localId;
  110. json["remoteId"] = remoteId;
  111. json["deviceAssetId"] = deviceAssetId;
  112. json["deviceId"] = deviceId;
  113. json["ownerId"] = ownerId;
  114. json["createdAt"] = createdAt.millisecondsSinceEpoch;
  115. json["modifiedAt"] = modifiedAt.millisecondsSinceEpoch;
  116. json["latitude"] = latitude;
  117. json["longitude"] = longitude;
  118. json["durationInSeconds"] = durationInSeconds;
  119. json["width"] = width;
  120. json["height"] = height;
  121. json["fileName"] = fileName;
  122. json["livePhotoVideoId"] = livePhotoVideoId;
  123. if (exifInfo != null) {
  124. json["exifInfo"] = exifInfo!.toJson();
  125. }
  126. return json;
  127. }
  128. static Asset? fromJson(dynamic value) {
  129. if (value is Map) {
  130. final json = value.cast<String, dynamic>();
  131. return Asset(
  132. localId: json["localId"],
  133. remoteId: json["remoteId"],
  134. deviceAssetId: json["deviceAssetId"],
  135. deviceId: json["deviceId"],
  136. ownerId: json["ownerId"],
  137. createdAt:
  138. DateTime.fromMillisecondsSinceEpoch(json["createdAt"], isUtc: true),
  139. modifiedAt: DateTime.fromMillisecondsSinceEpoch(
  140. json["modifiedAt"],
  141. isUtc: true,
  142. ),
  143. latitude: json["latitude"],
  144. longitude: json["longitude"],
  145. durationInSeconds: json["durationInSeconds"],
  146. width: json["width"],
  147. height: json["height"],
  148. fileName: json["fileName"],
  149. livePhotoVideoId: json["livePhotoVideoId"],
  150. exifInfo: ExifInfo.fromJson(json["exifInfo"]),
  151. );
  152. }
  153. return null;
  154. }
  155. }