http-files: max-file-size handling

This commit is contained in:
fiaxh 2017-09-26 17:01:06 +02:00
parent 54a25fd926
commit 9d8e1e88ec
9 changed files with 26 additions and 18 deletions

View file

@ -1,7 +1,7 @@
![Dino](https://cdn.rawgit.com/fiaxh/3cb1391c5a94443098d004b4bf7c712c/raw/62f6a5e7de8402a0a89ffc73e8d1ed170054051c/dino-writing.svg)
=======
![screenshots](https://i.imgur.com/xIKPEFF.png)
![screenshots](https://i.imgur.com/1KqLqDV.png)
Build
-----

View file

@ -145,7 +145,7 @@ public class ConnectionManager {
private Core.XmppStream? connect_(Account account, string? resource = null) {
if (!connection_mutexes[account].trylock()) return null;
if (connections.has_key(account)) connections[account].stream.remove_modules();
if (connections.has_key(account)) connections[account].stream.detach_modules();
connection_errors.unset(account);
if (resource == null) resource = account.resourcepart;

View file

@ -38,8 +38,8 @@ public class ConversationTitlebarWidget : Button, Plugins.ConversationTitlebarWi
FileChooserNative chooser = new FileChooserNative (
"Select file", get_toplevel() as Window, FileChooserAction.OPEN,
"Select", "Cancel");
int? max_file_size = stream_interactor.get_module(Manager.IDENTITY).get_max_file_size(conversation.account);
if (max_file_size != null) {
long max_file_size = stream_interactor.get_module(Manager.IDENTITY).get_max_file_size(conversation.account);
if (max_file_size != -1) {
FileFilter filter = new FileFilter();
filter.add_custom(FileFilterFlags.URI, (filter_info) => {
File file = File.new_for_uri(filter_info.uri);

View file

@ -36,7 +36,8 @@ public class FileProvider : Plugins.FileProvider, Object {
public void check_message(Message message, Conversation conversation) {
if (ignore_once.remove(message.body)) return;
bool in_roster = stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(conversation.account, conversation.counterpart) != null;
Jid relevant_jid = stream_interactor.get_module(MucManager.IDENTITY).get_real_jid(message.from, conversation.account) ?? conversation.counterpart;
bool in_roster = stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(conversation.account, relevant_jid) != null;
if (message.direction == Message.DIRECTION_RECEIVED && !in_roster) return;
if (message.body.length < 5) return;
if (!url_regex.match(message.body)) return;
@ -57,11 +58,11 @@ public class FileProvider : Plugins.FileProvider, Object {
Soup.Request request = session.request (message.body);
FileTransfer file_transfer = new FileTransfer();
file_transfer.account = conversation.account;
file_transfer.counterpart = conversation.counterpart;
file_transfer.counterpart = message.counterpart;
file_transfer.ourpart = message.ourpart;
file_transfer.encryption = Encryption.NONE;
file_transfer.time = new DateTime.now_utc();
file_transfer.local_time = new DateTime.now_utc();
file_transfer.time = message.time;
file_transfer.local_time = message.local_time;
file_transfer.direction = message.direction;
file_transfer.input_stream = request.send();
file_transfer.file_name = message.body.substring(message.body.last_index_of("/") + 1);

View file

@ -13,7 +13,7 @@ public class Manager : StreamInteractionModule, Object {
public signal void uploaded(FileTransfer file_transfer, string url);
private StreamInteractor stream_interactor;
private HashMap<Account, int?> max_file_sizes = new HashMap<Account, int?>(Account.hash_func, Account.equals_func);
private HashMap<Account, long> max_file_sizes = new HashMap<Account, long>(Account.hash_func, Account.equals_func);
private Manager(StreamInteractor stream_interactor) {
this.stream_interactor = stream_interactor;
@ -60,7 +60,7 @@ public class Manager : StreamInteractionModule, Object {
}
}
public int? get_max_file_size(Account account) {
public long get_max_file_size(Account account) {
lock (max_file_sizes) {
return max_file_sizes[account];
}

View file

@ -10,7 +10,7 @@ private const string NS_URI_0 = "urn:xmpp:http:upload:0";
public class UploadStreamModule : XmppStreamModule {
public static Core.ModuleIdentity<UploadStreamModule> IDENTITY = new Core.ModuleIdentity<UploadStreamModule>(NS_URI, "0363_http_file_upload");
public signal void feature_available(XmppStream stream, int? max_file_size);
public signal void feature_available(XmppStream stream, long max_file_size);
public delegate void OnUploadOk(XmppStream stream, string url_down);
public delegate void OnError(XmppStream stream, string error);
@ -122,7 +122,7 @@ public class UploadStreamModule : XmppStreamModule {
}
if (ver_available || ver_0_available) {
int? max_file_size = extract_max_file_size(info_result);
long max_file_size = extract_max_file_size(info_result);
if (ver_0_available) {
stream.add_flag(new Flag(jid, NS_URI_0));
} else if (ver_available) {
@ -135,7 +135,7 @@ public class UploadStreamModule : XmppStreamModule {
return false;
}
private int? extract_max_file_size(Xep.ServiceDiscovery.InfoResult info_result) {
private long extract_max_file_size(Xep.ServiceDiscovery.InfoResult info_result) {
string? max_file_size_str = null;
StanzaNode x_node = info_result.iq.stanza.get_deep_subnode("http://jabber.org/protocol/disco#info:query", "jabber:x:data:x");
Gee.List<StanzaNode> field_nodes = x_node.get_subnodes("field", "jabber:x:data");
@ -147,8 +147,8 @@ public class UploadStreamModule : XmppStreamModule {
break;
}
}
if (max_file_size_str != null) return int.parse(max_file_size_str);
return null;
if (max_file_size_str != null) return long.parse(max_file_size_str);
return -1;
}
}

View file

@ -149,7 +149,7 @@ public class XmppStream {
return this;
}
public void remove_modules() {
public void detach_modules() {
foreach (XmppStreamModule module in modules) module.detach(this);
}

View file

@ -30,6 +30,11 @@ namespace Xmpp.Iq {
namespaceRegistrants[namespace].add(module);
}
public void unregister_from_namespace(string namespace, Handler module) {
ArrayList<Handler>? handlers = namespaceRegistrants[namespace];
if (handlers != null) handlers.remove(module);
}
public override void attach(XmppStream stream) {
stream.received_iq_stanza.connect(on_received_iq_stanza);
}

View file

@ -71,12 +71,14 @@ public class Module : XmppStreamModule, Iq.Handler {
public void on_iq_set(XmppStream stream, Iq.Stanza iq) { }
public override void attach(XmppStream stream) {
stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI_INFO, this);
stream.add_flag(new Flag());
stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI_INFO, this);
add_feature(stream, NS_URI_INFO);
}
public override void detach(XmppStream stream) { }
public override void detach(XmppStream stream) {
stream.get_module(Iq.Module.IDENTITY).unregister_from_namespace(NS_URI_INFO, this);
}
public static void require(XmppStream stream) {
if (stream.get_module(IDENTITY) == null) stream.add_module(new ServiceDiscovery.Module());