123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- import 'package:hive/hive.dart';
- import 'package:immich_mobile/constants/hive_box.dart';
- import 'package:immich_mobile/shared/models/exif_info.dart';
- import 'package:openapi/api.dart';
- import 'package:photo_manager/photo_manager.dart';
- import 'package:immich_mobile/utils/builtin_extensions.dart';
- import 'package:path/path.dart' as p;
- /// Asset (online or local)
- class Asset {
- Asset.remote(AssetResponseDto remote)
- : remoteId = remote.id,
- createdAt = DateTime.parse(remote.createdAt),
- modifiedAt = DateTime.parse(remote.modifiedAt),
- durationInSeconds = remote.duration.toDuration().inSeconds,
- fileName = p.basename(remote.originalPath),
- height = remote.exifInfo?.exifImageHeight?.toInt(),
- width = remote.exifInfo?.exifImageWidth?.toInt(),
- livePhotoVideoId = remote.livePhotoVideoId,
- deviceAssetId = remote.deviceAssetId,
- deviceId = remote.deviceId,
- ownerId = remote.ownerId,
- latitude = remote.exifInfo?.latitude?.toDouble(),
- longitude = remote.exifInfo?.longitude?.toDouble(),
- exifInfo =
- remote.exifInfo != null ? ExifInfo.fromDto(remote.exifInfo!) : null;
- Asset.local(AssetEntity local, String owner)
- : localId = local.id,
- latitude = local.latitude,
- longitude = local.longitude,
- durationInSeconds = local.duration,
- height = local.height,
- width = local.width,
- fileName = local.title!,
- deviceAssetId = local.id,
- deviceId = Hive.box(userInfoBox).get(deviceIdKey),
- ownerId = owner,
- modifiedAt = local.modifiedDateTime.toUtc(),
- createdAt = local.createDateTime.toUtc() {
- if (createdAt.year == 1970) {
- createdAt = modifiedAt;
- }
- }
- Asset({
- this.localId,
- this.remoteId,
- required this.deviceAssetId,
- required this.deviceId,
- required this.ownerId,
- required this.createdAt,
- required this.modifiedAt,
- this.latitude,
- this.longitude,
- required this.durationInSeconds,
- this.width,
- this.height,
- required this.fileName,
- this.livePhotoVideoId,
- this.exifInfo,
- });
- AssetEntity? _local;
- AssetEntity? get local {
- if (isLocal && _local == null) {
- _local = AssetEntity(
- id: localId!.toString(),
- typeInt: isImage ? 1 : 2,
- width: width!,
- height: height!,
- duration: durationInSeconds,
- createDateSecond: createdAt.millisecondsSinceEpoch ~/ 1000,
- latitude: latitude,
- longitude: longitude,
- modifiedDateSecond: modifiedAt.millisecondsSinceEpoch ~/ 1000,
- title: fileName,
- );
- }
- return _local;
- }
- String? localId;
- String? remoteId;
- String deviceAssetId;
- String deviceId;
- String ownerId;
- DateTime createdAt;
- DateTime modifiedAt;
- double? latitude;
- double? longitude;
- int durationInSeconds;
- int? width;
- int? height;
- String fileName;
- String? livePhotoVideoId;
- ExifInfo? exifInfo;
- String get id => isLocal ? localId.toString() : remoteId!;
- String get name => p.withoutExtension(fileName);
- bool get isRemote => remoteId != null;
- bool get isLocal => localId != null;
- bool get isImage => durationInSeconds == 0;
- Duration get duration => Duration(seconds: durationInSeconds);
- @override
- bool operator ==(other) {
- if (other is! Asset) return false;
- return id == other.id && isLocal == other.isLocal;
- }
- @override
- int get hashCode => id.hashCode;
- // methods below are only required for caching as JSON
- Map<String, dynamic> toJson() {
- final json = <String, dynamic>{};
- json["localId"] = localId;
- json["remoteId"] = remoteId;
- json["deviceAssetId"] = deviceAssetId;
- json["deviceId"] = deviceId;
- json["ownerId"] = ownerId;
- json["createdAt"] = createdAt.millisecondsSinceEpoch;
- json["modifiedAt"] = modifiedAt.millisecondsSinceEpoch;
- json["latitude"] = latitude;
- json["longitude"] = longitude;
- json["durationInSeconds"] = durationInSeconds;
- json["width"] = width;
- json["height"] = height;
- json["fileName"] = fileName;
- json["livePhotoVideoId"] = livePhotoVideoId;
- if (exifInfo != null) {
- json["exifInfo"] = exifInfo!.toJson();
- }
- return json;
- }
- static Asset? fromJson(dynamic value) {
- if (value is Map) {
- final json = value.cast<String, dynamic>();
- return Asset(
- localId: json["localId"],
- remoteId: json["remoteId"],
- deviceAssetId: json["deviceAssetId"],
- deviceId: json["deviceId"],
- ownerId: json["ownerId"],
- createdAt:
- DateTime.fromMillisecondsSinceEpoch(json["createdAt"], isUtc: true),
- modifiedAt: DateTime.fromMillisecondsSinceEpoch(
- json["modifiedAt"],
- isUtc: true,
- ),
- latitude: json["latitude"],
- longitude: json["longitude"],
- durationInSeconds: json["durationInSeconds"],
- width: json["width"],
- height: json["height"],
- fileName: json["fileName"],
- livePhotoVideoId: json["livePhotoVideoId"],
- exifInfo: ExifInfo.fromJson(json["exifInfo"]),
- );
- }
- return null;
- }
- }
|