|
|
@ -1,10 +1,11 @@
|
|
|
|
use crate::tauri_plugin::error::PluginError;
|
|
|
|
use crate::tauri_plugin::error::{PluginError, PluginResult};
|
|
|
|
use crate::tauri_plugin::state::{ApiState, BufferState};
|
|
|
|
use crate::tauri_plugin::state::{ApiState, BufferState};
|
|
|
|
use crate::types::identifier::FileIdentifier;
|
|
|
|
use crate::types::identifier::FileIdentifier;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use tauri::http::{Request, Response, ResponseBuilder};
|
|
|
|
use tauri::http::{Request, Response, ResponseBuilder};
|
|
|
|
use tauri::{AppHandle, Builder, Manager, Runtime};
|
|
|
|
use tauri::{AppHandle, Builder, Manager, Runtime};
|
|
|
|
|
|
|
|
use tokio::runtime::{Builder as TokioRuntimeBuilder, Runtime as TokioRuntime};
|
|
|
|
use url::Url;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
|
|
|
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
|
|
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
|
|
@ -12,8 +13,21 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
|
|
|
pub fn register_custom_uri_schemes<R: Runtime>(builder: Builder<R>) -> Builder<R> {
|
|
|
|
pub fn register_custom_uri_schemes<R: Runtime>(builder: Builder<R>) -> Builder<R> {
|
|
|
|
builder
|
|
|
|
builder
|
|
|
|
.register_uri_scheme_protocol("once", once_scheme)
|
|
|
|
.register_uri_scheme_protocol("once", once_scheme)
|
|
|
|
.register_uri_scheme_protocol("content", content_scheme)
|
|
|
|
.register_uri_scheme_protocol("content", |a, r| {
|
|
|
|
.register_uri_scheme_protocol("thumb", thumb_scheme)
|
|
|
|
build_uri_runtime()?.block_on(content_scheme(a, r))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.register_uri_scheme_protocol("thumb", |a, r| {
|
|
|
|
|
|
|
|
build_uri_runtime()?.block_on(thumb_scheme(a, r))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn build_uri_runtime() -> PluginResult<TokioRuntime> {
|
|
|
|
|
|
|
|
let runtime = TokioRuntimeBuilder::new_current_thread()
|
|
|
|
|
|
|
|
.thread_name("custom-scheme")
|
|
|
|
|
|
|
|
.max_blocking_threads(1)
|
|
|
|
|
|
|
|
.build()?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(runtime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn once_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<Response> {
|
|
|
|
fn once_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<Response> {
|
|
|
@ -35,7 +49,7 @@ fn once_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<Resp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn content_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<Response> {
|
|
|
|
async fn content_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<Response> {
|
|
|
|
let api_state = app.state::<ApiState>();
|
|
|
|
let api_state = app.state::<ApiState>();
|
|
|
|
let buf_state = app.state::<BufferState>();
|
|
|
|
let buf_state = app.state::<BufferState>();
|
|
|
|
let hash = request.uri().trim_start_matches("content://");
|
|
|
|
let hash = request.uri().trim_start_matches("content://");
|
|
|
@ -46,14 +60,16 @@ fn content_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<R
|
|
|
|
.mimetype(&buffer.mime)
|
|
|
|
.mimetype(&buffer.mime)
|
|
|
|
.body(buffer.buf)
|
|
|
|
.body(buffer.buf)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
let api = api_state.api_sync()?;
|
|
|
|
let api = api_state.api().await?;
|
|
|
|
let file =
|
|
|
|
let file = api
|
|
|
|
futures::executor::block_on(api.file.get_file(FileIdentifier::Hash(hash.to_string())))?;
|
|
|
|
.file
|
|
|
|
|
|
|
|
.get_file(FileIdentifier::Hash(hash.to_string()))
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
let mime = file.mime_type.unwrap_or("image/png".to_string());
|
|
|
|
let mime = file.mime_type.unwrap_or("image/png".to_string());
|
|
|
|
let bytes = futures::executor::block_on(
|
|
|
|
let bytes = api
|
|
|
|
api.file
|
|
|
|
.file
|
|
|
|
.read_file_by_hash(FileIdentifier::Hash(hash.to_string())),
|
|
|
|
.read_file_by_hash(FileIdentifier::Hash(hash.to_string()))
|
|
|
|
)?;
|
|
|
|
.await?;
|
|
|
|
buf_state.add_entry(hash.to_string(), mime.clone(), bytes.clone());
|
|
|
|
buf_state.add_entry(hash.to_string(), mime.clone(), bytes.clone());
|
|
|
|
ResponseBuilder::new()
|
|
|
|
ResponseBuilder::new()
|
|
|
|
.mimetype(&mime)
|
|
|
|
.mimetype(&mime)
|
|
|
@ -62,7 +78,7 @@ fn content_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<R
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn thumb_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<Response> {
|
|
|
|
async fn thumb_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<Response> {
|
|
|
|
let api_state = app.state::<ApiState>();
|
|
|
|
let api_state = app.state::<ApiState>();
|
|
|
|
let buf_state = app.state::<BufferState>();
|
|
|
|
let buf_state = app.state::<BufferState>();
|
|
|
|
|
|
|
|
|
|
|
@ -91,12 +107,15 @@ fn thumb_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<Res
|
|
|
|
.mimetype(&buffer.mime)
|
|
|
|
.mimetype(&buffer.mime)
|
|
|
|
.body(buffer.buf)
|
|
|
|
.body(buffer.buf)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
let api = api_state.api_sync()?;
|
|
|
|
let api = api_state.api().await?;
|
|
|
|
let (thumb, bytes) = futures::executor::block_on(api.file.get_thumbnail_of_size(
|
|
|
|
let (thumb, bytes) = api
|
|
|
|
|
|
|
|
.file
|
|
|
|
|
|
|
|
.get_thumbnail_of_size(
|
|
|
|
FileIdentifier::Hash(hash.to_string()),
|
|
|
|
FileIdentifier::Hash(hash.to_string()),
|
|
|
|
((height as f32 * 0.8) as u32, (width as f32 * 0.8) as u32),
|
|
|
|
((height as f32 * 0.8) as u32, (width as f32 * 0.8) as u32),
|
|
|
|
((height as f32 * 1.2) as u32, (width as f32 * 1.2) as u32),
|
|
|
|
((height as f32 * 1.2) as u32, (width as f32 * 1.2) as u32),
|
|
|
|
))?;
|
|
|
|
)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
buf_state.add_entry(
|
|
|
|
buf_state.add_entry(
|
|
|
|
request.uri().to_string(),
|
|
|
|
request.uri().to_string(),
|
|
|
|
thumb.mime_type.clone(),
|
|
|
|
thumb.mime_type.clone(),
|
|
|
|