ente/lib/models/file.dart

165 lines
4.7 KiB
Dart
Raw Normal View History

2021-03-01 23:43:36 +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-06-19 23:03:26 +00:00
import 'package:photos/models/file_type.dart';
2020-06-03 16:06:49 +00:00
import 'package:photos/models/location.dart';
2020-03-28 13:56:06 +00:00
2020-06-19 23:03:26 +00:00
class File {
2020-08-09 22:34:59 +00:00
int generatedID;
int uploadedFileID;
int ownerID;
int collectionID;
2020-08-09 22:34:59 +00:00
String localID;
2020-04-24 12:40:24 +00:00
String title;
2020-05-17 12:39:38 +00:00
String deviceFolder;
int creationTime;
int modificationTime;
int updationTime;
2020-06-03 16:06:49 +00:00
Location location;
2020-06-19 23:03:26 +00:00
FileType fileType;
String encryptedKey;
String keyDecryptionNonce;
String fileDecryptionHeader;
String thumbnailDecryptionHeader;
String metadataDecryptionHeader;
2020-03-27 16:07:55 +00:00
2020-06-19 23:03:26 +00:00
File();
2020-08-11 23:04:16 +00:00
2020-06-19 23:03:26 +00:00
static Future<File> fromAsset(
2020-04-24 12:40:24 +00:00
AssetPathEntity pathEntity, AssetEntity asset) async {
2020-06-19 23:03:26 +00:00
File file = File();
2020-08-09 22:34:59 +00:00
file.localID = asset.id;
2020-06-19 23:03:26 +00:00
file.title = asset.title;
file.deviceFolder = pathEntity.name;
file.location = Location(asset.latitude, asset.longitude);
switch (asset.type) {
case AssetType.image:
file.fileType = FileType.image;
break;
case AssetType.video:
file.fileType = FileType.video;
break;
default:
file.fileType = FileType.other;
break;
}
file.creationTime = asset.createDateTime.microsecondsSinceEpoch;
if (file.creationTime == 0) {
try {
final parsedDateTime = DateTime.parse(
2020-06-19 23:03:26 +00:00
basenameWithoutExtension(file.title)
.replaceAll("IMG_", "")
.replaceAll("DCIM_", "")
.replaceAll("_", " "));
file.creationTime = parsedDateTime.microsecondsSinceEpoch;
} catch (e) {
file.creationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
}
}
file.modificationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
2020-06-19 23:03:26 +00:00
return file;
2020-03-30 15:08:50 +00:00
}
2020-06-13 16:44:16 +00:00
Future<AssetEntity> getAsset() {
2020-08-09 22:34:59 +00:00
return AssetEntity.fromId(localID);
2020-04-25 10:28:22 +00:00
}
2020-08-11 23:04:16 +00:00
void applyMetadata(Map<String, dynamic> metadata) {
localID = metadata["localID"];
title = metadata["title"];
deviceFolder = metadata["deviceFolder"];
creationTime = metadata["creationTime"];
modificationTime = metadata["modificationTime"];
final latitude = metadata["latitude"];
final longitude = metadata["longitude"];
2021-01-13 08:29:17 +00:00
if (latitude == null || longitude == null) {
location = null;
} else {
location = Location(latitude, longitude);
}
2020-08-11 23:04:16 +00:00
fileType = getFileType(metadata["fileType"]);
}
2020-08-10 23:47:22 +00:00
Map<String, dynamic> getMetadata() {
final metadata = Map<String, dynamic>();
metadata["localID"] = localID;
metadata["title"] = title;
metadata["deviceFolder"] = deviceFolder;
metadata["creationTime"] = creationTime;
metadata["modificationTime"] = modificationTime;
2021-01-19 08:30:48 +00:00
if (location != null &&
location.latitude != null &&
location.longitude != null) {
metadata["latitude"] = location.latitude;
metadata["longitude"] = location.longitude;
}
2020-08-10 23:47:22 +00:00
metadata["fileType"] = fileType.index;
return metadata;
}
2020-07-08 19:46:04 +00:00
String getDownloadUrl() {
2021-03-01 23:43:36 +00:00
if (kDebugMode) {
return Configuration.instance.getHttpEndpoint() +
"/files/download/" +
uploadedFileID.toString();
} else {
return "https://files.ente.workers.dev/?fileID=" +
uploadedFileID.toString();
}
2020-05-25 15:07:22 +00:00
}
// Passing token within the URL due to https://github.com/flutter/flutter/issues/16466
String getStreamUrl() {
return Configuration.instance.getHttpEndpoint() +
"/streams/" +
Configuration.instance.getToken() +
"/" +
2020-08-09 22:34:59 +00:00
uploadedFileID.toString() +
"/index.m3u8";
}
2020-05-27 14:20:52 +00:00
String getThumbnailUrl() {
2021-03-01 23:43:36 +00:00
if (kDebugMode) {
return Configuration.instance.getHttpEndpoint() +
"/files/preview/" +
uploadedFileID.toString();
} else {
return "https://thumbnails.ente.workers.dev/?fileID=" +
uploadedFileID.toString();
}
2020-05-27 14:20:52 +00:00
}
@override
2020-04-23 20:00:20 +00:00
String toString() {
2020-08-09 22:34:59 +00:00
return '''File(generatedId: $generatedID, uploadedFileId: $uploadedFileID,
localID: $localID, ownerID: $ownerID, collectionID: $collectionID,
fileType: $fileType, creationTime: $creationTime,
modificationTime: $modificationTime, updationTime: $updationTime)''';
2020-04-24 12:40:24 +00:00
}
@override
bool operator ==(Object o) {
if (identical(this, o)) return true;
2020-06-19 23:03:26 +00:00
return o is File &&
2020-08-09 22:34:59 +00:00
o.generatedID == generatedID &&
o.uploadedFileID == uploadedFileID &&
o.localID == localID;
2020-04-24 12:40:24 +00:00
}
@override
int get hashCode {
2020-08-09 22:34:59 +00:00
return generatedID.hashCode ^ uploadedFileID.hashCode ^ localID.hashCode;
}
String tag() {
return "local_" +
2020-08-09 22:34:59 +00:00
localID.toString() +
":remote_" +
2020-08-09 22:34:59 +00:00
uploadedFileID.toString() +
":generated_" +
2020-08-09 22:34:59 +00:00
generatedID.toString();
}
2020-03-30 14:28:46 +00:00
}