diff options
author | fiaxh <git@lightrise.org> | 2019-07-18 02:03:42 +0200 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2019-07-18 02:03:42 +0200 |
commit | f0abb8aaf9d06106235ca5e0e6b3ca2e425c4422 (patch) | |
tree | 03fce63f8c37b50b5202690a6801234724ed0021 /plugins/http-files/src/file_provider.vala | |
parent | 30353e92d63c033857ad0396c921b3bf5cab44c3 (diff) | |
download | dino-f0abb8aaf9d06106235ca5e0e6b3ca2e425c4422.tar.gz dino-f0abb8aaf9d06106235ca5e0e6b3ca2e425c4422.zip |
Refactor file receive/send interfaces and UI
Diffstat (limited to 'plugins/http-files/src/file_provider.vala')
-rw-r--r-- | plugins/http-files/src/file_provider.vala | 134 |
1 files changed, 78 insertions, 56 deletions
diff --git a/plugins/http-files/src/file_provider.vala b/plugins/http-files/src/file_provider.vala index b7d18e36..38687863 100644 --- a/plugins/http-files/src/file_provider.vala +++ b/plugins/http-files/src/file_provider.vala @@ -7,16 +7,15 @@ using Xmpp; namespace Dino.Plugins.HttpFiles { public class FileProvider : Dino.FileProvider, Object { - public string id { get { return "http"; } } private StreamInteractor stream_interactor; private Dino.Database dino_db; - private Regex url_regex; + private Regex url_regex = /^(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))$/; + private Regex omemo_url_regex = /^aesgcm:\/\/(.*)#(([A-Fa-f0-9]{2}){48}|([A-Fa-f0-9]{2}){44})$/; public FileProvider(StreamInteractor stream_interactor, Dino.Database dino_db) { this.stream_interactor = stream_interactor; this.dino_db = dino_db; - this.url_regex = /^(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))$/; stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this)); } @@ -38,7 +37,11 @@ public class FileProvider : Dino.FileProvider, Object { public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) { if (outer.url_regex.match(message.body)) { string? oob_url = Xmpp.Xep.OutOfBandData.get_url_from_message(stanza); - if (oob_url != null && oob_url == message.body) { + + bool normal_file = oob_url != null && oob_url == message.body; + bool omemo_file = outer.omemo_url_regex.match(message.body); + + if (normal_file || omemo_file) { yield outer.on_file_message(message, conversation); } } @@ -47,83 +50,102 @@ public class FileProvider : Dino.FileProvider, Object { } private async void on_file_message(Entities.Message message, Conversation conversation) { - FileTransfer file_transfer = new FileTransfer(); - file_transfer.account = conversation.account; - file_transfer.counterpart = message.counterpart; - file_transfer.ourpart = message.ourpart; - file_transfer.encryption = Encryption.NONE; - file_transfer.time = message.time; - file_transfer.local_time = message.local_time; - file_transfer.direction = message.direction; - file_transfer.file_name = message.body.substring(message.body.last_index_of("/") + 1); - file_transfer.size = -1; - file_transfer.state = FileTransfer.State.NOT_STARTED; - file_transfer.provider = 0; - file_transfer.info = message.id.to_string(); - - if (stream_interactor.get_module(FileManager.IDENTITY).is_sender_trustworthy(file_transfer, conversation)) { - yield get_meta_info(file_transfer); - if (file_transfer.size >= 0 && file_transfer.size < 5000000) { - ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, message.id); - if (content_item != null) { - stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true); - } - } - file_incoming(file_transfer, conversation); + // Hide message + ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, message.id); + if (content_item != null) { + stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true); } + + var additional_info = message.id.to_string(); + + var receive_data = new HttpFileReceiveData(); + receive_data.url = message.body; + + var file_meta = new HttpFileMeta(); + file_meta.file_name = Uri.unescape_string(message.body.substring(message.body.last_index_of("/") + 1)); + file_meta.message = message; + + file_incoming(additional_info, message.from, message.time, message.local_time, conversation, receive_data, file_meta); } - public async void get_meta_info(FileTransfer file_transfer) { - string url_body = dino_db.message.select({dino_db.message.body}).with(dino_db.message.id, "=", int.parse(file_transfer.info))[dino_db.message.body]; + public async FileMeta get_meta_info(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) throws FileReceiveError { + HttpFileReceiveData? http_receive_data = receive_data as HttpFileReceiveData; + if (http_receive_data == null) return file_meta; + var session = new Soup.Session(); - var head_message = new Soup.Message("HEAD", url_body); + var head_message = new Soup.Message("HEAD", http_receive_data.url); + if (head_message != null) { - yield session.send_async(head_message, null); + try { + yield session.send_async(head_message, null); + } catch (Error e) { + throw new FileReceiveError.GET_METADATA_FAILED("HEAD request failed"); + } string? content_type = null, content_length = null; - print(url_body + ":\n"); head_message.response_headers.foreach((name, val) => { - print(name + " " + val + "\n"); if (name == "Content-Type") content_type = val; if (name == "Content-Length") content_length = val; }); - file_transfer.mime_type = content_type; + file_meta.mime_type = content_type; if (content_length != null) { - file_transfer.size = int.parse(content_length); + file_meta.size = int.parse(content_length); } } + + return file_meta; } - public async void download(FileTransfer file_transfer, File file_) { + public async InputStream download(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) throws FileReceiveError { + HttpFileReceiveData? http_receive_data = receive_data as HttpFileReceiveData; + if (http_receive_data == null) assert(false); + try { - File file = file_; - string url_body = dino_db.message.select({dino_db.message.body}).with(dino_db.message.id, "=", int.parse(file_transfer.info))[dino_db.message.body]; var session = new Soup.Session(); - Soup.Request request = session.request(url_body); + Soup.Request request = session.request(http_receive_data.url); - file_transfer.input_stream = yield request.send_async(null); + return yield request.send_async(null); + } catch (Error e) { + throw new FileReceiveError.DOWNLOAD_FAILED("Downloading file error: %s".printf(e.message)); + } + } - foreach (IncomingFileProcessor processor in stream_interactor.get_module(FileManager.IDENTITY).incoming_processors) { - if (processor.can_process(file_transfer)) { - processor.process(file_transfer); - } - } + public FileMeta get_file_meta(FileTransfer file_transfer) throws FileReceiveError { + Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(file_transfer.counterpart.bare_jid, file_transfer.account); + if (conversation == null) throw new FileReceiveError.GET_METADATA_FAILED("No conversation"); - if (file_transfer.encryption == Encryption.PGP || file.get_path().has_suffix(".pgp")) { - file = File.new_for_path(file.get_path().substring(0, file.get_path().length - 4)); - } + Message? message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_id(int.parse(file_transfer.info), conversation); + if (message == null) throw new FileReceiveError.GET_METADATA_FAILED("No message"); - OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION); - yield os.splice_async(file_transfer.input_stream, 0); - os.close(); - file_transfer.path = file.get_basename(); - file_transfer.input_stream = yield file.read_async(); + var file_meta = new HttpFileMeta(); + file_meta.size = file_transfer.size; + file_meta.mime_type = file_transfer.mime_type; - file_transfer.state = FileTransfer.State.COMPLETE; - } catch (Error e) { - file_transfer.state = FileTransfer.State.FAILED; + // Extract file name from URL + file_meta.file_name = Uri.unescape_string(message.body.substring(message.body.last_index_of("/") + 1)); + if (file_meta.file_name.contains("#")) { + file_meta.file_name = file_meta.file_name.substring(0, file_meta.file_name.last_index_of("#")); } + + file_meta.message = message; + + return file_meta; } + + public FileReceiveData? get_file_receive_data(FileTransfer file_transfer) { + Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(file_transfer.counterpart.bare_jid, file_transfer.account); + if (conversation == null) return null; + + Message? message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_id(int.parse(file_transfer.info), conversation); + if (message == null) return null; + + var receive_data = new HttpFileReceiveData(); + receive_data.url = message.body; + + return receive_data; + } + + public int get_id() { return 0; } } } |