From d71da4afec7bcacf07c68f7c519cb9219979741f Mon Sep 17 00:00:00 2001 From: mairandomness Date: Mon, 18 Feb 2019 20:11:47 -0500 Subject: [PATCH] change thumb path to random file name in temp --- fractal-gtk/src/appop/message.rs | 13 +++++++++++-- fractal-matrix-api/src/backend/room.rs | 7 +++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs index 3591ec55..95f2ff18 100644 --- a/fractal-gtk/src/appop/message.rs +++ b/fractal-gtk/src/appop/message.rs @@ -8,8 +8,10 @@ use gtk; use gtk::prelude::*; use lazy_static::lazy_static; use log::error; +use rand::Rng; use serde_json::json; use serde_json::Value as JsonValue; +use std::env::temp_dir; use std::fs; use std::path::PathBuf; @@ -494,11 +496,18 @@ fn get_image_media_info(file: &str, mimetype: &str) -> Option { // make thumbnail max 800x600 let thumb = Pixbuf::new_from_file_at_scale(&file, 800, 600, true).ok()?; - thumb.savev("/tmp/fractal_thumb.png", "png", &[]).ok()?; + let mut rng = rand::thread_rng(); + let x: u64 = rng.gen_range(1, 9223372036854775807); + let thumb_path = format!( + "{}/fractal_{}.png", + temp_dir().to_str().unwrap_or_default(), + x.to_string() + ); + thumb.savev(&thumb_path, "png", &[]).ok()?; let info = json!({ "info": { - "thumbnail_url": "", + "thumbnail_url": thumb_path, "thumbnail_info": { "w": thumb.get_width(), "h": thumb.get_height(), diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs index 517b9548..de13b9b6 100644 --- a/fractal-matrix-api/src/backend/room.rs +++ b/fractal-matrix-api/src/backend/room.rs @@ -465,7 +465,9 @@ pub fn set_room_avatar(bk: &Backend, roomid: &str, avatar: &str) -> Result<(), E pub fn attach_file(bk: &Backend, mut msg: Message) -> Result<(), Error> { let fname = msg.url.clone().unwrap_or_default(); - let thumb = msg.thumb.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 tx = bk.tx.clone(); let itx = bk.internal_tx.clone(); @@ -484,9 +486,6 @@ pub fn attach_file(bk: &Backend, mut msg: Message) -> Result<(), Error> { } Ok(thumb_uri) => { msg.thumb = Some(thumb_uri.to_string()); - - let mut extra_content: ExtraContent = - serde_json::from_value(msg.extra_content.unwrap()).unwrap(); extra_content.info.thumbnail_url = Some(thumb_uri); msg.extra_content = Some(serde_json::to_value(&extra_content).unwrap()); }