diff --git a/gramps/gen/mime/_pythonmime.py b/gramps/gen/mime/_pythonmime.py index 979f3b86c..dc32e7881 100644 --- a/gramps/gen/mime/_pythonmime.py +++ b/gramps/gen/mime/_pythonmime.py @@ -33,6 +33,7 @@ _type_map = { 'image/png' : 'PNG image', 'application/pdf' : 'PDF document', 'text/rtf' : 'Rich Text File', + 'text/html' : 'Web page', } mimetypes.add_type('application/x-gramps','.grdb') diff --git a/gramps/gen/utils/file.py b/gramps/gen/utils/file.py index 52bcecf32..c9009a021 100644 --- a/gramps/gen/utils/file.py +++ b/gramps/gen/utils/file.py @@ -202,6 +202,8 @@ def media_path_full(db, filename): Given a database and a filename of a media, return the media filename is full form, eg 'graves/tomb.png' becomes '/home/me/genea/graves/tomb.png """ + if filename.startswith(("http://", "https://", "ftp://")): + return filename if os.path.isabs(filename): return filename mpath = media_path(db) diff --git a/gramps/gen/utils/thumbnails.py b/gramps/gen/utils/thumbnails.py index 3d3548f3a..a9ec82693 100644 --- a/gramps/gen/utils/thumbnails.py +++ b/gramps/gen/utils/thumbnails.py @@ -243,7 +243,10 @@ def get_thumbnail_path(src_file, mtype=None, rectangle=None, size=SIZE_NORMAL): """ filename = __build_thumb_path(src_file, rectangle, size) if not os.path.isfile(src_file): - return os.path.join(IMAGE_DIR, "image-missing.png") + if src_file.startswith(("http://", "https://", "ftp://")): + return os.path.join(IMAGE_DIR, "gramps-url.png") + else: + return os.path.join(IMAGE_DIR, "image-missing.png") else: if (not os.path.isfile(filename)) or ( os.path.getmtime(src_file) > os.path.getmtime(filename)): diff --git a/gramps/gui/editors/editmedia.py b/gramps/gui/editors/editmedia.py index f874f699f..055ba3944 100644 --- a/gramps/gui/editors/editmedia.py +++ b/gramps/gui/editors/editmedia.py @@ -27,6 +27,9 @@ # #------------------------------------------------------------------------- import os +from urllib.request import urlopen, Request +from urllib.error import HTTPError + #------------------------------------------------------------------------- # # GTK/Gnome modules @@ -307,18 +310,43 @@ class EditMedia(EditPrimary): return path = self.file_path.get_text() - full_path = media_path_full(self.db, path) - if os.path.isfile(full_path): - self.determine_mime() + if path.startswith(("http://", "https://", "ftp://")): + request = Request(path, headers={'User-Agent': 'Mozilla/5.0'}) + try: + response = urlopen(request) + status = response.status + reason = response.reason + except HTTPError as error: + status = error.code + reason = error.reason + + if status == 200: + mime_type = response.getheader('Content-Type', default='') + offset = mime_type.find(';') + if offset != -1: + mime_type = mime_type[:offset] + self.obj.set_mime_type(mime_type) + else: + msg1 = _("Invalid url!") + msg2 = _("The url '{url}' is not valid.\n" + "Error: {error}").format(url=path, error=reason) + ErrorDialog(msg1, msg2, parent=self.window) + self.ok_button.set_sensitive(True) + return else: - msg1 = _("There is no media matching the current path value!") - msg2 = _("You have attempted to use the path with " - "value '%(path)s'. This path does not exist!" - " Please enter a different path") % { - 'path' : path } - ErrorDialog(msg1, msg2, parent=self.window) - self.ok_button.set_sensitive(True) - return + + full_path = media_path_full(self.db, path) + if os.path.isfile(full_path): + self.determine_mime() + else: + msg1 = _("There is no media matching the current path value!") + msg2 = _("You have attempted to use the path with " + "value '%(path)s'. This path does not exist!" + " Please enter a different path") % { + 'path' : path } + ErrorDialog(msg1, msg2, parent=self.window) + self.ok_button.set_sensitive(True) + return self.obj.set_path(path) diff --git a/gramps/gui/utils.py b/gramps/gui/utils.py index c8cd36692..6c8605fa4 100644 --- a/gramps/gui/utils.py +++ b/gramps/gui/utils.py @@ -395,28 +395,27 @@ def open_file_with_default_application(path, uistate): :type file_path: string :return: nothing """ - - norm_path = os.path.normpath(path) - if not os.path.exists(norm_path): - display_error_dialog(0, _("File %s does not exist") % norm_path, - uistate) - return + if path.startswith(("http://", "https://", "ftp://")): + norm_path = path + else: + norm_path = os.path.normpath(path) + if not os.path.exists(norm_path): + display_error_dialog(0, _("File %s does not exist") % norm_path, + uistate) + return if win(): try: os.startfile(norm_path) except WindowsError as msg: - display_error_dialog(0, str(msg), - uistate) - - return - - if not norm_path.startswith('file://'): - norm_path = 'file://' + norm_path - try: - Gio.AppInfo.launch_default_for_uri(norm_path) - except GLib.Error as error: - display_error_dialog(0, str(error), uistate) + display_error_dialog(0, str(msg), uistate) + else: + if not norm_path.startswith(("http://", "https://", "ftp://", "file://")): + norm_path = 'file://' + norm_path + try: + Gio.AppInfo.launch_default_for_uri(norm_path) + except GLib.Error as error: + display_error_dialog(0, str(error), uistate) def process_pending_events(max_count=20): """