ente/lib/models/photo.dart

105 lines
3 KiB
Dart
Raw Normal View History

import 'dart:io';
2020-04-24 12:40:24 +00:00
import 'dart:typed_data';
2020-03-28 13:56:06 +00:00
import 'package:flutter/foundation.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';
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-05-26 19:33:29 +00:00
String thumbnailPath;
2020-04-13 11:28:01 +00:00
int createTimestamp;
int updateTimestamp;
2020-03-27 16:07:55 +00:00
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-05-26 19:33:29 +00:00
thumbnailPath = json["previewURL"],
2020-04-13 11:28:01 +00:00
createTimestamp = json["createTimestamp"],
updateTimestamp = json["updateTimestamp"];
2020-03-27 16:07:55 +00:00
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}) {
if (localId == null) {
return HttpClient().getUrl(Uri.parse(getRemoteUrl())).then((request) {
return request.close().then((response) {
return consolidateHttpClientResponseBytes(response);
});
});
} else {
return getAsset().originBytes;
2020-04-24 12:40:24 +00:00
}
2020-04-23 20:00:20 +00:00
}
2020-05-25 15:07:22 +00:00
String getRemoteUrl() {
return Configuration.instance.getHttpEndpoint() +
"/files/file/" +
uploadedFileId.toString();
2020-05-25 15:07:22 +00:00
}
2020-05-27 14:20:52 +00:00
String getThumbnailUrl() {
return Configuration.instance.getHttpEndpoint() + "/" + thumbnailPath;
}
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() {
return 'Photo(generatedId: $generatedId, uploadedFileId: $uploadedFileId, localId: $localId, title: $title, deviceFolder: $deviceFolder, remotePath: $remotePath, createTimestamp: $createTimestamp, updateTimestamp: $updateTimestamp)';
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;
2020-04-24 12:40:24 +00:00
}
@override
int get hashCode {
return generatedId.hashCode ^ uploadedFileId.hashCode ^ localId.hashCode;
}
2020-03-30 14:28:46 +00:00
}