From ba4fbab831b09bd66fca8136333db384b44ff6c1 Mon Sep 17 00:00:00 2001 From: Lucas Fryzek Date: Sun, 6 Oct 2024 00:39:55 +0100 Subject: chat: Add support for file upload --- src/chat.py | 18 ++++++++++++++++++ src/config.py | 3 ++- src/gtk/chat.ui | 18 ++++++++++++++++++ src/gtk/preferences.ui | 11 +++++++++++ src/meson.build | 3 ++- src/message.py | 2 +- src/preferences.py | 6 ++++++ src/relay/netfile.py | 25 +++++++++++++++++++++++++ 8 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/relay/netfile.py (limited to 'src') diff --git a/src/chat.py b/src/chat.py index 5e52347..bb498c3 100644 --- a/src/chat.py +++ b/src/chat.py @@ -24,6 +24,7 @@ from gi.repository import GObject from .message import WeegtkMessage from .color import Color from weegtk import config +from weegtk import netfile import json @@ -153,3 +154,20 @@ class WeegtkChat(Adw.Bin): entry_buffer.set_text("", 0) self.emit("buffer_input", self.data['full_name'], text) + def open_file_dialog(self, dialog, result, caller): + try: + file = dialog.open_finish(result) + except GLib.GError: + # gtk-dialog-error-quark: Dismissed by user + pass + else: + conf = config.read() + file_url = netfile.upload(file.get_path(), conf["upload"]["url"]) + entry_buffer = self.text_entry.get_buffer() + entry_buffer.insert_text(self.text_entry.get_position(), file_url, -1) + + @Gtk.Template.Callback() + def attach_file(self, *args): + dialog = Gtk.FileDialog() + dialog.set_title("Select file to upload") + dialog.open(self.get_root(), None, self.open_file_dialog, self) diff --git a/src/config.py b/src/config.py index ee9d611..ac52063 100644 --- a/src/config.py +++ b/src/config.py @@ -32,7 +32,7 @@ CONFIG_FILENAME = f"{CONFIG_DIR}/weegtk.conf" CONFIG_DEFAULT_RELAY_LINES = 50 -CONFIG_DEFAULT_SECTIONS = ('relay', 'look', 'color', 'ssh') +CONFIG_DEFAULT_SECTIONS = ('relay', 'look', 'color', 'ssh', 'upload') CONFIG_DEFAULT_OPTIONS = (('relay.hostname', ''), ('relay.port', ''), ('relay.ssl', 'off'), @@ -43,6 +43,7 @@ CONFIG_DEFAULT_OPTIONS = (('relay.hostname', ''), ('ssh.port', ''), ('ssh.username', ''), ('ssh.key', ''), + ('upload.url', ''), ('look.debug', 'off'), ('look.statusbar', 'off')) diff --git a/src/gtk/chat.ui b/src/gtk/chat.ui index 233c362..6959d45 100644 --- a/src/gtk/chat.ui +++ b/src/gtk/chat.ui @@ -51,6 +51,15 @@ 5 5 + + + mail-attachment + + + + fill @@ -62,6 +71,15 @@ + + + mail-forward + + + + diff --git a/src/gtk/preferences.ui b/src/gtk/preferences.ui index 5823d2f..9e4d9fc 100644 --- a/src/gtk/preferences.ui +++ b/src/gtk/preferences.ui @@ -77,6 +77,17 @@ + + + Upload + + + Upload URL + True + + + + diff --git a/src/meson.build b/src/meson.build index 3225adc..649a19e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -36,7 +36,8 @@ weegtk_sources = [ 'preferences.py', 'relay/network.py', 'relay/protocol.py', - 'relay/color.py' + 'relay/color.py', + 'relay/netfile.py' ] install_data(weegtk_sources, install_dir: moduledir) diff --git a/src/message.py b/src/message.py index 4e37609..27cf817 100644 --- a/src/message.py +++ b/src/message.py @@ -1,4 +1,4 @@ -# window.py +# message.py # # Copyright 2024 Lucas Fryzek # diff --git a/src/preferences.py b/src/preferences.py index d864012..1a87e29 100644 --- a/src/preferences.py +++ b/src/preferences.py @@ -39,6 +39,8 @@ class WeegtkPreferences(Adw.PreferencesDialog): ssh_key_path = "" + upload_url = Gtk.Template.Child() + def __init__(self, **kwargs): super().__init__(**kwargs) @@ -53,6 +55,8 @@ class WeegtkPreferences(Adw.PreferencesDialog): self.ssh_username.set_text(self.config["ssh"]["username"]) self.set_ssh_key(self.config["ssh"]["key"]) + self.upload_url.set_text(self.config["upload"]["url"]) + self.install_action("ssh_key.open", None, self.ssh_key_open) def read_preferences(self): @@ -65,6 +69,8 @@ class WeegtkPreferences(Adw.PreferencesDialog): self.config["ssh"]["username"] = self.ssh_username.get_text() self.config["ssh"]["key"] = self.ssh_key_path + self.config["upload"]["url"] = self.upload_url.get_text() + def set_ssh_key(self, key_path): self.ssh_key_path = key_path short_name = os.path.basename(key_path) diff --git a/src/relay/netfile.py b/src/relay/netfile.py new file mode 100644 index 0000000..321b810 --- /dev/null +++ b/src/relay/netfile.py @@ -0,0 +1,25 @@ +# netfile.py +# +# Copyright 2024 Lucas Fryzek +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# SPDX-License-Identifier: GPL-3.0-or-later +import requests + +def upload(filepath, upload_url): + files = {'file': open(filepath, 'rb')} + + r = requests.post(upload_url, files=files) + return r.text.strip() -- cgit