Skip to content

Commit

Permalink
chore(cargo): Update dependency (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 authored May 19, 2023
1 parent 206e38a commit 03f3811
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 184 deletions.
216 changes: 125 additions & 91 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ humansize = "2.1.3"
window-vibrancy = "0.3.2"
uuid = { version = "1", features = ["serde", "v4"] }
libloading = "0.7.4"
warp = { git = "https://github.com/Satellite-im/Warp", rev = "33a1c95cbb04b6a139057a1c043459799f4c8162" }
warp-mp-ipfs = { git = "https://github.com/Satellite-im/Warp", rev = "33a1c95cbb04b6a139057a1c043459799f4c8162" }
warp-rg-ipfs = { git = "https://github.com/Satellite-im/Warp", rev = "33a1c95cbb04b6a139057a1c043459799f4c8162" }
warp-fs-ipfs = { git = "https://github.com/Satellite-im/Warp", rev = "33a1c95cbb04b6a139057a1c043459799f4c8162" }
warp = { git = "https://github.com/Satellite-im/Warp", rev = "08486f1607d14c8702090030a39ab5687f2a00e4" }
warp-mp-ipfs = { git = "https://github.com/Satellite-im/Warp", rev = "08486f1607d14c8702090030a39ab5687f2a00e4" }
warp-rg-ipfs = { git = "https://github.com/Satellite-im/Warp", rev = "08486f1607d14c8702090030a39ab5687f2a00e4" }
warp-fs-ipfs = { git = "https://github.com/Satellite-im/Warp", rev = "08486f1607d14c8702090030a39ab5687f2a00e4" }
rfd = "0.11.3"
mime = "0.3.16"
serde = "1.0"
Expand Down
5 changes: 0 additions & 5 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ pub const VIDEO_FILE_EXTENSIONS: &[&str] = &[
".mp4", ".mov", ".mkv", ".avi", ".flv", ".wmv", ".m4v", ".3gp",
];

pub const IMAGE_EXTENSIONS: &[&str] = &[
".png", ".jpg", ".jpeg", ".svg", ".heic", ".tiff", ".gif", ".webp", ".apng", ".avif", ".ico",
".bmp", ".svgz",
];

pub const DOC_EXTENSIONS: &[&str] = &[".doc", ".docx", ".pdf", ".txt"];

pub fn get_images_dir() -> anyhow::Result<PathBuf> {
Expand Down
105 changes: 30 additions & 75 deletions common/src/warp_runner/manager/commands/constellation_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ use derive_more::Display;

use futures::{channel::oneshot, StreamExt};
use humansize::{format_size, DECIMAL};
use mime::*;
use once_cell::sync::Lazy;
use tempfile::TempDir;
use tokio::sync::mpsc;

use crate::{state::storage::Storage as uplink_storage, IMAGE_EXTENSIONS, VIDEO_FILE_EXTENSIONS};
use crate::{state::storage::Storage as uplink_storage, VIDEO_FILE_EXTENSIONS};
use crate::{warp_runner::Storage as warp_storage, DOC_EXTENSIONS};

use warp::{
constellation::{
directory::Directory,
item::{Item, ItemType},
file::File,
item::{FormatType, Item, ItemType},
Progression,
},
error::Error,
Expand Down Expand Up @@ -473,7 +473,6 @@ async fn upload_files(
}

let video_formats = VIDEO_FILE_EXTENSIONS.to_vec();
let image_formats = IMAGE_EXTENSIONS.to_vec();
let doc_formats = DOC_EXTENSIONS.to_vec();

let file_extension = std::path::Path::new(&filename)
Expand All @@ -482,23 +481,6 @@ async fn upload_files(
.map(|s| format!(".{s}"))
.unwrap_or_default();

if image_formats.iter().any(|f| f == &file_extension) {
match set_thumbnail_if_file_is_image(warp_storage, filename.clone()).await {
Ok(_) => {
log::info!("Image Thumbnail uploaded");
let _ = tx.send(FileTransferProgress::Step(
FileTransferStep::Thumbnail(Some(())),
));
}
Err(error) => {
log::error!("Not possible to update thumbnail for image: {:?}", error);
let _ = tx.send(FileTransferProgress::Step(
FileTransferStep::Thumbnail(None),
));
}
};
}

if video_formats.iter().any(|f| f == &file_extension) {
match set_thumbnail_if_file_is_video(
warp_storage,
Expand Down Expand Up @@ -635,15 +617,14 @@ async fn set_thumbnail_if_file_is_video(

if let Some(mut child) = output.stdout {
let mut contents = vec![];

child.read_to_end(&mut contents)?;

let image = std::fs::read(temp_path)?;

let prefix = format!("data:{};base64,", IMAGE_JPEG);
let base64_image = base64::encode(image);
let img = prefix + base64_image.as_str();
item.set_thumbnail(&img);
item.set_thumbnail(&image);
item.set_thumbnail_format(FormatType::Mime(
"image/jpeg".parse().expect("Correct mime type"),
));
Ok(())
} else {
log::warn!("Failed to save thumbnail from a video file");
Expand Down Expand Up @@ -697,11 +678,10 @@ async fn set_thumbnail_if_file_is_document(
let path_2 = format!("{}.jpg", temp_path.to_string_lossy());
std::thread::sleep(std::time::Duration::from_secs(1));
let image = std::fs::read(path_2)?;

let prefix = format!("data:{};base64,", IMAGE_JPEG);
let base64_image = base64::encode(image);
let img = prefix + base64_image.as_str();
item.set_thumbnail(&img);
item.set_thumbnail(&image);
item.set_thumbnail_format(FormatType::Mime(
"image/jpeg".parse().expect("Correct mime type"),
));
Ok(())
} else {
log::warn!("Failed to save thumbnail from a document file");
Expand All @@ -712,50 +692,6 @@ async fn set_thumbnail_if_file_is_document(
.map_err(anyhow::Error::from)?
}

async fn set_thumbnail_if_file_is_image(
warp_storage: &warp_storage,
filename_to_save: String,
) -> Result<(), Error> {
let item = warp_storage
.current_directory()?
.get_item(&filename_to_save)?;
let file = warp_storage.get_buffer(&filename_to_save).await?;

// Since files selected are filtered to be jpg, jpeg, png or svg the last branch is not reachable
let extension = Path::new(&filename_to_save)
.extension()
.and_then(OsStr::to_str)
.map(|ext| ext.to_lowercase());

let mime = match extension {
Some(m) => match m.as_str() {
"png" => IMAGE_PNG.to_string(),
"jpg" => IMAGE_JPEG.to_string(),
"jpeg" => IMAGE_JPEG.to_string(),
"svg" => IMAGE_SVG.to_string(),
_ => {
log::warn!("invalid mime type: {m:?}");
return Err(Error::InvalidItem);
}
},
None => {
log::warn!("thumbnail has no mime type");
return Err(Error::InvalidItem);
}
};

if !file.is_empty() {
let prefix = format!("data:{mime};base64,");
let base64_image = base64::encode(&file);
let img = prefix + base64_image.as_str();
item.set_thumbnail(&img);
Ok(())
} else {
log::warn!("thumbnail file is empty");
Err(Error::InvalidItem)
}
}

async fn download_file(
warp_storage: &warp_storage,
file_name: String,
Expand All @@ -767,3 +703,22 @@ async fn download_file(
log::info!("{file_name} downloaded");
Ok(())
}

pub fn thumbnail_to_base64(file: &File) -> String {
let thumbnail = file.thumbnail();

if thumbnail.is_empty() {
return String::new();
}

let ty = file.thumbnail_format();
let mime = match ty {
FormatType::Mime(mime) => mime.to_string(),
FormatType::Generic => "application/octet-stream".into(),
};

let prefix = format!("data:image/{mime};base64,");
let base64_image = base64::encode(thumbnail);

prefix + &base64_image
}
3 changes: 2 additions & 1 deletion common/src/warp_runner/manager/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ mod tesseract_commands;

// this shortens the path required to use the functions and structs
pub use constellation_commands::{
handle_constellation_cmd, ConstellationCmd, FileTransferProgress, FileTransferStep,
handle_constellation_cmd, thumbnail_to_base64, ConstellationCmd, FileTransferProgress,
FileTransferStep,
};
pub use multipass_commands::{handle_multipass_cmd, MultiPassCmd};
pub use other_commands::*;
Expand Down
14 changes: 9 additions & 5 deletions common/src/warp_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod conv_stream;
mod manager;
pub mod ui_adapter;

pub use manager::commands::{FileTransferProgress, FileTransferStep};
pub use manager::commands::{thumbnail_to_base64, FileTransferProgress, FileTransferStep};
pub use manager::{ConstellationCmd, MultiPassCmd, OtherCmd, RayGunCmd, TesseractCmd};

pub type WarpCmdTx = UnboundedSender<WarpCmd>;
Expand Down Expand Up @@ -338,10 +338,14 @@ async fn warp_initialization(tesseract: Tesseract) -> Result<manager::Warp, warp
.await
.map(|mp| Box::new(mp) as Account)?;

let storage =
warp_fs_ipfs::IpfsFileSystem::new(account.clone(), Some(FsIpfsConfig::production(path)))
.await
.map(|ct| Box::new(ct) as Storage)?;
let mut config = FsIpfsConfig::production(path);
config.thumbnail_size = (500, 500);

config.thumbnail_exact_format = false;

let storage = warp_fs_ipfs::IpfsFileSystem::new(account.clone(), Some(config))
.await
.map(|ct| Box::new(ct) as Storage)?;

// FYI: setting `rg_config.store_setting.disable_sender_event_emit` to `true` will prevent broadcasting `ConversationCreated` on the sender side
let rg_config = RgIpfsConfig::production(path);
Expand Down
4 changes: 2 additions & 2 deletions ui/src/layouts/file_preview.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use common::icons::outline::Shape as Icon;
use common::language::get_local_text;
use common::{icons::outline::Shape as Icon, warp_runner::thumbnail_to_base64};
use dioxus::prelude::*;
use kit::components::context_menu::{ContextItem, ContextMenu};
use warp::constellation::file::File;
Expand All @@ -12,7 +12,7 @@ pub struct Props<'a> {

#[allow(non_snake_case)]
pub fn FilePreview<'a>(cx: Scope<'a, Props<'a>>) -> Element<'a> {
let thumbnail = cx.props.file.thumbnail();
let thumbnail = thumbnail_to_base64(cx.props.file);

cx.render(rsx!(rsx!(div {
ContextMenu {
Expand Down
3 changes: 2 additions & 1 deletion ui/src/layouts/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use common::icons::Icon as IconElement;
use common::language::get_local_text;
use common::state::ToastNotification;
use common::state::{ui, Action, State};
use common::warp_runner::thumbnail_to_base64;
use dioxus::{html::input_data::keyboard_types::Code, prelude::*};
use dioxus_desktop::use_window;
use dioxus_router::*;
Expand Down Expand Up @@ -409,7 +410,7 @@ pub fn FilesLayout(cx: Scope<Props>) -> Element {
)),
File {
key: "{key}-file",
thumbnail: file.thumbnail(),
thumbnail: thumbnail_to_base64(file),
text: file.name(),
aria_label: file.name(),
with_rename: *is_renaming_map.read() == Some(key),
Expand Down

0 comments on commit 03f3811

Please sign in to comment.