From b6799e59bb9cc2ea511b0f19aab88a83c244dda8 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Fri, 19 Apr 2019 21:42:40 +0200 Subject: Remove avatars with missmatch between supposed and actual sha1 hash on load, make loading async --- libdino/src/service/avatar_storage.vala | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'libdino/src/service/avatar_storage.vala') diff --git a/libdino/src/service/avatar_storage.vala b/libdino/src/service/avatar_storage.vala index b924ca27..a307fe66 100644 --- a/libdino/src/service/avatar_storage.vala +++ b/libdino/src/service/avatar_storage.vala @@ -28,9 +28,24 @@ public class AvatarStorage : Xep.PixbufStorage, Object { return file.query_exists(); } - public Pixbuf? get_image(string id) { + public async Pixbuf? get_image(string id) { try { - return new Pixbuf.from_file(Path.build_filename(folder, id)); + File file = File.new_for_path(Path.build_filename(folder, id)); + FileInputStream stream = yield file.read_async(); + + uint8 fbuf[100]; + size_t size; + + Checksum checksum = new Checksum (ChecksumType.SHA1); + while ((size = yield stream.read_async(fbuf)) > 0) { + checksum.update(fbuf, size); + } + + if (checksum.get_string() != id) { + FileUtils.remove(file.get_path()); + } + stream.seek(0, SeekType.SET); + return yield new Pixbuf.from_stream_async(stream); } catch (Error e) { return null; } -- cgit v1.3