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

Feat/add factory method for file info and matrix file #27

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
43 changes: 43 additions & 0 deletions lib/src/utils/matrix_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,49 @@ class MatrixFile {
return MatrixFile(bytes: bytes, name: name, mimeType: mimeType, filePath: filePath);
}

factory MatrixFile.fromFileInfo(
{required FileInfo fileInfo}) {
final msgType = msgTypeFromMime(fileInfo.mimeType);
if (msgType == MessageTypes.Image) {
return MatrixImageFile(
name: fileInfo.fileName,
mimeType: fileInfo.mimeType,
filePath: fileInfo.filePath,
bytes: null,
width: (fileInfo.metadata['w'] as double?)?.toInt(),
height: (fileInfo.metadata['h'] as double?)?.toInt(),
);
}
if (msgType == MessageTypes.Video) {
return MatrixVideoFile(
bytes: fileInfo is VideoFileInfo
? fileInfo.imagePlaceholderBytes
: null,
name: fileInfo.fileName,
mimeType: fileInfo.mimeType,
filePath: fileInfo.filePath,
width: (fileInfo.metadata['w'] as double?)?.toInt(),
height: (fileInfo.metadata['h'] as double?)?.toInt(),
duration: fileInfo.metadata['duration'],
);
}
if (msgType == MessageTypes.Audio) {
return MatrixAudioFile(
bytes: Uint8List.fromList([]),
name: fileInfo.fileName,
mimeType: fileInfo.mimeType,
filePath: fileInfo.filePath,
duration: fileInfo.metadata['duration'],
);
}
return MatrixFile(
bytes: null,
name: fileInfo.fileName,
mimeType: fileInfo.mimeType,
filePath: fileInfo.filePath,
);
}

int get size => bytes?.length ?? 0;

String get msgType {
Expand Down
31 changes: 31 additions & 0 deletions lib/src/utils/models/file_info.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

import 'dart:typed_data';

import 'package:equatable/equatable.dart';
import 'package:matrix/matrix.dart';
import 'package:mime/mime.dart';

class FileInfo with EquatableMixin {
Expand All @@ -23,6 +26,34 @@ class FileInfo with EquatableMixin {
'mimetype': mimeType,
'size': fileSize,
});


factory FileInfo.fromMatrixFile(MatrixFile file) {
if (file.msgType == MessageTypes.Image) {
return ImageFileInfo(
file.name,
file.filePath ?? '',
file.size,
width: file.info['w'],
height: file.info['h'],
);
} else if (file.msgType == MessageTypes.Video) {
return VideoFileInfo(
file.name,
file.filePath ?? '',
file.size,
imagePlaceholderBytes: file.bytes ?? Uint8List(0),
width: file.info['w'],
height: file.info['h'],
duration: Duration(milliseconds: file.info['duration']),
);
}
return FileInfo(
file.name,
file.filePath ?? '',
file.size
);
}

@override
List<Object?> get props => [fileName, filePath, fileSize, readStream];
Expand Down
Loading