Merge branch 'soru/auto-cache-media' into 'main'
feat: Automatically cache storable files on upload Closes #131 See merge request famedly/famedlysdk!568
This commit is contained in:
commit
2841d1636c
|
|
@ -19,6 +19,7 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:core';
|
import 'dart:core';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
|
|
@ -520,6 +521,20 @@ class Client extends MatrixApi {
|
||||||
return archiveList;
|
return archiveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Uploads a file and automatically caches it in the database, if it is small enough
|
||||||
|
/// and returns the mxc url as a string.
|
||||||
|
@override
|
||||||
|
Future<String> upload(Uint8List file, String fileName,
|
||||||
|
{String contentType}) async {
|
||||||
|
final mxc = await super.upload(file, fileName, contentType: contentType);
|
||||||
|
final storeable = database != null && file.length <= database.maxFileSize;
|
||||||
|
if (storeable) {
|
||||||
|
await database.storeFile(
|
||||||
|
mxc, file, DateTime.now().millisecondsSinceEpoch);
|
||||||
|
}
|
||||||
|
return mxc;
|
||||||
|
}
|
||||||
|
|
||||||
/// Uploads a new user avatar for this user.
|
/// Uploads a new user avatar for this user.
|
||||||
Future<void> setAvatar(MatrixFile file) async {
|
Future<void> setAvatar(MatrixFile file) async {
|
||||||
final uploadResp = await upload(file.bytes, file.name);
|
final uploadResp = await upload(file.bytes, file.name);
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ import 'package:test/test.dart';
|
||||||
|
|
||||||
import 'fake_matrix_api.dart';
|
import 'fake_matrix_api.dart';
|
||||||
import 'fake_database.dart';
|
import 'fake_database.dart';
|
||||||
|
import 'fake_client.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
Client matrix;
|
Client matrix;
|
||||||
|
|
@ -444,6 +445,13 @@ void main() {
|
||||||
await matrix.ignoreUser('@charley2:stupid.abc');
|
await matrix.ignoreUser('@charley2:stupid.abc');
|
||||||
await matrix.unignoreUser('@charley:stupid.abc');
|
await matrix.unignoreUser('@charley:stupid.abc');
|
||||||
});
|
});
|
||||||
|
test('upload', () async {
|
||||||
|
final client = await getClient();
|
||||||
|
final response = await client.upload(Uint8List(0), 'file.jpeg');
|
||||||
|
expect(response, 'mxc://example.com/AQwafuaFswefuhsfAFAgsw');
|
||||||
|
expect(await client.database.getFile(response) != null, true);
|
||||||
|
await client.dispose(closeDatabase: true);
|
||||||
|
});
|
||||||
|
|
||||||
test('dispose', () async {
|
test('dispose', () async {
|
||||||
await matrix.dispose(closeDatabase: true);
|
await matrix.dispose(closeDatabase: true);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue