From 9ea16b6d8568cb383eb1f469d1dc54bfcad4f188 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Mon, 16 Oct 2017 00:23:51 +0200 Subject: PGP encrypted file transfers --- libdino/src/entity/file_transfer.vala | 9 +++++++ libdino/src/service/file_manager.vala | 45 ++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 8 deletions(-) (limited to 'libdino/src') diff --git a/libdino/src/entity/file_transfer.vala b/libdino/src/entity/file_transfer.vala index 10fe99f5..e26b723c 100644 --- a/libdino/src/entity/file_transfer.vala +++ b/libdino/src/entity/file_transfer.vala @@ -25,6 +25,11 @@ public class FileTransfer : Object { public OutputStream output_stream { get; set; } public string file_name { get; set; } + private string? server_file_name_ = null; + public string server_file_name { + get { return server_file_name_ ?? file_name; } + set { server_file_name_ = value; } + } public string path { get; set; } public string mime_type { get; set; } public int size { get; set; } @@ -90,6 +95,10 @@ public class FileTransfer : Object { notify.connect(on_update); } + public string get_uri() { + return Path.build_filename(Dino.get_storage_dir(), "files", path); + } + private void on_update(Object o, ParamSpec sp) { Qlite.UpdateBuilder update_builder = db.file_transfer.update().with(db.file_transfer.id, "=", id); switch (sp.name) { diff --git a/libdino/src/service/file_manager.vala b/libdino/src/service/file_manager.vala index 6517e9a5..92ca7506 100644 --- a/libdino/src/service/file_manager.vala +++ b/libdino/src/service/file_manager.vala @@ -16,6 +16,8 @@ public class FileManager : StreamInteractionModule, Object { private StreamInteractor stream_interactor; private Database db; private Gee.List file_senders = new ArrayList(); + private Gee.List incomming_processors = new ArrayList(); + private Gee.List outgoing_processors = new ArrayList(); public static void start(StreamInteractor stream_interactor, Database db) { FileManager m = new FileManager(stream_interactor, db); @@ -46,12 +48,19 @@ public class FileManager : StreamInteractionModule, Object { file_transfer.encryption = Encryption.NONE; file_transfer.file_name = file_info.get_display_name(); file_transfer.input_stream = file.read(); + file_transfer.mime_type = file_info.get_content_type(); file_transfer.size = (int)file_info.get_size(); save_file(file_transfer); file_transfer.persist(db); + foreach (OutgoingFileProcessor processor in outgoing_processors) { + if (processor.can_process(conversation, file_transfer)) { + processor.process(conversation, file_transfer); + } + } + foreach (FileSender file_sender in file_senders) { if (file_sender.can_send(conversation, file_transfer)) { file_sender.send_file(conversation, file_transfer); @@ -88,11 +97,7 @@ public class FileManager : StreamInteractionModule, Object { } public void add_provider(FileProvider file_provider) { - file_provider.file_incoming.connect((file_transfer) => { - save_file(file_transfer); - file_transfer.persist(db); - received_file(file_transfer); - }); + file_provider.file_incoming.connect(handle_incomming_file); } public void add_sender(FileSender file_sender) { @@ -102,6 +107,30 @@ public class FileManager : StreamInteractionModule, Object { }); } + public void add_incomming_processor(IncommingFileProcessor processor) { + incomming_processors.add(processor); + } + + public void add_outgoing_processor(OutgoingFileProcessor processor) { + outgoing_processors.add(processor); + } + + private void handle_incomming_file(FileTransfer file_transfer) { + foreach (IncommingFileProcessor processor in incomming_processors) { + if (processor.can_process(file_transfer)) { + processor.process(file_transfer); + } + } + save_file(file_transfer); + + File file = File.new_for_path(file_transfer.get_uri()); + FileInfo file_info = file.query_info("*", FileQueryInfoFlags.NONE); + file_transfer.mime_type = file_info.get_content_type(); + + file_transfer.persist(db); + received_file(file_transfer); + } + private void save_file(FileTransfer file_transfer) { 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)); @@ -132,12 +161,12 @@ public interface FileSender : Object { public interface IncommingFileProcessor : Object { public abstract bool can_process(FileTransfer file_transfer); - public abstract FileTransfer process(FileTransfer file_transfer); + public abstract void process(FileTransfer file_transfer); } public interface OutgoingFileProcessor : Object { - public abstract bool can_process(FileTransfer file_transfer); - public abstract FileTransfer process(FileTransfer file_transfer); + public abstract bool can_process(Conversation conversation, FileTransfer file_transfer); + public abstract void process(Conversation conversation, FileTransfer file_transfer); } } -- cgit v1.2.3-70-g09d2