[fractal] api: Properly handle case when thumbnail can't be generated



commit 8d15778c6fa45d3ed0b839a9f90837b5d3de72b4
Author: Christopher Davis <brainblasted disroot org>
Date:   Thu Oct 31 11:16:32 2019 -0700

    api: Properly handle case when thumbnail can't be generated
    
    For some images - gifs in particular - thumbnails can't be
    generated by Fractal. In such cases `m.extra_content` would
    be `None`. In `attach_message()`, however, we unwrapped
    `extra_content` assuming it would never be `None`. This
    caused a crash when uploading gifs.
    
    This commit makes sure that we handle the case of missing
    `extra_content` by making `extra_content` in `attach_file()`
    an `Option<ExtraContent>`, then setting the thumbnail url
    appropriately.
    
    Incidentally, this commit also seems to fix an issue where images would show broken thumbnails after a 
user
    uploaded them.

 fractal-matrix-api/src/backend/room.rs   | 14 ++++++++++----
 fractal-matrix-api/src/model/fileinfo.rs |  4 ++--
 2 files changed, 12 insertions(+), 6 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs
index 6fdfc2cc..ff0e8dda 100644
--- a/fractal-matrix-api/src/backend/room.rs
+++ b/fractal-matrix-api/src/backend/room.rs
@@ -604,9 +604,15 @@ pub fn set_room_avatar(bk: &Backend, baseu: Url, roomid: &str, avatar: &str) ->
 
 pub fn attach_file(bk: &Backend, baseu: Url, mut msg: Message) -> Result<(), Error> {
     let fname = msg.url.clone().unwrap_or_default();
-    let mut extra_content: ExtraContent =
-        serde_json::from_value(msg.clone().extra_content.unwrap()).unwrap();
-    let thumb = extra_content.info.thumbnail_url.clone().unwrap_or_default();
+    let extra_content: Option<ExtraContent> = {
+        msg.clone()
+            .extra_content
+            .map_or(None, |c| Some(serde_json::from_value(c).unwrap()))
+    };
+
+    let thumb = extra_content
+        .clone()
+        .map_or(String::new(), |c| c.info.thumbnail_url.unwrap_or_default());
 
     let tx = bk.tx.clone();
     let itx = bk.internal_tx.clone();
@@ -625,7 +631,7 @@ pub fn attach_file(bk: &Backend, baseu: Url, mut msg: Message) -> Result<(), Err
                 }
                 Ok(thumb_uri) => {
                     msg.thumb = Some(thumb_uri.to_string());
-                    extra_content.info.thumbnail_url = Some(thumb_uri);
+                    extra_content.clone().unwrap().info.thumbnail_url = Some(thumb_uri);
                     msg.extra_content = Some(serde_json::to_value(&extra_content).unwrap());
                 }
             }
diff --git a/fractal-matrix-api/src/model/fileinfo.rs b/fractal-matrix-api/src/model/fileinfo.rs
index b69fef7c..ceb6bf85 100644
--- a/fractal-matrix-api/src/model/fileinfo.rs
+++ b/fractal-matrix-api/src/model/fileinfo.rs
@@ -1,7 +1,7 @@
 use serde::{Deserialize, Serialize};
 use serde_json::Value as JsonValue;
 
-#[derive(Debug, Serialize, Deserialize)]
+#[derive(Clone, Debug, Serialize, Deserialize)]
 pub struct Info {
     pub thumbnail_url: Option<String>,
     pub thumbnail_info: Option<JsonValue>,
@@ -12,7 +12,7 @@ pub struct Info {
     pub orientation: Option<i32>,
 }
 
-#[derive(Debug, Serialize, Deserialize)]
+#[derive(Clone, Debug, Serialize, Deserialize)]
 pub struct ExtraContent {
     pub info: Info,
 }


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