From 736522737f4dae39af126353abfe26903b2c82ea Mon Sep 17 00:00:00 2001 From: fiaxh Date: Wed, 13 Feb 2019 21:50:15 +0100 Subject: [PATCH] Make file read/write async --- libdino/src/entity/file_transfer.vala | 1 - libdino/src/service/avatar_storage.vala | 4 ++-- libdino/src/service/file_manager.vala | 12 ++++++------ main/src/ui/chat_input/view.vala | 2 +- plugins/http-files/src/file_provider.vala | 4 ++-- plugins/omemo/src/file_provider.vala | 10 +++++----- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/libdino/src/entity/file_transfer.vala b/libdino/src/entity/file_transfer.vala index 2c45aa08..6b1492d6 100644 --- a/libdino/src/entity/file_transfer.vala +++ b/libdino/src/entity/file_transfer.vala @@ -44,7 +44,6 @@ public class FileTransfer : Object { input_stream_ = value; } } - public OutputStream output_stream { get; set; } public string file_name { get; set; } private string? server_file_name_ = null; diff --git a/libdino/src/service/avatar_storage.vala b/libdino/src/service/avatar_storage.vala index 46b43d99..f40414d1 100644 --- a/libdino/src/service/avatar_storage.vala +++ b/libdino/src/service/avatar_storage.vala @@ -17,7 +17,7 @@ public class AvatarStorage : Xep.PixbufStorage, Object { try { if (file.query_exists()) file.delete(); //TODO y? DataOutputStream fos = new DataOutputStream(file.create(FileCreateFlags.REPLACE_DESTINATION)); - fos.write(data); + fos.write_async.begin(data); } catch (Error e) { // Ignore: we failed in storing, so we refuse to display later... } @@ -36,4 +36,4 @@ public class AvatarStorage : Xep.PixbufStorage, Object { } } } -} \ No newline at end of file +} diff --git a/libdino/src/service/file_manager.vala b/libdino/src/service/file_manager.vala index 241bcebf..4763d721 100644 --- a/libdino/src/service/file_manager.vala +++ b/libdino/src/service/file_manager.vala @@ -34,7 +34,7 @@ public class FileManager : StreamInteractionModule, Object { DirUtils.create_with_parents(get_storage_dir(), 0700); } - public void send_file(string uri, Conversation conversation) { + public async void send_file(string uri, Conversation conversation) { FileTransfer file_transfer = new FileTransfer(); file_transfer.account = conversation.account; file_transfer.counterpart = conversation.counterpart; @@ -49,11 +49,11 @@ public class FileManager : StreamInteractionModule, Object { file_transfer.file_name = file_info.get_display_name(); file_transfer.mime_type = file_info.get_content_type(); file_transfer.size = (int)file_info.get_size(); - file_transfer.input_stream = file.read(); + file_transfer.input_stream = yield file.read_async(); } catch (Error e) { file_transfer.state = FileTransfer.State.FAILED; } - save_file(file_transfer); + yield save_file(file_transfer); file_transfer.persist(db); @@ -162,16 +162,16 @@ public class FileManager : StreamInteractionModule, Object { } } - private void save_file(FileTransfer file_transfer) { + private async void save_file(FileTransfer file_transfer) { try { string filename = Random.next_int().to_string("%x") + "_" + file_transfer.file_name; File file = File.new_for_path(Path.build_filename(get_storage_dir(), filename)); OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION); - os.splice(file_transfer.input_stream, 0); + yield os.splice_async(file_transfer.input_stream, 0); os.close(); file_transfer.state = FileTransfer.State.COMPLETE; file_transfer.path = filename; - file_transfer.input_stream = file.read(); + file_transfer.input_stream = yield file.read_async(); } catch (Error e) { file_transfer.state = FileTransfer.State.FAILED; } diff --git a/main/src/ui/chat_input/view.vala b/main/src/ui/chat_input/view.vala index dd111997..5e1ccb71 100644 --- a/main/src/ui/chat_input/view.vala +++ b/main/src/ui/chat_input/view.vala @@ -54,7 +54,7 @@ public class View : Box { // } if (chooser.run() == Gtk.ResponseType.ACCEPT) { string uri = chooser.get_filename(); - stream_interactor.get_module(FileManager.IDENTITY).send_file(uri, conversation); + stream_interactor.get_module(FileManager.IDENTITY).send_file.begin(uri, conversation); } }); diff --git a/plugins/http-files/src/file_provider.vala b/plugins/http-files/src/file_provider.vala index 553068cc..80f99fa6 100644 --- a/plugins/http-files/src/file_provider.vala +++ b/plugins/http-files/src/file_provider.vala @@ -114,10 +114,10 @@ public class FileProvider : Dino.FileProvider, Object { } OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION); - os.splice(file_transfer.input_stream, 0); + yield os.splice_async(file_transfer.input_stream, 0); os.close(); file_transfer.path = file.get_basename(); - file_transfer.input_stream = file.read(); + file_transfer.input_stream = yield file.read_async(); file_transfer.state = FileTransfer.State.COMPLETE; } catch (Error e) { diff --git a/plugins/omemo/src/file_provider.vala b/plugins/omemo/src/file_provider.vala index 49b42e92..bda73bfb 100644 --- a/plugins/omemo/src/file_provider.vala +++ b/plugins/omemo/src/file_provider.vala @@ -106,14 +106,14 @@ public class FileProvider : Dino.FileProvider, Object { var session = new Soup.Session(); Soup.Request request = session.request(url); - file_transfer.input_stream = decrypt_file(yield request.send_async(null), url_body); + file_transfer.input_stream = yield decrypt_file(yield request.send_async(null), url_body); file_transfer.encryption = Encryption.OMEMO; OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION); - os.splice(file_transfer.input_stream, 0); + yield os.splice_async(file_transfer.input_stream, 0); os.close(); file_transfer.path = file.get_basename(); - file_transfer.input_stream = file.read(); + file_transfer.input_stream = yield file.read_async(); file_transfer.state = FileTransfer.State.COMPLETE; } catch (Error e) { @@ -121,7 +121,7 @@ public class FileProvider : Dino.FileProvider, Object { } } - public InputStream? decrypt_file(InputStream input_stream, string url) { + public async InputStream? decrypt_file(InputStream input_stream, string url) { // Decode IV and key MatchInfo match_info; this.url_regex.match(url, 0, out match_info); @@ -140,7 +140,7 @@ public class FileProvider : Dino.FileProvider, Object { Array data = new Array(false, true, 0); size_t len = -1; do { - len = input_stream.read(buf); + len = yield input_stream.read_async(buf); data.append_vals(buf, (uint) len); } while(len > 0);