famedlysdk/lib/src/utils/matrix_file.dart

116 lines
2.8 KiB
Dart
Raw Normal View History

2020-01-02 14:09:49 +00:00
/// Workaround until [File] in dart:io and dart:html is unified
import 'dart:typed_data';
2020-03-16 10:38:03 +00:00
import 'package:matrix_file_e2ee/matrix_file_e2ee.dart';
2020-06-29 12:00:26 +00:00
import 'package:mime/mime.dart';
2020-03-16 10:38:03 +00:00
import '../../matrix_api/model/message_types.dart';
2019-10-18 11:05:07 +00:00
class MatrixFile {
Uint8List bytes;
2020-06-29 12:00:26 +00:00
String name;
String mimeType;
2019-10-18 11:05:07 +00:00
2020-06-29 12:00:26 +00:00
/// Encrypts this file and returns the
2020-03-23 09:37:51 +00:00
/// encryption information as an [EncryptedFile].
2020-03-16 10:38:03 +00:00
Future<EncryptedFile> encrypt() async {
2020-06-29 12:00:26 +00:00
return await encryptFile(bytes);
}
MatrixFile({this.bytes, this.name, this.mimeType}) {
mimeType ??= lookupMimeType(name, headerBytes: bytes);
name = name.split('/').last.toLowerCase();
2020-03-16 10:38:03 +00:00
}
2019-10-18 11:05:07 +00:00
int get size => bytes.length;
2020-06-29 12:00:26 +00:00
2020-08-01 13:04:03 +00:00
String get msgType {
if (mimeType.toLowerCase().startsWith('image/')) {
return MessageTypes.Image;
}
if (mimeType.toLowerCase().startsWith('video/')) {
return MessageTypes.Video;
}
if (mimeType.toLowerCase().startsWith('audio/')) {
return MessageTypes.Audio;
}
return MessageTypes.File;
}
2020-06-29 12:00:26 +00:00
Map<String, dynamic> get info => ({
'mimetype': mimeType,
'size': size,
});
}
class MatrixImageFile extends MatrixFile {
int width;
int height;
String blurhash;
MatrixImageFile(
{Uint8List bytes,
String name,
String mimeType,
this.width,
this.height,
this.blurhash})
: super(bytes: bytes, name: name, mimeType: mimeType);
@override
String get msgType => 'm.image';
@override
Map<String, dynamic> get info => ({
...super.info,
if (width != null) 'w': width,
if (height != null) 'h': height,
if (blurhash != null) 'xyz.amorgan.blurhash': blurhash,
});
}
class MatrixVideoFile extends MatrixFile {
int width;
int height;
int duration;
MatrixVideoFile(
{Uint8List bytes,
String name,
String mimeType,
this.width,
this.height,
this.duration})
: super(bytes: bytes, name: name, mimeType: mimeType);
@override
String get msgType => 'm.video';
@override
Map<String, dynamic> get info => ({
...super.info,
if (width != null) 'w': width,
if (height != null) 'h': height,
if (duration != null) 'duration': duration,
});
}
class MatrixAudioFile extends MatrixFile {
int duration;
MatrixAudioFile(
{Uint8List bytes, String name, String mimeType, this.duration})
: super(bytes: bytes, name: name, mimeType: mimeType);
@override
String get msgType => 'm.audio';
@override
Map<String, dynamic> get info => ({
...super.info,
if (duration != null) 'duration': duration,
});
}
extension ToMatrixFile on EncryptedFile {
MatrixFile toMatrixFile() {
return MatrixFile(
bytes: data, name: 'crypt', mimeType: 'application/octet-stream');
}
2019-10-18 11:05:07 +00:00
}