2017-08-02 15:29:55 +00:00
|
|
|
using Dino.Entities;
|
|
|
|
using Xmpp;
|
|
|
|
using Gee;
|
|
|
|
|
|
|
|
namespace Dino.Plugins.HttpFiles {
|
|
|
|
|
2017-10-14 17:34:30 +00:00
|
|
|
public class Manager : StreamInteractionModule, FileSender, Object {
|
2017-08-02 15:29:55 +00:00
|
|
|
public static ModuleIdentity<Manager> IDENTITY = new ModuleIdentity<Manager>("http_files");
|
|
|
|
public string id { get { return IDENTITY.id; } }
|
|
|
|
|
2017-08-29 22:03:37 +00:00
|
|
|
public signal void uploading(FileTransfer file_transfer);
|
|
|
|
public signal void uploaded(FileTransfer file_transfer, string url);
|
2017-08-02 15:29:55 +00:00
|
|
|
|
|
|
|
private StreamInteractor stream_interactor;
|
2017-10-22 15:24:16 +00:00
|
|
|
private Database db;
|
2017-09-26 15:01:06 +00:00
|
|
|
private HashMap<Account, long> max_file_sizes = new HashMap<Account, long>(Account.hash_func, Account.equals_func);
|
2017-10-22 15:24:16 +00:00
|
|
|
|
|
|
|
public static void start(StreamInteractor stream_interactor, Database db) {
|
|
|
|
Manager m = new Manager(stream_interactor, db);
|
|
|
|
stream_interactor.add_module(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Manager(StreamInteractor stream_interactor, Database db) {
|
2017-08-02 15:29:55 +00:00
|
|
|
this.stream_interactor = stream_interactor;
|
2017-10-22 15:24:16 +00:00
|
|
|
this.db = db;
|
2017-08-02 15:29:55 +00:00
|
|
|
|
2017-10-14 17:34:30 +00:00
|
|
|
stream_interactor.get_module(FileManager.IDENTITY).add_sender(this);
|
2017-08-02 15:29:55 +00:00
|
|
|
stream_interactor.stream_negotiated.connect(on_stream_negotiated);
|
2017-10-22 15:24:16 +00:00
|
|
|
stream_interactor.get_module(MessageProcessor.IDENTITY).build_message_stanza.connect(check_add_oob);
|
2017-08-02 15:29:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 17:34:30 +00:00
|
|
|
public void send_file(Conversation conversation, FileTransfer file_transfer) {
|
2018-01-12 20:03:09 +00:00
|
|
|
Xmpp.XmppStream? stream = stream_interactor.get_stream(file_transfer.account);
|
2017-08-02 15:29:55 +00:00
|
|
|
if (stream != null) {
|
2018-01-28 22:26:37 +00:00
|
|
|
stream_interactor.module_manager.get_module(file_transfer.account, UploadStreamModule.IDENTITY).upload(stream, file_transfer.input_stream, file_transfer.server_file_name, file_transfer.mime_type,
|
2017-08-02 15:29:55 +00:00
|
|
|
(stream, url_down) => {
|
2017-08-29 22:03:37 +00:00
|
|
|
uploaded(file_transfer, url_down);
|
2018-01-28 19:56:27 +00:00
|
|
|
file_transfer.info = url_down;
|
|
|
|
Entities.Message message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(url_down, conversation);
|
|
|
|
message.encryption = Encryption.NONE;
|
|
|
|
stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(message, conversation);
|
|
|
|
file_transfer.info = message.id.to_string();
|
2017-08-02 15:29:55 +00:00
|
|
|
},
|
2018-01-19 21:37:02 +00:00
|
|
|
(stream, error_str) => {
|
|
|
|
print(@"Failed getting upload url + $error_str\n");
|
2017-08-29 22:03:37 +00:00
|
|
|
file_transfer.state = FileTransfer.State.FAILED;
|
|
|
|
}
|
2017-08-02 15:29:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 17:34:30 +00:00
|
|
|
public bool can_send(Conversation conversation, FileTransfer file_transfer) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool is_upload_available(Conversation conversation) {
|
2017-08-07 19:44:22 +00:00
|
|
|
lock (max_file_sizes) {
|
2017-10-14 17:34:30 +00:00
|
|
|
return max_file_sizes.has_key(conversation.account);
|
2017-08-07 19:44:22 +00:00
|
|
|
}
|
2017-08-02 15:29:55 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 15:01:06 +00:00
|
|
|
public long get_max_file_size(Account account) {
|
2017-08-07 19:44:22 +00:00
|
|
|
lock (max_file_sizes) {
|
|
|
|
return max_file_sizes[account];
|
|
|
|
}
|
2017-08-02 15:29:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 20:03:09 +00:00
|
|
|
private void on_stream_negotiated(Account account, XmppStream stream) {
|
2017-08-02 15:29:55 +00:00
|
|
|
stream_interactor.module_manager.get_module(account, UploadStreamModule.IDENTITY).feature_available.connect((stream, max_file_size) => {
|
2017-08-07 19:44:22 +00:00
|
|
|
lock (max_file_sizes) {
|
|
|
|
max_file_sizes[account] = max_file_size;
|
|
|
|
}
|
2017-08-02 15:29:55 +00:00
|
|
|
upload_available(account);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:03:09 +00:00
|
|
|
private void check_add_oob(Entities.Message message, Xmpp.MessageStanza message_stanza, Conversation conversation) {
|
2017-10-22 15:24:16 +00:00
|
|
|
if (message_is_file(db, message)) {
|
|
|
|
Xep.OutOfBandData.add_url_to_message(message_stanza, message_stanza.body);
|
|
|
|
}
|
2017-08-02 15:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 17:34:30 +00:00
|
|
|
public class FileMessageFilterDisplay : Plugins.MessageDisplayProvider, Object {
|
|
|
|
public string id { get; set; default="file_message_filter"; }
|
|
|
|
public double priority { get; set; default=10; }
|
|
|
|
|
|
|
|
public Database db;
|
|
|
|
|
|
|
|
public FileMessageFilterDisplay(Dino.Database db) {
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool can_display(Entities.Message? message) {
|
2017-10-22 15:24:16 +00:00
|
|
|
return message_is_file(db, message);
|
2017-10-14 17:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Plugins.MetaConversationItem? get_item(Entities.Message message, Conversation conversation) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-22 15:24:16 +00:00
|
|
|
}
|
2017-10-14 17:34:30 +00:00
|
|
|
|
2017-10-22 15:24:16 +00:00
|
|
|
private bool message_is_file(Database db, Entities.Message message) {
|
2018-01-28 19:56:27 +00:00
|
|
|
Qlite.QueryBuilder builder = db.file_transfer.select().with(db.file_transfer.info, "=", message.id.to_string());
|
|
|
|
Qlite.QueryBuilder builder2 = db.file_transfer.select().with(db.file_transfer.info, "=", message.body);
|
|
|
|
return builder.count() > 0 || builder2.count() > 0;
|
2017-10-14 17:34:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 15:29:55 +00:00
|
|
|
}
|