Merge branch 'upload-fix-file' into 'master'

[Upload] Fix file type

See merge request famedly/famedlysdk!101
This commit is contained in:
Christian Pauly 2019-10-16 09:09:09 +00:00
commit 4bb5d4568e
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/EventUpdate.dart';
export 'package:famedlysdk/src/sync/UserUpdate.dart'; export 'package:famedlysdk/src/sync/UserUpdate.dart';
export 'package:famedlysdk/src/utils/ChatTime.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/utils/MxContent.dart';
export 'package:famedlysdk/src/AccountData.dart'; export 'package:famedlysdk/src/AccountData.dart';
export 'package:famedlysdk/src/Client.dart'; export 'package:famedlysdk/src/Client.dart';

View File

@ -23,12 +23,12 @@
import 'dart:async'; import 'dart:async';
import 'dart:core'; import 'dart:core';
import 'dart:io';
import 'package:famedlysdk/src/AccountData.dart'; import 'package:famedlysdk/src/AccountData.dart';
import 'package:famedlysdk/src/Presence.dart'; import 'package:famedlysdk/src/Presence.dart';
import 'package:famedlysdk/src/StoreAPI.dart'; import 'package:famedlysdk/src/StoreAPI.dart';
import 'package:famedlysdk/src/sync/UserUpdate.dart'; import 'package:famedlysdk/src/sync/UserUpdate.dart';
import 'package:famedlysdk/src/utils/MatrixFile.dart';
import 'Connection.dart'; import 'Connection.dart';
import 'Room.dart'; import 'Room.dart';
@ -342,7 +342,7 @@ class Client {
} }
/// Uploads a new user avatar for this user. Returns ErrorResponse if something went wrong. /// 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); final uploadResp = await connection.upload(file);
if (uploadResp is ErrorResponse) return uploadResp; if (uploadResp is ErrorResponse) return uploadResp;
final setAvatarResp = await connection.jsonRequest( final setAvatarResp = await connection.jsonRequest(

View File

@ -24,10 +24,10 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:core'; import 'dart:core';
import 'dart:io';
import 'package:famedlysdk/src/Room.dart'; import 'package:famedlysdk/src/Room.dart';
import 'package:famedlysdk/src/RoomList.dart'; import 'package:famedlysdk/src/RoomList.dart';
import 'package:famedlysdk/src/utils/MatrixFile.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:mime_type/mime_type.dart'; 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 /// Uploads a file with the name [fileName] as base64 encoded to the server
/// and returns the mxc url as a string or an [ErrorResponse]. /// 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; List<int> fileBytes;
if (client.homeserver != "https://fakeServer.notExisting") if (client.homeserver != "https://fakeServer.notExisting")
fileBytes = await file.readAsBytes(); fileBytes = await file.readAsBytes();

View File

@ -21,6 +21,8 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>. * along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/ */
import 'dart:io';
import 'package:famedlysdk/src/Client.dart'; import 'package:famedlysdk/src/Client.dart';
import 'package:famedlysdk/src/Event.dart'; import 'package:famedlysdk/src/Event.dart';
import 'package:famedlysdk/src/RoomAccountData.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/responses/ErrorResponse.dart';
import 'package:famedlysdk/src/sync/EventUpdate.dart'; import 'package:famedlysdk/src/sync/EventUpdate.dart';
import 'package:famedlysdk/src/utils/ChatTime.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart';
import 'package:famedlysdk/src/utils/MatrixFile.dart';
import 'package:famedlysdk/src/utils/MxContent.dart'; import 'package:famedlysdk/src/utils/MxContent.dart';
//import 'package:image/image.dart'; //import 'package:image/image.dart';
import 'package:mime_type/mime_type.dart'; import 'package:mime_type/mime_type.dart';
@ -250,13 +251,13 @@ class Room {
Future<String> sendTextEvent(String message, {String txid = null}) => Future<String> sendTextEvent(String message, {String txid = null}) =>
sendEvent({"msgtype": "m.text", "body": message}, txid: txid); 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 txid = null}) async {
String fileName = file.path.split("/").last; String fileName = file.path.split("/").last;
// Try to get the size of the file // Try to get the size of the file
int size; int size;
try { try {
size = file.bytes.length; size = file.readAsBytesSync().length;
} catch (e) { } catch (e) {
print("[UPLOAD] Could not get size. Reason: ${e.toString()}"); print("[UPLOAD] Could not get size. Reason: ${e.toString()}");
} }
@ -284,7 +285,7 @@ class Room {
return await sendEvent(content, txid: txid); 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 txid = null, int width, int height}) async {
String fileName = file.path.split("/").last; String fileName = file.path.split("/").last;
final dynamic uploadResp = await client.connection.upload(file); final dynamic uploadResp = await client.connection.upload(file);
@ -294,7 +295,7 @@ class Room {
"body": fileName, "body": fileName,
"url": uploadResp, "url": uploadResp,
"info": { "info": {
"size": file.bytes.length, "size": file.readAsBytesSync().length,
"mimetype": mime(fileName), "mimetype": mime(fileName),
"w": width, "w": width,
"h": height, "h": height,
@ -716,7 +717,7 @@ class Room {
/// Uploads a new user avatar for this room. Returns ErrorResponse if something went wrong /// Uploads a new user avatar for this room. Returns ErrorResponse if something went wrong
/// and the event ID otherwise. /// and the event ID otherwise.
Future<dynamic> setAvatar(MatrixFile file) async { Future<dynamic> setAvatar(File file) async {
final uploadResp = await client.connection.upload(file); final uploadResp = await client.connection.upload(file);
if (uploadResp is ErrorResponse) return uploadResp; if (uploadResp is ErrorResponse) return uploadResp;
final setAvatarResp = await client.connection.jsonRequest( 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:async';
import 'dart:io';
import 'package:famedlysdk/src/AccountData.dart'; import 'package:famedlysdk/src/AccountData.dart';
import 'package:famedlysdk/src/Client.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/EventUpdate.dart';
import 'package:famedlysdk/src/sync/RoomUpdate.dart'; import 'package:famedlysdk/src/sync/RoomUpdate.dart';
import 'package:famedlysdk/src/sync/UserUpdate.dart'; import 'package:famedlysdk/src/sync/UserUpdate.dart';
import 'package:famedlysdk/src/utils/MatrixFile.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'FakeMatrixApi.dart'; import 'FakeMatrixApi.dart';
@ -298,15 +298,14 @@ void main() {
}); });
test('upload', () async { test('upload', () async {
final MatrixFile testFile = final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
MatrixFile(bytes: [], path: "/root/file.jpeg");
final dynamic resp = await matrix.connection.upload(testFile); final dynamic resp = await matrix.connection.upload(testFile);
expect(resp, "mxc://example.com/AQwafuaFswefuhsfAFAgsw"); expect(resp, "mxc://example.com/AQwafuaFswefuhsfAFAgsw");
}); });
test('setAvatar', () async { test('setAvatar', () async {
final MatrixFile testFile = final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
MatrixFile(bytes: [], path: "/root/file.jpeg");
final dynamic resp = await matrix.setAvatar(testFile); final dynamic resp = await matrix.setAvatar(testFile);
expect(resp, null); expect(resp, null);
}); });

View File

@ -21,6 +21,8 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>. * along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/ */
import 'dart:io';
import 'package:famedlysdk/src/Client.dart'; import 'package:famedlysdk/src/Client.dart';
import 'package:famedlysdk/src/Event.dart'; import 'package:famedlysdk/src/Event.dart';
import 'package:famedlysdk/src/Room.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/Timeline.dart';
import 'package:famedlysdk/src/User.dart'; import 'package:famedlysdk/src/User.dart';
import 'package:famedlysdk/src/utils/ChatTime.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart';
import 'package:famedlysdk/src/utils/MatrixFile.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'FakeMatrixApi.dart'; import 'FakeMatrixApi.dart';
@ -258,8 +259,7 @@ void main() {
}); });
test('setAvatar', () async { test('setAvatar', () async {
final MatrixFile testFile = final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
MatrixFile(bytes: [], path: "/root/file.jpeg");
final dynamic resp = await room.setAvatar(testFile); final dynamic resp = await room.setAvatar(testFile);
expect(resp, "YUwRidLecu:example.com"); expect(resp, "YUwRidLecu:example.com");
}); });
@ -286,8 +286,7 @@ void main() {
});*/ });*/
test('sendFileEvent', () async { test('sendFileEvent', () async {
final MatrixFile testFile = final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
MatrixFile(bytes: [], path: "/root/file.jpeg");
final dynamic resp = final dynamic resp =
await room.sendFileEvent(testFile, "m.file", txid: "testtxid"); await room.sendFileEvent(testFile, "m.file", txid: "testtxid");
expect(resp, "42"); expect(resp, "42");