Skip to content

Commit

Permalink
Merge pull request #18 from picture-pro/15-switch-to-avif-thumbnails
Browse files Browse the repository at this point in the history
15 switch to avif thumbnails
  • Loading branch information
johnbchron authored Feb 19, 2024
2 parents 06ae1cf + 57e093f commit e90423f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 41 deletions.
4 changes: 2 additions & 2 deletions crates/artifact/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn inner_download_artifact(
.wrap_err("Failed to read bytes of downloaded artifact")
}

#[instrument(skip(object_store))]
#[instrument(skip(object_store, contents))]
pub async fn upload_artifact(
object_store: ObjectStoreGenerator,
id: &str,
Expand All @@ -76,7 +76,7 @@ pub async fn upload_artifact(
inner_upload_artifact(&*object_store, id, contents).await
}

#[instrument(skip(object_store))]
#[instrument(skip(object_store, contents))]
async fn inner_upload_artifact(
object_store: &dyn object_store::ObjectStore,
id: &str,
Expand Down
4 changes: 2 additions & 2 deletions crates/bl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ pub async fn upload_single_photo(
image::imageops::FilterType::Lanczos3,
);

// encode as png to bytes
// encode as jpeg to bytes
let mut thumbnail_bytes = Vec::new();
let encoder = image::codecs::png::PngEncoder::new(&mut thumbnail_bytes);
let encoder = image::codecs::jpeg::JpegEncoder::new(&mut thumbnail_bytes);
thumbnail_image.write_with_encoder(encoder).map_err(|e| {
PhotoUploadError::InvalidImage(format!(
"Failed to encode thumbnail image: {e:?}"
Expand Down
10 changes: 10 additions & 0 deletions crates/site-app/src/components/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ pub fn navigate_to(path: &str) {
logging::error!("failed to navigate: {:?}", e);
}
}

pub fn reload() {
let result = web_sys::window()
.expect("Failed to get window")
.location()
.reload();
if let Err(e) = result {
logging::error!("failed to reload: {:?}", e);
}
}
21 changes: 0 additions & 21 deletions crates/site-app/src/components/photo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,6 @@ pub async fn fetch_photo_thumbnail(
}
};

// load using the image crate
// let thumbnail_image =
// info_span!("load_image_from_artifact_data").in_scope(|| {
// image::load_from_memory(&thumbnail_artifact_content).map_err(|e| {
// ServerFnError::new(format!("Failed to load thumbnail as image:
// {e:?}")) })
// })?;

// // encode to avif bytes
// let mut buffer = Vec::new();
// let encoder = image::codecs::avif::AvifEncoder::new(&mut buffer);
// info_span!("encode_thumbnail_to_avif").in_scope(|| {
// thumbnail_image.write_with_encoder(encoder).map_err(|e| {
// ServerFnError::new(format!("Failed to encode thumbnail: {e:?}"))
// })
// })?;

// // encode avif bytes to base64
// let data = info_span!("encode_thumbnail_to_base64")
// .in_scope(|| BASE64_STANDARD.encode(&buffer));

let data = BASE64_STANDARD.encode(&thumbnail_artifact_content);

Ok(PhotoThumbnailDisplayParams {
Expand Down
19 changes: 17 additions & 2 deletions crates/site-app/src/components/photo_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ pub fn PhotoUpload() -> impl IntoView {
// `MultipartData` implements `From<FormData>`
photo_upload(data.into())
});
let pending = upload_action.pending();
let value = upload_action.value();
let successful = move || matches!(value(), Some(Ok(_)));

create_effect(move |_| {
if successful() {
crate::components::navigation::reload();
}
});

view! {
<div class="d-card bg-base-200 shadow max-w-sm">
Expand All @@ -31,15 +40,21 @@ pub fn PhotoUpload() -> impl IntoView {
</div>
<div class="mt-6"></div>
<div class="d-form-control">
<button type="submit" class="d-btn d-btn-primary w-full">"Upload"</button>
<button
type="submit" class="d-btn d-btn-primary w-full"
disabled={move || pending()}
>
{ move || if pending() { view!{ <span class="d-loading d-loading-spinner" /> }.into_view() } else { view! {}.into_view() } }
"Upload"
</button>
</div>
</form>
</div>
</div>
}
}

#[cfg_attr(feature = "ssr", tracing::instrument)]
#[cfg_attr(feature = "ssr", tracing::instrument(skip(data)))]
#[server(input = MultipartFormData)]
pub async fn photo_upload(
data: MultipartData,
Expand Down
21 changes: 7 additions & 14 deletions crates/site-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ async fn leptos_routes_handler(
handler(req).await.into_response()
}

fn init_logging() {
#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install().expect("Failed to install color_eyre");

let filter = tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or(
Expand All @@ -72,22 +73,14 @@ fn init_logging() {
tracing_subscriber::fmt().with_env_filter(filter).init();
}
#[cfg(feature = "chrome-tracing")]
{
let guard = {
use tracing_subscriber::prelude::*;

let (chrome_layer, _guard) =
let (chrome_layer, guard) =
tracing_chrome::ChromeLayerBuilder::new().build();
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(filter)
.with(chrome_layer)
.init();
}
}

#[tokio::main]
async fn main() -> Result<()> {
init_logging();
tracing_subscriber::registry().with(chrome_layer).init();
guard
};

// Setting get_configuration(None) means we'll be using cargo-leptos's env
// values For deployment these variables are:
Expand Down

0 comments on commit e90423f

Please sign in to comment.