[fractal] dependencies: Remove tree_magic from fractal-gtk



commit 4d601d6472d90808cf3ba60f5de78c886881a381
Author: Christopher Davis <brainblasted disroot org>
Date:   Thu Feb 7 15:40:19 2019 -0500

    dependencies: Remove tree_magic from fractal-gtk
    
    Fractal crashes when switching to rooms with images due
    to an issue with tree_magic. We used tree_magic to sniff out
    the mime type, but we can use gio::FileInfo instead.
    
    Related to https://gitlab.gnome.org/GNOME/fractal/issues/37

 Cargo.lock                       |  1 -
 fractal-gtk/Cargo.toml           |  1 -
 fractal-gtk/src/appop/message.rs | 51 ++++++++++++++++++++++++----------------
 fractal-gtk/src/widgets/image.rs | 14 ++++++++---
 4 files changed, 42 insertions(+), 25 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 47ab1f55..ba63e5ea 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -496,7 +496,6 @@ dependencies = [
  "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_json 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
  "sourceview 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "tree_magic 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
diff --git a/fractal-gtk/Cargo.toml b/fractal-gtk/Cargo.toml
index cd2ff37b..7d3266c6 100644
--- a/fractal-gtk/Cargo.toml
+++ b/fractal-gtk/Cargo.toml
@@ -28,7 +28,6 @@ regex = "1.1.0"
 secret-service = "0.4.0"
 serde_json = "1.0.33"
 sourceview = "0.5.0"
-tree_magic = "0.2.1"
 url = "1.7.2"
 
 [dependencies.gst]
diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs
index 9ef59c7c..9a4aa8e0 100644
--- a/fractal-gtk/src/appop/message.rs
+++ b/fractal-gtk/src/appop/message.rs
@@ -1,11 +1,11 @@
 use comrak::{markdown_to_html, ComrakOptions};
+use gio::prelude::{FileExt, FileInfoExt};
 use gtk;
 use gtk::prelude::*;
 use lazy_static::lazy_static;
 use log::error;
 use std::fs;
 use std::path::PathBuf;
-use tree_magic;
 
 use crate::appop::room::Force;
 use crate::appop::AppOp;
@@ -233,26 +233,37 @@ impl AppOp {
     pub fn attach_message(&mut self, path: PathBuf) {
         if let Some(room) = self.active_room.clone() {
             if let Some(sender) = self.uid.clone() {
-                let mime = tree_magic::from_filepath(&path);
-                let mtype = match mime.as_ref() {
-                    "image/gif" => "m.image",
-                    "image/png" => "m.image",
-                    "image/jpeg" => "m.image",
-                    "image/jpg" => "m.image",
-                    _ => "m.file",
-                };
-                let body = path
-                    .file_name()
-                    .and_then(|s| s.to_str())
-                    .unwrap_or_default();
-                let path_string = path.to_str().unwrap_or_default();
-
-                let mut m = Message::new(room, sender, body.to_string(), mtype.to_string());
-                if mtype == "m.image" {
-                    m.extra_content = get_image_media_info(path_string, mime.as_ref());
+                if let Ok(info) = gio::File::new_for_path(&path).query_info(
+                    &gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+                    gio::FileQueryInfoFlags::NONE,
+                    None,
+                ) {
+                    // This should always return a type
+                    let mime = info
+                        .get_content_type()
+                        .expect("Could not parse content type from file");
+                    let mtype = match mime.as_ref() {
+                        "image/gif" => "m.image",
+                        "image/png" => "m.image",
+                        "image/jpeg" => "m.image",
+                        "image/jpg" => "m.image",
+                        _ => "m.file",
+                    };
+                    let body = path
+                        .file_name()
+                        .and_then(|s| s.to_str())
+                        .unwrap_or_default();
+                    let path_string = path.to_str().unwrap_or_default();
+
+                    let mut m = Message::new(room, sender, body.to_string(), mtype.to_string());
+                    if mtype == "m.image" {
+                        m.extra_content = get_image_media_info(path_string, mime.as_ref());
+                    }
+                    self.add_tmp_room_message(m);
+                    self.dequeue_message();
+                } else {
+                    error!("Can't send message: Could not query info");
                 }
-                self.add_tmp_room_message(m);
-                self.dequeue_message();
             } else {
                 error!("Can't send message: No user is logged in");
             }
diff --git a/fractal-gtk/src/widgets/image.rs b/fractal-gtk/src/widgets/image.rs
index 3ca01272..b0d18fbd 100644
--- a/fractal-gtk/src/widgets/image.rs
+++ b/fractal-gtk/src/widgets/image.rs
@@ -4,6 +4,7 @@ use gdk_pixbuf::Pixbuf;
 use gdk_pixbuf::PixbufAnimation;
 use gdk_pixbuf::PixbufAnimationExt;
 use gdk_pixbuf::PixbufExt;
+use gio::prelude::{FileExt, FileInfoExt};
 use glib;
 use gtk;
 use gtk::prelude::*;
@@ -12,7 +13,6 @@ use std::path::Path;
 use std::sync::mpsc::channel;
 use std::sync::mpsc::{Receiver, Sender};
 use std::sync::{Arc, Mutex};
-use tree_magic;
 
 use crate::backend::BKCommand;
 use std::sync::mpsc::TryRecvError;
@@ -373,8 +373,16 @@ pub fn is_gif(fname: &str) -> bool {
     if !p.is_file() {
         return false;
     }
-    let result = tree_magic::from_filepath(p);
-    result == "image/gif"
+
+    if let Ok(info) = gio::File::new_for_path(&p).query_info(
+        &gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+        gio::FileQueryInfoFlags::NONE,
+        None,
+    ) {
+        info.get_content_type() == Some("image/gif".to_string())
+    } else {
+        false
+    }
 }
 
 /// Adjust the `w` x `h` to `maxw` x `maxh` keeping the Aspect ratio


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]