[fractal] create upload_thumbnail
- From: Christopher Davis <christopherdavis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] create upload_thumbnail
- Date: Sun, 24 Feb 2019 13:33:42 +0000 (UTC)
commit a384c194722416551391c8a1694ac3b5ab814bea
Author: mairandomness <mairakodama gmail com>
Date: Sun Jan 27 11:47:38 2019 -0500
create upload_thumbnail
change get_image_media_info to include thumbnail
propagate need for backend
try to debug the code
remove a println I forgot
Progress on using channel for thumb uploads
move thumbnail code to the backend
fix mimetype on json and only call ge_image_media_info on image files
remove random 'e' from util.rs
move get_image_media_info back to the frontend
create Info struct
hehe
lol
fractal-gtk/src/appop/message.rs | 39 ++++++++++----------
fractal-matrix-api/src/backend/room.rs | 62 ++++++++++++++++++++++----------
fractal-matrix-api/src/model/fileinfo.rs | 18 ++++++++++
fractal-matrix-api/src/model/mod.rs | 1 +
fractal-matrix-api/src/types.rs | 2 ++
5 files changed, 83 insertions(+), 39 deletions(-)
---
diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs
index 2f8cd850..3591ec55 100644
--- a/fractal-gtk/src/appop/message.rs
+++ b/fractal-gtk/src/appop/message.rs
@@ -1,5 +1,6 @@
use comrak::{markdown_to_html, ComrakOptions};
use gdk_pixbuf::Pixbuf;
+use gdk_pixbuf::PixbufExt;
use gio::prelude::{FileExt, FileInfoExt};
use gstreamer_editing_services::prelude::*;
use gstreamer_editing_services::UriClipAsset;
@@ -7,6 +8,8 @@ use gtk;
use gtk::prelude::*;
use lazy_static::lazy_static;
use log::error;
+use serde_json::json;
+use serde_json::Value as JsonValue;
use std::fs;
use std::path::PathBuf;
@@ -21,9 +24,6 @@ use crate::widgets;
use crate::types::Message;
-use serde_json::json;
-use serde_json::Value as JsonValue;
-
pub struct TmpMsg {
pub msg: Message,
pub widget: Option<gtk::Widget>,
@@ -485,31 +485,30 @@ fn create_ui_message(
}
}
-/// This function open the image and fill the info data as a Json value
-/// If something fails this will returns None
-///
-/// The output json will look like:
-///
-/// {
-/// "info": {
-/// "h": 296,
-/// "w": 296,
-/// "size": 8796,
-/// "orientation": 0,
-/// "mimetype": "image/png"
-/// }
-/// }
+/// This function opens the image, creates a thumbnail
+/// and populates the info Json with the information it has
+
fn get_image_media_info(file: &str, mimetype: &str) -> Option<JsonValue> {
- let (_, w, h) = Pixbuf::get_file_info(file)?;
- let size = fs::metadata(file).ok()?.len();
+ let (_, w, h) = Pixbuf::get_file_info(&file)?;
+ let size = fs::metadata(&file).ok()?.len();
+
+ // 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 info = json!({
"info": {
+ "thumbnail_url": "",
+ "thumbnail_info": {
+ "w": thumb.get_width(),
+ "h": thumb.get_height(),
+ "mimetype": "image/png"
+ },
"w": w,
"h": h,
"size": size,
"mimetype": mimetype,
- "orientation": 0,
+ "orientation": 0
}
});
diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs
index 8699e7bd..517b9548 100644
--- a/fractal-matrix-api/src/backend/room.rs
+++ b/fractal-matrix-api/src/backend/room.rs
@@ -22,6 +22,7 @@ use crate::backend::types::BKResponse;
use crate::backend::types::Backend;
use crate::backend::types::RoomType;
+use crate::types::ExtraContent;
use crate::types::Member;
use crate::types::Message;
use crate::types::RoomEventFilter;
@@ -462,37 +463,46 @@ pub fn set_room_avatar(bk: &Backend, roomid: &str, avatar: &str) -> Result<(), E
Ok(())
}
-pub fn attach_file(bk: &Backend, msg: Message) -> Result<(), Error> {
+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();
- if fname.starts_with("mxc://") {
+ let tx = bk.tx.clone();
+ let itx = bk.internal_tx.clone();
+ let baseu = bk.get_base_url().clone();
+ let tk = bk.data.lock().unwrap().access_token.clone();
+
+ if fname.starts_with("mxc://") && thumb.starts_with("mxc://") {
return send_msg(bk, msg);
}
- let mut file = File::open(&fname)?;
- let mut contents: Vec<u8> = vec![];
- file.read_to_end(&mut contents)?;
+ thread::spawn(move || {
+ if thumb != "" {
+ match upload_file(&tk, &baseu, &thumb) {
+ Err(err) => {
+ tx.send(BKResponse::AttachFileError(err)).unwrap();
+ }
+ Ok(thumb_uri) => {
+ msg.thumb = Some(thumb_uri.to_string());
- let baseu = bk.get_base_url();
- let tk = bk.data.lock().unwrap().access_token.clone();
- let params = &[("access_token", tk.clone())];
- let mediaurl = media_url(&baseu, "upload", params)?;
+ 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());
+ }
+ }
+ }
- let mut m = msg.clone();
- let tx = bk.tx.clone();
- let itx = bk.internal_tx.clone();
- thread::spawn(move || {
- match put_media(mediaurl.as_str(), contents) {
+ match upload_file(&tk, &baseu, &fname) {
Err(err) => {
tx.send(BKResponse::AttachFileError(err)).unwrap();
}
- Ok(js) => {
- let uri = js["content_uri"].as_str().unwrap_or_default();
- m.url = Some(uri.to_string());
+ Ok(uri) => {
+ msg.url = Some(uri.to_string());
if let Some(t) = itx {
- t.send(BKCommand::SendMsg(m.clone())).unwrap();
+ t.send(BKCommand::SendMsg(msg.clone())).unwrap();
}
- tx.send(BKResponse::AttachedFile(m)).unwrap();
+ tx.send(BKResponse::AttachedFile(msg)).unwrap();
}
};
});
@@ -500,6 +510,20 @@ pub fn attach_file(bk: &Backend, msg: Message) -> Result<(), Error> {
Ok(())
}
+fn upload_file(tk: &str, baseu: &Url, fname: &str) -> Result<String, Error> {
+ let mut file = File::open(fname)?;
+ let mut contents: Vec<u8> = vec![];
+ file.read_to_end(&mut contents)?;
+
+ let params = &[("access_token", tk.to_string())];
+ let mediaurl = media_url(&baseu, "upload", params)?;
+
+ match put_media(mediaurl.as_str(), contents) {
+ Err(err) => Err(err),
+ Ok(js) => Ok(js["content_uri"].as_str().unwrap_or_default().to_string()),
+ }
+}
+
pub fn new_room(
bk: &Backend,
name: &str,
diff --git a/fractal-matrix-api/src/model/fileinfo.rs b/fractal-matrix-api/src/model/fileinfo.rs
new file mode 100644
index 00000000..b69fef7c
--- /dev/null
+++ b/fractal-matrix-api/src/model/fileinfo.rs
@@ -0,0 +1,18 @@
+use serde::{Deserialize, Serialize};
+use serde_json::Value as JsonValue;
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct Info {
+ pub thumbnail_url: Option<String>,
+ pub thumbnail_info: Option<JsonValue>,
+ pub w: Option<u32>,
+ pub h: Option<u32>,
+ pub size: u32,
+ pub mimetype: String,
+ pub orientation: Option<i32>,
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct ExtraContent {
+ pub info: Info,
+}
diff --git a/fractal-matrix-api/src/model/mod.rs b/fractal-matrix-api/src/model/mod.rs
index 93ed1579..758a0e27 100644
--- a/fractal-matrix-api/src/model/mod.rs
+++ b/fractal-matrix-api/src/model/mod.rs
@@ -1,4 +1,5 @@
pub mod event;
+pub mod fileinfo;
pub mod filter;
pub mod member;
pub mod message;
diff --git a/fractal-matrix-api/src/types.rs b/fractal-matrix-api/src/types.rs
index fce0a4a4..3742967d 100644
--- a/fractal-matrix-api/src/types.rs
+++ b/fractal-matrix-api/src/types.rs
@@ -1,4 +1,6 @@
pub use crate::model::event::Event;
+pub use crate::model::fileinfo::ExtraContent;
+pub use crate::model::fileinfo::Info;
pub use crate::model::filter::EventFilter;
pub use crate::model::filter::Filter;
pub use crate::model::filter::RoomEventFilter;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]