[Files] Use MatrixFile
This commit is contained in:
parent
64d9fbad6c
commit
77f62f042d
|
@ -30,6 +30,7 @@ export 'package:famedlysdk/src/sync/RoomUpdate.dart';
|
|||
export 'package:famedlysdk/src/sync/EventUpdate.dart';
|
||||
export 'package:famedlysdk/src/sync/UserUpdate.dart';
|
||||
export 'package:famedlysdk/src/utils/ChatTime.dart';
|
||||
export 'package:famedlysdk/src/utils/MatrixFile.dart';
|
||||
export 'package:famedlysdk/src/utils/MxContent.dart';
|
||||
export 'package:famedlysdk/src/AccountData.dart';
|
||||
export 'package:famedlysdk/src/Client.dart';
|
||||
|
|
|
@ -23,12 +23,12 @@
|
|||
|
||||
import 'dart:async';
|
||||
import 'dart:core';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:famedlysdk/src/AccountData.dart';
|
||||
import 'package:famedlysdk/src/Presence.dart';
|
||||
import 'package:famedlysdk/src/StoreAPI.dart';
|
||||
import 'package:famedlysdk/src/sync/UserUpdate.dart';
|
||||
import 'package:famedlysdk/src/utils/MatrixFile.dart';
|
||||
|
||||
import 'Connection.dart';
|
||||
import 'Room.dart';
|
||||
|
@ -342,7 +342,7 @@ class Client {
|
|||
}
|
||||
|
||||
/// Uploads a new user avatar for this user. Returns ErrorResponse if something went wrong.
|
||||
Future<dynamic> setAvatar(File file) async {
|
||||
Future<dynamic> setAvatar(MatrixFile file) async {
|
||||
final uploadResp = await connection.upload(file);
|
||||
if (uploadResp is ErrorResponse) return uploadResp;
|
||||
final setAvatarResp = await connection.jsonRequest(
|
||||
|
|
|
@ -24,10 +24,10 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:core';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:famedlysdk/src/Room.dart';
|
||||
import 'package:famedlysdk/src/RoomList.dart';
|
||||
import 'package:famedlysdk/src/utils/MatrixFile.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:mime_type/mime_type.dart';
|
||||
|
||||
|
@ -217,7 +217,7 @@ class Connection {
|
|||
dynamic json;
|
||||
if (data is Map) data.removeWhere((k, v) => v == null);
|
||||
(!(data is String)) ? json = jsonEncode(data) : json = data;
|
||||
if (data is List<int>) json = data;
|
||||
if (data is List<int> || action.startsWith("/media/r0/upload")) json = data;
|
||||
|
||||
final url = "${client.homeserver}/_matrix${action}";
|
||||
|
||||
|
@ -288,10 +288,10 @@ class Connection {
|
|||
|
||||
/// Uploads a file with the name [fileName] as base64 encoded to the server
|
||||
/// and returns the mxc url as a string or an [ErrorResponse].
|
||||
Future<dynamic> upload(File file) async {
|
||||
List<int> fileBytes;
|
||||
Future<dynamic> upload(MatrixFile file) async {
|
||||
dynamic fileBytes;
|
||||
if (client.homeserver != "https://fakeServer.notExisting")
|
||||
fileBytes = await file.readAsBytes();
|
||||
fileBytes = file.bytes;
|
||||
String fileName = file.path.split("/").last;
|
||||
String mimeType = mime(file.path);
|
||||
print("[UPLOADING] $fileName, type: $mimeType, size: ${fileBytes?.length}");
|
||||
|
|
|
@ -21,8 +21,6 @@
|
|||
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:famedlysdk/src/Client.dart';
|
||||
import 'package:famedlysdk/src/Event.dart';
|
||||
import 'package:famedlysdk/src/RoomAccountData.dart';
|
||||
|
@ -30,6 +28,7 @@ import 'package:famedlysdk/src/RoomState.dart';
|
|||
import 'package:famedlysdk/src/responses/ErrorResponse.dart';
|
||||
import 'package:famedlysdk/src/sync/EventUpdate.dart';
|
||||
import 'package:famedlysdk/src/utils/ChatTime.dart';
|
||||
import 'package:famedlysdk/src/utils/MatrixFile.dart';
|
||||
import 'package:famedlysdk/src/utils/MxContent.dart';
|
||||
//import 'package:image/image.dart';
|
||||
import 'package:mime_type/mime_type.dart';
|
||||
|
@ -251,13 +250,13 @@ class Room {
|
|||
Future<String> sendTextEvent(String message, {String txid = null}) =>
|
||||
sendEvent({"msgtype": "m.text", "body": message}, txid: txid);
|
||||
|
||||
Future<String> sendFileEvent(File file, String msgType,
|
||||
Future<String> sendFileEvent(MatrixFile file, String msgType,
|
||||
{String txid = null}) async {
|
||||
String fileName = file.path.split("/").last;
|
||||
// Try to get the size of the file
|
||||
int size;
|
||||
try {
|
||||
size = file.readAsBytesSync().length;
|
||||
size = file.size;
|
||||
} catch (e) {
|
||||
print("[UPLOAD] Could not get size. Reason: ${e.toString()}");
|
||||
}
|
||||
|
@ -285,7 +284,7 @@ class Room {
|
|||
return await sendEvent(content, txid: txid);
|
||||
}
|
||||
|
||||
Future<String> sendImageEvent(File file,
|
||||
Future<String> sendImageEvent(MatrixFile file,
|
||||
{String txid = null, int width, int height}) async {
|
||||
String fileName = file.path.split("/").last;
|
||||
final dynamic uploadResp = await client.connection.upload(file);
|
||||
|
@ -295,7 +294,7 @@ class Room {
|
|||
"body": fileName,
|
||||
"url": uploadResp,
|
||||
"info": {
|
||||
"size": file.readAsBytesSync().length,
|
||||
"size": file.size,
|
||||
"mimetype": mime(fileName),
|
||||
"w": width,
|
||||
"h": height,
|
||||
|
@ -717,7 +716,7 @@ class Room {
|
|||
|
||||
/// Uploads a new user avatar for this room. Returns ErrorResponse if something went wrong
|
||||
/// and the event ID otherwise.
|
||||
Future<dynamic> setAvatar(File file) async {
|
||||
Future<dynamic> setAvatar(MatrixFile file) async {
|
||||
final uploadResp = await client.connection.upload(file);
|
||||
if (uploadResp is ErrorResponse) return uploadResp;
|
||||
final setAvatarResp = await client.connection.jsonRequest(
|
||||
|
|
7
lib/src/utils/MatrixFile.dart
Normal file
7
lib/src/utils/MatrixFile.dart
Normal file
|
@ -0,0 +1,7 @@
|
|||
class MatrixFile {
|
||||
List<int> bytes;
|
||||
String path;
|
||||
|
||||
MatrixFile({this.bytes, this.path});
|
||||
int get size => bytes.length;
|
||||
}
|
|
@ -22,7 +22,6 @@
|
|||
*/
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:famedlysdk/src/AccountData.dart';
|
||||
import 'package:famedlysdk/src/Client.dart';
|
||||
|
@ -35,6 +34,7 @@ import 'package:famedlysdk/src/responses/PushrulesResponse.dart';
|
|||
import 'package:famedlysdk/src/sync/EventUpdate.dart';
|
||||
import 'package:famedlysdk/src/sync/RoomUpdate.dart';
|
||||
import 'package:famedlysdk/src/sync/UserUpdate.dart';
|
||||
import 'package:famedlysdk/src/utils/MatrixFile.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'FakeMatrixApi.dart';
|
||||
|
@ -298,14 +298,16 @@ void main() {
|
|||
});
|
||||
|
||||
test('upload', () async {
|
||||
final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
|
||||
final MatrixFile testFile =
|
||||
MatrixFile(bytes: [], path: "fake/path/file.jpeg");
|
||||
|
||||
final dynamic resp = await matrix.connection.upload(testFile);
|
||||
expect(resp, "mxc://example.com/AQwafuaFswefuhsfAFAgsw");
|
||||
});
|
||||
|
||||
test('setAvatar', () async {
|
||||
final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
|
||||
final MatrixFile testFile =
|
||||
MatrixFile(bytes: [], path: "fake/path/file.jpeg");
|
||||
final dynamic resp = await matrix.setAvatar(testFile);
|
||||
expect(resp, null);
|
||||
});
|
||||
|
|
|
@ -21,8 +21,6 @@
|
|||
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:famedlysdk/src/Client.dart';
|
||||
import 'package:famedlysdk/src/Event.dart';
|
||||
import 'package:famedlysdk/src/Room.dart';
|
||||
|
@ -30,6 +28,7 @@ import 'package:famedlysdk/src/RoomState.dart';
|
|||
import 'package:famedlysdk/src/Timeline.dart';
|
||||
import 'package:famedlysdk/src/User.dart';
|
||||
import 'package:famedlysdk/src/utils/ChatTime.dart';
|
||||
import 'package:famedlysdk/src/utils/MatrixFile.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'FakeMatrixApi.dart';
|
||||
|
@ -259,7 +258,8 @@ void main() {
|
|||
});
|
||||
|
||||
test('setAvatar', () async {
|
||||
final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
|
||||
final MatrixFile testFile =
|
||||
MatrixFile(bytes: [], path: "fake/path/file.jpeg");
|
||||
final dynamic resp = await room.setAvatar(testFile);
|
||||
expect(resp, "YUwRidLecu:example.com");
|
||||
});
|
||||
|
@ -286,7 +286,8 @@ void main() {
|
|||
});*/
|
||||
|
||||
test('sendFileEvent', () async {
|
||||
final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
|
||||
final MatrixFile testFile =
|
||||
MatrixFile(bytes: [], path: "fake/path/file.jpeg");
|
||||
final dynamic resp =
|
||||
await room.sendFileEvent(testFile, "m.file", txid: "testtxid");
|
||||
expect(resp, "42");
|
||||
|
|
Loading…
Reference in a new issue