From a8fd421e32db6bb830df4149ecf48fb0f56e86ad Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Wed, 11 Oct 2017 23:42:59 +0200 Subject: [PATCH] share, share-proxy: device file upload --- src/mconnect/share-proxy.vala | 2 +- src/mconnect/share.vala | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/mconnect/share-proxy.vala b/src/mconnect/share-proxy.vala index 3cd0c9e..998b408 100644 --- a/src/mconnect/share-proxy.vala +++ b/src/mconnect/share-proxy.vala @@ -41,7 +41,7 @@ class ShareHandlerProxy : Object, PacketHandlerInterfaceProxy { } public void share_file(string path) throws IOError { - + this.share_handler.share_file(this.device, path); } public void share_url(string url) throws IOError { diff --git a/src/mconnect/share.vala b/src/mconnect/share.vala index ca62f78..af5181b 100644 --- a/src/mconnect/share.vala +++ b/src/mconnect/share.vala @@ -131,6 +131,24 @@ class ShareHandler : Object, PacketHandlerInterface { builder.get_root().get_object()); } + private Packet make_file_share_packet(string filename, uint64 size, + uint16 port) { + + var builder = new Json.Builder(); + builder.begin_object(); + builder.set_member_name("filename"); + builder.add_string_value(filename); + builder.end_object(); + + var pkt = new Packet(SHARE, + builder.get_root().get_object()); + pkt.payload = Packet.Payload(){ + size=size, + port=port + }; + return pkt; + } + public void share_url(Device dev, string url) { debug("share url %s to device %s", url, dev.to_string()); @@ -141,6 +159,47 @@ class ShareHandler : Object, PacketHandlerInterface { debug("share text %s to device %s", text, dev.to_string()); dev.send(make_share_packet("text", text)); + } + + public void share_file(Device dev, string path) { + debug("share file %s to device %s", path, dev.to_string()); + + var file = File.new_for_path(path); + uint64 size = 0; + try { + var fi = file.query_info(FileAttribute.STANDARD_SIZE, + FileQueryInfoFlags.NONE); + size = fi.get_size(); + } catch (Error e) { + warning("failed to obtain file size: %s", e.message); + return; + } + debug("file size: %llu", size); + + if (size == 0) { + warning("trying to share empty file %s", path); + return; + } + + FileInputStream input; + try { + input = file.read(); + } catch (Error e) { + warning("failed to open source file at path %s: %s", + file.get_path(), e.message); + throw e; + } + + uint16 port; + var listener = Core.instance().transfer_manager.make_listener(out port); + debug("allocated listener on port %u", port); + + var t = new UploadTransfer(dev, listener, input, size); + + Core.instance().transfer_manager.push_job(t); + + t.start_async.begin(); + dev.send(make_file_share_packet(file.get_basename(), size, port)); } } \ No newline at end of file