ente/lib/models/file.dart

156 lines
4.5 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';
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';
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 {
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-06-19 23:03:26 +00:00
String previewURL;
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;
2020-03-27 16:07:55 +00:00
2020-06-19 23:03:26 +00:00
File();
File.fromJson(Map<String, dynamic> json) {
uploadedFileId = json["fileID"];
localId = json["deviceFileID"];
deviceFolder = json["deviceFolder"];
title = json["title"];
fileType = getFileType(json["fileType"]);
remotePath = json["path"];
previewURL = json["previewURL"];
creationTime = json["creationTime"];
modificationTime = json["modificationTime"];
updationTime = json["updationTime"];
2020-06-19 23:03:26 +00:00
}
2020-03-27 16:07:55 +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();
file.localId = asset.id;
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() {
return AssetEntity.fromId(localId);
2020-04-25 10:28:22 +00:00
}
2020-06-13 16:44:16 +00:00
Future<Uint8List> getBytes({int quality = 100}) async {
if (localId == null) {
return HttpClient().getUrl(Uri.parse(getRemoteUrl())).then((request) {
return request.close().then((response) {
return consolidateHttpClientResponseBytes(response);
});
});
} else {
2020-06-13 16:44:16 +00:00
final originalBytes = (await getAsset()).originBytes;
if (extension(title) == ".HEIC" || quality != 100) {
return originalBytes.then((bytes) {
return FlutterImageCompress.compressWithList(bytes, quality: quality)
.then((converted) {
return Uint8List.fromList(converted);
});
});
} else {
return originalBytes;
}
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() +
"?token=" +
Configuration.instance.getToken();
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() +
"/" +
uploadedFileId.toString() +
"/index.m3u8";
}
2020-05-27 14:20:52 +00:00
String getThumbnailUrl() {
2020-06-19 23:03:26 +00:00
return Configuration.instance.getHttpEndpoint() + "/" + previewURL;
2020-05-27 14:20:52 +00:00
}
@override
2020-04-23 20:00:20 +00:00
String toString() {
2020-06-19 23:03:26 +00:00
return '''File(generatedId: $generatedId, uploadedFileId: $uploadedFileId,
localId: $localId, title: $title, deviceFolder: $deviceFolder,
location: $location, remotePath: $remotePath, fileType: $fileType,
createTimestamp: $creationTime, updateTimestamp: $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-04-24 12:40:24 +00:00
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;
}
String tag() {
return "local_" +
localId.toString() +
":remote_" +
uploadedFileId.toString() +
":generated_" +
generatedId.toString();
}
2020-03-30 14:28:46 +00:00
}