Skip to content

Commit

Permalink
feat/add factory method for matrixFile
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed Oct 17, 2023
1 parent 670f3c8 commit 6365306
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
41 changes: 41 additions & 0 deletions lib/src/utils/matrix_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,47 @@ 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: 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
20 changes: 12 additions & 8 deletions lib/src/utils/models/file_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,31 @@ class FileInfo with EquatableMixin {
});


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

@override
Expand Down

0 comments on commit 6365306

Please sign in to comment.