Make file read/write async

This commit is contained in:
fiaxh 2019-02-13 21:50:15 +01:00
parent d668e0158d
commit 736522737f
6 changed files with 16 additions and 17 deletions

View file

@ -44,7 +44,6 @@ public class FileTransfer : Object {
input_stream_ = value; input_stream_ = value;
} }
} }
public OutputStream output_stream { get; set; }
public string file_name { get; set; } public string file_name { get; set; }
private string? server_file_name_ = null; private string? server_file_name_ = null;

View file

@ -17,7 +17,7 @@ public class AvatarStorage : Xep.PixbufStorage, Object {
try { try {
if (file.query_exists()) file.delete(); //TODO y? if (file.query_exists()) file.delete(); //TODO y?
DataOutputStream fos = new DataOutputStream(file.create(FileCreateFlags.REPLACE_DESTINATION)); DataOutputStream fos = new DataOutputStream(file.create(FileCreateFlags.REPLACE_DESTINATION));
fos.write(data); fos.write_async.begin(data);
} catch (Error e) { } catch (Error e) {
// Ignore: we failed in storing, so we refuse to display later... // Ignore: we failed in storing, so we refuse to display later...
} }
@ -36,4 +36,4 @@ public class AvatarStorage : Xep.PixbufStorage, Object {
} }
} }
} }
} }

View file

@ -34,7 +34,7 @@ public class FileManager : StreamInteractionModule, Object {
DirUtils.create_with_parents(get_storage_dir(), 0700); 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(); FileTransfer file_transfer = new FileTransfer();
file_transfer.account = conversation.account; file_transfer.account = conversation.account;
file_transfer.counterpart = conversation.counterpart; 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.file_name = file_info.get_display_name();
file_transfer.mime_type = file_info.get_content_type(); file_transfer.mime_type = file_info.get_content_type();
file_transfer.size = (int)file_info.get_size(); 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) { } catch (Error e) {
file_transfer.state = FileTransfer.State.FAILED; file_transfer.state = FileTransfer.State.FAILED;
} }
save_file(file_transfer); yield save_file(file_transfer);
file_transfer.persist(db); 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 { try {
string filename = Random.next_int().to_string("%x") + "_" + file_transfer.file_name; 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)); File file = File.new_for_path(Path.build_filename(get_storage_dir(), filename));
OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION); 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(); os.close();
file_transfer.state = FileTransfer.State.COMPLETE; file_transfer.state = FileTransfer.State.COMPLETE;
file_transfer.path = filename; file_transfer.path = filename;
file_transfer.input_stream = file.read(); file_transfer.input_stream = yield file.read_async();
} catch (Error e) { } catch (Error e) {
file_transfer.state = FileTransfer.State.FAILED; file_transfer.state = FileTransfer.State.FAILED;
} }

View file

@ -54,7 +54,7 @@ public class View : Box {
// } // }
if (chooser.run() == Gtk.ResponseType.ACCEPT) { if (chooser.run() == Gtk.ResponseType.ACCEPT) {
string uri = chooser.get_filename(); 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);
} }
}); });

View file

@ -114,10 +114,10 @@ public class FileProvider : Dino.FileProvider, Object {
} }
OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION); 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(); os.close();
file_transfer.path = file.get_basename(); 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; file_transfer.state = FileTransfer.State.COMPLETE;
} catch (Error e) { } catch (Error e) {

View file

@ -106,14 +106,14 @@ public class FileProvider : Dino.FileProvider, Object {
var session = new Soup.Session(); var session = new Soup.Session();
Soup.Request request = session.request(url); 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; file_transfer.encryption = Encryption.OMEMO;
OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION); 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(); os.close();
file_transfer.path = file.get_basename(); 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; file_transfer.state = FileTransfer.State.COMPLETE;
} catch (Error e) { } 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 // Decode IV and key
MatchInfo match_info; MatchInfo match_info;
this.url_regex.match(url, 0, out match_info); this.url_regex.match(url, 0, out match_info);
@ -140,7 +140,7 @@ public class FileProvider : Dino.FileProvider, Object {
Array<uint8> data = new Array<uint8>(false, true, 0); Array<uint8> data = new Array<uint8>(false, true, 0);
size_t len = -1; size_t len = -1;
do { do {
len = input_stream.read(buf); len = yield input_stream.read_async(buf);
data.append_vals(buf, (uint) len); data.append_vals(buf, (uint) len);
} while(len > 0); } while(len > 0);