From 3013ecdf6ce980a53899ad0bb27b982f1bb77634 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Sat, 18 Apr 2020 16:52:01 -0300 Subject: [PATCH] Not every resource can be displayed inline --- src/storage/joplin.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/storage/joplin.rs b/src/storage/joplin.rs index da780e5..2ac8cbe 100644 --- a/src/storage/joplin.rs +++ b/src/storage/joplin.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::path::Path; use reqwest::multipart::Form; use reqwest::multipart::Part; @@ -9,6 +10,8 @@ use crate::config::JoplinConfig; use crate::storage::data::Data; use crate::storage::storage::Storage; +static INLINABLE: [&'static str; 4] = ["jpeg", "jpg", "png", "gif"]; + /// This is the folder structured returned by Joplin. It is here so Reqwests can /// unjson the data (there are more fields, but these are the only ones we need /// right now). @@ -86,7 +89,12 @@ impl Joplin { fn add_resources_to_text(text: &mut String, resources: &Vec) { resources.iter().for_each(|resource| { let link = format!( - "![{filename}](:/{resource})", + "{inline}[{filename}](:/{resource})", + inline = if Joplin::is_inlineable(&resource.filename) { + "!" + } else { + "" + }, filename = resource.filename, resource = resource.id ); @@ -95,6 +103,16 @@ impl Joplin { }); } + fn is_inlineable(filename: &String) -> bool { + if let Some(extension) = Path::new(filename).extension() { + INLINABLE + .iter() + .any(|ext| *ext == extension.to_str().unwrap_or("")) + } else { + false + } + } + fn save_attachments(&self, record: &Data) -> Vec { record .attachments @@ -123,10 +141,7 @@ impl Joplin { } fn upload_resource(&self, filename: String, content: Vec) -> String { - let props = format!( - "{{\"title\": \"{filename}\", \"filename\": \"{filename}\"}}", - filename = &filename, - ); + let props = format!("{{\"title\": \"{filename}\"}}", filename = &filename); let data_part = Part::bytes(content).file_name(filename); let props_part = Part::text(props); let form = Form::new()