[Upload] Fix file type

This commit is contained in:
Christian Pauly 2019-10-16 09:09:09 +00:00
parent 8bb35f7e88
commit 4ad22731a6
7 changed files with 19 additions and 29 deletions

View File

@ -30,7 +30,6 @@ 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';

View File

@ -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(MatrixFile file) async {
Future<dynamic> setAvatar(File file) async {
final uploadResp = await connection.upload(file);
if (uploadResp is ErrorResponse) return uploadResp;
final setAvatarResp = await connection.jsonRequest(

View File

@ -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';
@ -288,7 +288,7 @@ 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(MatrixFile file) async {
Future<dynamic> upload(File file) async {
List<int> fileBytes;
if (client.homeserver != "https://fakeServer.notExisting")
fileBytes = await file.readAsBytes();

View File

@ -21,6 +21,8 @@
* 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';
@ -28,7 +30,6 @@ 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';
@ -250,13 +251,13 @@ class Room {
Future<String> sendTextEvent(String message, {String txid = null}) =>
sendEvent({"msgtype": "m.text", "body": message}, txid: txid);
Future<String> sendFileEvent(MatrixFile file, String msgType,
Future<String> sendFileEvent(File 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.bytes.length;
size = file.readAsBytesSync().length;
} catch (e) {
print("[UPLOAD] Could not get size. Reason: ${e.toString()}");
}
@ -284,7 +285,7 @@ class Room {
return await sendEvent(content, txid: txid);
}
Future<String> sendImageEvent(MatrixFile file,
Future<String> sendImageEvent(File file,
{String txid = null, int width, int height}) async {
String fileName = file.path.split("/").last;
final dynamic uploadResp = await client.connection.upload(file);
@ -294,7 +295,7 @@ class Room {
"body": fileName,
"url": uploadResp,
"info": {
"size": file.bytes.length,
"size": file.readAsBytesSync().length,
"mimetype": mime(fileName),
"w": width,
"h": height,
@ -716,7 +717,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(MatrixFile file) async {
Future<dynamic> setAvatar(File file) async {
final uploadResp = await client.connection.upload(file);
if (uploadResp is ErrorResponse) return uploadResp;
final setAvatarResp = await client.connection.jsonRequest(

View File

@ -1,8 +0,0 @@
class MatrixFile {
List<int> bytes;
String path;
MatrixFile({this.bytes, this.path});
Future<List<int>> readAsBytes() async => bytes;
}

View File

@ -22,6 +22,7 @@
*/
import 'dart:async';
import 'dart:io';
import 'package:famedlysdk/src/AccountData.dart';
import 'package:famedlysdk/src/Client.dart';
@ -34,7 +35,6 @@ 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,15 +298,14 @@ void main() {
});
test('upload', () async {
final MatrixFile testFile =
MatrixFile(bytes: [], path: "/root/file.jpeg");
final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
final dynamic resp = await matrix.connection.upload(testFile);
expect(resp, "mxc://example.com/AQwafuaFswefuhsfAFAgsw");
});
test('setAvatar', () async {
final MatrixFile testFile =
MatrixFile(bytes: [], path: "/root/file.jpeg");
final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
final dynamic resp = await matrix.setAvatar(testFile);
expect(resp, null);
});

View File

@ -21,6 +21,8 @@
* 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';
@ -28,7 +30,6 @@ 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';
@ -258,8 +259,7 @@ void main() {
});
test('setAvatar', () async {
final MatrixFile testFile =
MatrixFile(bytes: [], path: "/root/file.jpeg");
final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
final dynamic resp = await room.setAvatar(testFile);
expect(resp, "YUwRidLecu:example.com");
});
@ -286,8 +286,7 @@ void main() {
});*/
test('sendFileEvent', () async {
final MatrixFile testFile =
MatrixFile(bytes: [], path: "/root/file.jpeg");
final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
final dynamic resp =
await room.sendFileEvent(testFile, "m.file", txid: "testtxid");
expect(resp, "42");