Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-746: support get and store fileEntity in database #28

Merged
merged 6 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/src/database/database_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import 'dart:io';
import 'dart:typed_data';

import 'package:matrix/encryption/utils/olm_session.dart';
Expand Down Expand Up @@ -97,6 +98,8 @@ abstract class DatabaseApi {

Future<Uint8List?> getFile(Uri mxcUri);

Future<File?> getFileEntity(Uri mxcUri);

Future storeFile(Uri mxcUri, Uint8List bytes, int time);

Future storeSyncFilterId(
Expand Down
6 changes: 6 additions & 0 deletions lib/src/database/hive_collections_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';

Expand Down Expand Up @@ -461,6 +462,11 @@ class HiveCollectionsDatabase extends DatabaseApi {
return null;
}

@override
Future<File?> getFileEntity(Uri mxcUri) async {
hoangdat marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

@override
Future<StoredInboundGroupSession?> getInboundGroupSession(
String roomId,
Expand Down
6 changes: 6 additions & 0 deletions lib/src/database/hive_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';

Expand Down Expand Up @@ -1466,6 +1467,11 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
// see no need to implement this in a deprecated part
throw UnimplementedError();
}

@override
Future<File?> getFileEntity(Uri mxcUri) {
throw UnimplementedError();
}
}

dynamic _castValue(dynamic value) {
Expand Down
16 changes: 10 additions & 6 deletions lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,14 @@ class Event extends MatrixEvent {
return uint8list != null;
}

bool isFileStoreable({bool getThumbnail = false}) {
final database = room.client.database;
final thisInfoMap = getThumbnail ? thumbnailInfoMap : infoMap;
return database != null &&
thisInfoMap['size'] is int &&
thisInfoMap['size'] <= database.maxFileSize;
}

/// Downloads (and decrypts if necessary) the attachment of this
/// event and returns it as a [MatrixFile]. If this event doesn't
/// contain an attachment, this throws an error. Set [getThumbnail] to
Expand All @@ -613,12 +621,8 @@ class Event extends MatrixEvent {
if (isEncrypted && !room.client.encryptionEnabled) {
throw ('Encryption is not enabled in your Client.');
}

// Is this file storeable?
final thisInfoMap = getThumbnail ? thumbnailInfoMap : infoMap;
var storeable = database != null &&
thisInfoMap['size'] is int &&
thisInfoMap['size'] <= database.maxFileSize;

var storeable = isFileStoreable(getThumbnail: getThumbnail);

Uint8List? uint8list;
if (storeable) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils/crypto/native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ abstract class Cipher {
final intPointer = memNeeded.cast<IntPtr>();
final keyPointer = memNeeded.elementAt(sizeOf<IntPtr>());
final initialVectorPointer = keyPointer.elementAt(keyDecoded.length);
final hashValuePointer = initialVectorPointer.elementAt(maxHashSize);
final hashValuePointer = initialVectorPointer.elementAt(initialVectorDecoded.length);
final hashSizePointer = hashValuePointer.elementAt(maxHashSize);

IOSink? outIoSink;
Expand Down
8 changes: 4 additions & 4 deletions lib/src/utils/matrix_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class MatrixFile {
mimeType: fileInfo.mimeType,
filePath: fileInfo.filePath,
bytes: null,
width: (fileInfo.metadata['w'] as double?)?.toInt(),
height: (fileInfo.metadata['h'] as double?)?.toInt(),
width: fileInfo.metadata['w'],
height: fileInfo.metadata['h'],
);
}
if (msgType == MessageTypes.Video) {
Expand All @@ -89,8 +89,8 @@ class MatrixFile {
name: fileInfo.fileName,
mimeType: fileInfo.mimeType,
filePath: fileInfo.filePath,
width: (fileInfo.metadata['w'] as double?)?.toInt(),
height: (fileInfo.metadata['h'] as double?)?.toInt(),
width: fileInfo.metadata['w'],
height: fileInfo.metadata['h'],
duration: fileInfo.metadata['duration'],
);
}
Expand Down
7 changes: 6 additions & 1 deletion lib/src/utils/models/file_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ class FileInfo with EquatableMixin {
final int fileSize;
final Stream<List<int>>? readStream;

FileInfo(this.fileName, this.filePath, this.fileSize, {this.readStream});
FileInfo(
this.fileName,
this.filePath,
this.fileSize,{
this.readStream,
});

factory FileInfo.empty() {
return FileInfo('', '', 0);
Expand Down
7 changes: 4 additions & 3 deletions lib/src/utils/models/image_file_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import 'package:matrix/matrix.dart';
class ImageFileInfo extends FileInfo {
ImageFileInfo(
super.fileName,
super.filePath,
super.filePath,
super.fileSize, {
super.readStream,
this.width,
this.height,
}
Expand All @@ -18,8 +19,8 @@ class ImageFileInfo extends FileInfo {
Map<String, dynamic> get metadata => ({
'mimetype': mimeType,
'size': fileSize,
'w': width?.toDouble(),
'h': height?.toDouble(),
'w': width,
'h': height,
});

@override
Expand Down
5 changes: 3 additions & 2 deletions lib/src/utils/models/video_file_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class VideoFileInfo extends FileInfo {
super.fileName,
super.filePath,
super.fileSize, {
super.readStream,
required this.imagePlaceholderBytes,
this.width,
this.height,
Expand All @@ -25,8 +26,8 @@ class VideoFileInfo extends FileInfo {
Map<String, dynamic> get metadata => ({
'mimetype': mimeType,
'size': fileSize,
'w': width?.toDouble(),
'h': height?.toDouble(),
'w': width,
'h': height,
if (duration != null) 'duration': duration!.inMilliseconds,
});

Expand Down
Loading