ente/lib/models/photo.dart

108 lines
3.2 KiB
Dart
Raw Normal View History

2020-04-24 12:40:24 +00:00
import 'dart:typed_data';
2020-03-28 13:56:06 +00:00
2020-04-24 12:40:24 +00:00
import 'package:flutter_image_compress/flutter_image_compress.dart';
2020-03-28 13:56:06 +00:00
import 'package:photo_manager/photo_manager.dart';
2020-04-24 12:40:24 +00:00
import 'package:path/path.dart';
import 'package:logging/logging.dart';
2020-05-25 15:07:22 +00:00
import 'package:photos/core/configuration.dart';
2020-03-28 13:56:06 +00:00
2020-03-27 16:07:55 +00:00
class Photo {
int generatedId;
2020-04-12 12:38:49 +00:00
int uploadedFileId;
2020-04-11 21:29:07 +00:00
String localId;
2020-04-24 12:40:24 +00:00
String title;
2020-05-17 12:39:38 +00:00
String deviceFolder;
2020-05-22 18:22:55 +00:00
int remoteFolderId;
2020-04-24 12:40:24 +00:00
String remotePath;
2020-04-13 11:28:01 +00:00
int createTimestamp;
2020-03-27 16:07:55 +00:00
int syncTimestamp;
2020-03-28 13:56:06 +00:00
Photo();
2020-03-27 16:07:55 +00:00
Photo.fromJson(Map<String, dynamic> json)
: uploadedFileId = json["fileID"],
localId = json["deviceFileID"],
2020-05-17 12:39:38 +00:00
deviceFolder = json["deviceFolder"],
title = json["title"],
2020-05-17 12:39:38 +00:00
remotePath = json["path"],
2020-04-13 11:28:01 +00:00
createTimestamp = json["createTimestamp"],
2020-03-27 16:07:55 +00:00
syncTimestamp = json["syncTimestamp"];
2020-04-24 12:40:24 +00:00
static Future<Photo> fromAsset(
AssetPathEntity pathEntity, AssetEntity asset) async {
2020-03-28 13:56:06 +00:00
Photo photo = Photo();
2020-04-12 12:38:49 +00:00
photo.uploadedFileId = -1;
2020-04-11 21:29:07 +00:00
photo.localId = asset.id;
2020-04-24 12:40:24 +00:00
photo.title = asset.title;
2020-05-17 12:39:38 +00:00
photo.deviceFolder = pathEntity.name;
photo.createTimestamp = asset.createDateTime.microsecondsSinceEpoch;
if (photo.createTimestamp == 0) {
try {
final parsedDateTime = DateTime.parse(
basenameWithoutExtension(photo.title)
.replaceAll("IMG_", "")
.replaceAll("DCIM_", "")
.replaceAll("_", " "));
photo.createTimestamp = parsedDateTime.microsecondsSinceEpoch;
} catch (e) {
photo.createTimestamp = asset.modifiedDateTime.microsecondsSinceEpoch;
}
}
2020-04-11 11:15:27 +00:00
return photo;
2020-03-30 15:08:50 +00:00
}
2020-04-25 10:28:22 +00:00
AssetEntity getAsset() {
return AssetEntity(id: localId);
}
2020-04-24 19:11:24 +00:00
Future<Uint8List> getBytes({int quality = 100}) {
2020-04-25 10:28:22 +00:00
final asset = getAsset();
2020-04-24 19:11:24 +00:00
if (extension(title) == ".HEIC" || quality != 100) {
2020-04-24 12:40:24 +00:00
return asset.originBytes.then((bytes) =>
2020-04-24 19:11:24 +00:00
FlutterImageCompress.compressWithList(bytes, quality: quality)
2020-04-24 12:40:24 +00:00
.then((result) => Uint8List.fromList(result)));
} else {
return asset.originBytes;
}
2020-04-23 20:00:20 +00:00
}
2020-05-25 15:07:22 +00:00
String getRemoteUrl() {
return Configuration.instance.getHttpEndpoint() + "/" + remotePath;
}
2020-04-24 12:40:24 +00:00
Future<Uint8List> getOriginalBytes() {
2020-04-30 16:15:18 +00:00
return getAsset().originBytes;
2020-03-28 13:56:06 +00:00
}
@override
2020-04-23 20:00:20 +00:00
String toString() {
2020-05-17 12:39:38 +00:00
return 'Photo(generatedId: $generatedId, uploadedFileId: $uploadedFileId, localId: $localId, title: $title, deviceFolder: $deviceFolder, remotePath: $remotePath, createTimestamp: $createTimestamp, syncTimestamp: $syncTimestamp)';
2020-04-24 12:40:24 +00:00
}
@override
bool operator ==(Object o) {
if (identical(this, o)) return true;
return o is Photo &&
o.generatedId == generatedId &&
o.uploadedFileId == uploadedFileId &&
o.localId == localId &&
o.title == title &&
2020-05-17 12:39:38 +00:00
o.deviceFolder == deviceFolder &&
2020-04-24 12:40:24 +00:00
o.remotePath == remotePath &&
o.createTimestamp == createTimestamp &&
o.syncTimestamp == syncTimestamp;
}
@override
int get hashCode {
return generatedId.hashCode ^
uploadedFileId.hashCode ^
localId.hashCode ^
title.hashCode ^
2020-05-17 12:39:38 +00:00
deviceFolder.hashCode ^
2020-04-24 12:40:24 +00:00
remotePath.hashCode ^
createTimestamp.hashCode ^
syncTimestamp.hashCode;
}
2020-03-30 14:28:46 +00:00
}