This commit is contained in:
Denis Manherz 2024-08-20 14:57:01 +02:00
parent da28216938
commit 421aac0b86
11 changed files with 2494 additions and 115 deletions

19
.direnv/bin/nix-direnv-reload Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -e
if [[ ! -d "/home/denis/repos/leptose_intoleranz" ]]; then
echo "Cannot find source directory; Did you move it?"
echo "(Looking for "/home/denis/repos/leptose_intoleranz")"
echo 'Cannot force reload with this script - use "direnv reload" manually and then try again'
exit 1
fi
# rebuild the cache forcefully
_nix_direnv_force_reload=1 direnv exec "/home/denis/repos/leptose_intoleranz" true
# Update the mtime for .envrc.
# This will cause direnv to reload again - but without re-building.
touch "/home/denis/repos/leptose_intoleranz/.envrc"
# Also update the timestamp of whatever profile_rc we have.
# This makes sure that we know we are up to date.
touch -r "/home/denis/repos/leptose_intoleranz/.envrc" "/home/denis/repos/leptose_intoleranz/.direnv"/*.rc

View file

@ -0,0 +1 @@
/nix/store/vqljs1ixabpdbfmq3llnlkj5dac1hq4c-source

View file

@ -0,0 +1 @@
/nix/store/w6qscasrm9g56nwgz9xv6nwcpcm2fjz8-source

View file

@ -0,0 +1 @@
/nix/store/x6kzvmscjdajg950cr3xw720dwmj9dnb-source

View file

@ -0,0 +1 @@
/nix/store/gpcnsvybyhm677fpg9ygdm4bppyrnwhp-nix-shell-env

File diff suppressed because it is too large Load diff

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

375
app.rs.bak Normal file
View file

@ -0,0 +1,375 @@
use crate::error_template::{AppError, ErrorTemplate};
use cfg_if::*;
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
cfg_if! {
if #[cfg(feature="ssr")]{
use surrealdb::sql::Thing;
use surrealdb::Surreal;
use surrealdb::engine::remote::ws::Ws;
use surrealdb::opt::auth::Root;
}
}
#[component]
pub fn App() -> impl IntoView {
// Provides context that manages stylesheets, titles, meta tags, etc.
provide_meta_context();
view! {
<Link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"
/>
<Stylesheet id="leptos" href="/pkg/wallpaper-gen-front-back.css"/>
// sets the document title
<Title text="Welcome to Leptos"/>
// content for this welcome page
<Router fallback=|| {
let mut outside_errors = Errors::default();
outside_errors.insert_with_default_key(AppError::NotFound);
view! { <ErrorTemplate outside_errors/> }.into_view()
}>
<main>
<Routes>
<Route path="/" view=Home/>
</Routes>
</main>
</Router>
}
}
#[component]
fn Home() -> impl IntoView {
use leptos::html::Div;
use leptos_use::*;
let (size, set_size) = create_signal(1.);
let (page, set_page) = create_signal(0);
//let res = create_local_resource(page, get_pics2);
let (all, set_all) = create_signal(Vec::<PicStruct>::new());
let res = create_local_resource(
move || (page.get(), size.get()),
move |d| get_pics2(d.0, d.1),
);
let el = create_node_ref::<Div>();
let _ = use_infinite_scroll_with_options(
el,
move |_| async move {
if !res.loading().get() && res().is_some() {
set_page.update(|page| *page += 1);
}
res.and_then(move |_| {
set_all.update(|dat| dat.extend(res().unwrap().unwrap()));
});
/*if !res.loading().get() {
set_page.update(|page| *page += 1);
} else if res().is_some() {
set_all.update(|dat| dat.extend(res().unwrap().unwrap()));
// println!("{:?}", all().len())
}*/
},
UseInfiniteScrollOptions::default().distance(10.0),
);
view! {
<h1>"Home"</h1>
<div>
<input
on:input=move |ev| {
set_size(event_target_value(&ev).parse::<f64>().unwrap());
}
prop:value=size
type="range"
min="0.5"
max="2"
step="0.5"
value="1"
/>
</div>
<div class="main overflow-y-auto max-h-300" node_ref=el>
<For each=move || all.get() key=|state| state.key.clone() let:child>
<Pic width=child.width height=child.height scale=size url=child.url/>
</For>
</div>
<div class="w-full">
<p class="text-center">{move || page()}</p>
</div>
}
}
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PicStruct {
key: String,
width: i32,
height: i32,
url: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
struct Wallpaper {
file_name: String,
info: String,
date_added: String,
height: u64,
width: u64,
path: String,
thumbs: Vec<String>,
}
#[derive(Debug, Serialize)]
struct Name<'a> {
first: &'a str,
last: &'a str,
}
#[derive(Debug, Serialize)]
struct Person<'a> {
title: &'a str,
name: Name<'a>,
marketing: bool,
}
#[derive(Debug, Serialize)]
struct Responsibility {
marketing: bool,
}
#[derive(Debug, Deserialize)]
struct Record {
#[allow(dead_code)]
id: i32,
}
use std::env;
#[server(GetPics, "/api")]
pub async fn get_pics2(page: i32, size: f64) -> Result<Vec<PicStruct>, ServerFnError> {
use image::GenericImageView;
use imageinfo::ImageInfo;
use std::fs;
use std::path::PathBuf;
let query = "
SELECT * FROM wallpapers LIMIT 10;
";
let db = Surreal::new::<Ws>("127.0.0.1:8000")
.await
.expect("Couldnt connect to db server");
// Select a specific namespace / database
db.use_ns("test")
.use_db("test")
.await
.expect("Couldnt find db");
//let papers: Vec<Wallpaper> = db.select("wallpapers").await.expect("Err");
//println!("{papers:#?}");
let mut res = db.query(query).await?;
let pap: Vec<Wallpaper> = res.take(0)?;
println!("{pap:#?}");
let mut pics = Vec::new();
let folder_path = "./public";
// Define the number of pictures per page
let pics_per_page = 10;
// Calculate the index to start from based on the page number
let start_index = (page) * pics_per_page;
let end_index = start_index + pics_per_page;
// manage a vector with all the image paths
let mut image_file_paths = Vec::<PathBuf>::new();
for entry in fs::read_dir(folder_path)? {
let entry = entry?;
let file_path = entry.path();
let file_name_str = file_path.file_name().unwrap_or_default().to_str().unwrap();
if size == 0.5 {
//skip 1024 and 512
if file_name_str.contains("512x512") || file_name_str.contains("1024x1024") {
continue;
}
} else if size == 1.0 {
if file_name_str.contains("256x256") || file_name_str.contains("1024x1024") {
println!("sdfasdf");
continue;
}
} else if size == 1.5 {
if file_name_str.contains("256x256") || file_name_str.contains("512x512") {
continue;
}
}
image_file_paths.push(file_path.clone());
}
for (i, file_path) in image_file_paths.iter().enumerate() {
if file_path.is_file() {
if i < start_index as usize {
continue;
}
let file = std::fs::File::open(file_path)?;
let mut bufreader = std::io::BufReader::new(&file);
let exifreader = exif::Reader::new();
let exif = exifreader.read_from_container(&mut bufreader)?;
for f in exif.fields() {
println!(
"{} {} {}",
f.tag,
f.ifd_num,
f.display_value().with_unit(&exif)
);
}
let file_name = file_path.file_name().unwrap_or_default();
let file_name_str = file_name.to_str().unwrap();
// Check if the file has a supported image extension
if let Some(extension) = file_path.extension() {
if let Some(extension_str) = extension.to_str() {
if image::ImageFormat::from_extension(extension_str).is_some() {
// Process the image
let img = image::open(&file_path).expect("Failed to open image");
let (x, y) = img.dimensions();
pics.push(PicStruct {
key: file_name_str.to_string(), // Could not be unique if two images were created at exactly
// the same time
width: x as i32,
height: y as i32,
url: file_name.to_string_lossy().into(),
});
if i + 1 >= end_index as usize {
break;
}
}
}
}
}
}
// Send different picture resolutions depending on selected size
// sizes 256 512 1024
// Iterate over the entries in the folder
/*for (index, entry) in fs::read_dir(folder_path)
.expect("Failed to read directory")
.enumerate()
{
if let Ok(entry) = entry {
if index < start_index as usize {
// Skip pictures before the start index
continue;
}
let file_path = entry.path();
let file_name = file_path.file_name().unwrap_or_default();
let file_name_str = file_name.to_str().unwrap();
// Skip the wrong sizes
if size == 0.5 {
//skip 1024 and 512
if file_name_str.contains("512x512") || file_name_str.contains("1024x1024") {
continue;
}
} else if size == 1.0 {
if file_name_str.contains("256x256") || file_name_str.contains("1024x1024") {
continue;
}
} else if size == 1.5 {
if file_name_str.contains("256x256") || file_name_str.contains("512x512") {
continue;
}
}
// Check if the entry is a file
if file_path.is_file() {
// Check if the file has a supported image extension
if let Some(extension) = file_path.extension() {
if let Some(extension_str) = extension.to_str() {
if image::ImageFormat::from_extension(extension_str).is_some() {
// Process the image
let img = image::open(&file_path).expect("Failed to open image");
let (x, y) = img.dimensions();
pics.push(PicStruct {
key: file_name_str.to_string(), // Could not be unique if two images were created at exactly
// the same time
width: x as i32,
height: y as i32,
url: file_name.to_string_lossy().into(),
});
// Break if we have reached the end index
if index + 1 >= end_index as usize {
break;
}
}
}
}
}
}
}*/
println!("{:#?}", pics);
Ok(pics)
}
#[component]
fn Info(show: ReadSignal<bool>, set_show: WriteSignal<bool>) -> impl IntoView {
view! {
<Show when=move || show()>
<div id="info-main" class="rounded">
<button type="button" on:click=move |_| set_show(false)>
"Close"
</button>
<input type="text" id="country" name="country" value="Norway" readonly/>
<br/>
<br/>
</div>
</Show>
}
}
#[component]
fn Pic(width: i32, height: i32, url: String, scale: ReadSignal<f64>) -> impl IntoView {
let (showInfo, set_showInfo) = create_signal(false);
view! {
<div
class="pic-outer"
// style="border:1px solid black;"
style:width=move || format!("{}px", f64::from(width + 10))
style:height=move || format!("{}px", f64::from(height + 10))
>
<div
class="pic-inner"
style=format!("background-image: url('{}'); background-size: cover;", url)
style:width=move || format!("{}px", f64::from(width))
style:height=move || format!("{}px", f64::from(height))
>
<div class="dropdown">
<button class="dropbtn">
<i class="fa-solid fa-download"></i>
</button>
<div class="dropdown-content">
<a href="https://google.com">16:9</a>
<a href="#">16:10</a>
<a href="#">4:3</a>
</div>
</div>
<button id="info-button" on:click=move |_| set_showInfo(true)>
<i class="fa-regular fa-circle-question"></i>
</button>
</div>
</div>
<Info show=showInfo set_show=set_showInfo/>
}
}

44
flake.lock Normal file
View file

@ -0,0 +1,44 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1704420045,
"narHash": "sha256-C36QmoJd5tdQ5R9MC1jM7fBkZW9zBUqbUCsgwS6j4QU=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "c1be43e8e837b8dbee2b3665a007e761680f0c3d",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-23.11",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-unstable": {
"locked": {
"lastModified": 1704538339,
"narHash": "sha256-1734d3mQuux9ySvwf6axRWZRBhtcZA9Q8eftD6EZg6U=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "46ae0210ce163b3cba6c7da08840c1d63de9c701",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"nixpkgs-unstable": "nixpkgs-unstable"
}
}
},
"root": "root",
"version": 7
}

53
flake.nix Normal file
View file

@ -0,0 +1,53 @@
{
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
inputs.nixpkgs-unstable.url = "github:nixos/nixpkgs/nixos-unstable";
outputs = {
self,
nixpkgs,
...
}: let
pkgs = import nixpkgs {
system = "x86_64-linux";
};
in {
devShells.x86_64-linux = {
default = pkgs.mkShell {
buildInputs = with pkgs; [
clang
# Replace llvmPackages with llvmPackages_X, where X is the latest LLVM version (at the time of writing, 16)
cargo-leptos
rustup
nodejs
pkg-config
openssl
nodejs
];
RUSTC_VERSION = pkgs.lib.readFile ./rust-toolchain;
# https://github.com/rust-lang/rust-bindgen#environment-variables
LIBCLANG_PATH = pkgs.lib.makeLibraryPath [pkgs.llvmPackages_latest.libclang.lib];
shellHook = ''
export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin
export PATH=$PATH:''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/
'';
# Add precompiled library to rustc search path
RUSTFLAGS = builtins.map (a: ''-L ${a}/lib'') [
# add libraries here (e.g. pkgs.libvmi)
];
# Add glibc, clang, glib and other headers to bindgen search path
BINDGEN_EXTRA_CLANG_ARGS =
# Includes with normal include path
(builtins.map (a: ''-I"${a}/include"'') [
# add dev libraries here (e.g. pkgs.libvmi.dev)
pkgs.glibc.dev
])
# Includes with special directory paths
++ [
''-I"${pkgs.llvmPackages_latest.libclang.lib}/lib/clang/${pkgs.llvmPackages_latest.libclang.version}/include"''
''-I"${pkgs.glib.dev}/include/glib-2.0"''
''-I${pkgs.glib.out}/lib/glib-2.0/include/''
];
};
};
};
}

View file

@ -157,95 +157,82 @@ pub async fn get_pics2(page: i32, size: f64) -> Result<Vec<PicStruct>, ServerFnE
use imageinfo::ImageInfo; use imageinfo::ImageInfo;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
// Database setup
let query = " let query = "SELECT * FROM wallpapers LIMIT 10;";
SELECT * FROM wallpapers LIMIT 10;
";
let db = Surreal::new::<Ws>("127.0.0.1:8000") let db = Surreal::new::<Ws>("127.0.0.1:8000")
.await .await
.expect("Couldnt connect to db server"); .expect("Couldn't connect to db server");
// Select a specific namespace / database
db.use_ns("test") db.use_ns("test")
.use_db("test") .use_db("test")
.await .await
.expect("Couldnt find db"); .expect("Couldn't find db");
//let papers: Vec<Wallpaper> = db.select("wallpapers").await.expect("Err");
//println!("{papers:#?}");
let mut res = db.query(query).await?; let mut res = db.query(query).await?;
let pap: Vec<Wallpaper> = res.take(0)?; let pap: Vec<Wallpaper> = res.take(0)?;
println!("{pap:#?}");
let mut pics = Vec::new(); let mut pics = Vec::new();
let folder_path = PathBuf::from("./public/wallpaper-gen-data");
let folder_path = "./public";
// Define the number of pictures per page
let pics_per_page = 10; let pics_per_page = 10;
// Calculate the index to start from based on the page number
let start_index = (page) * pics_per_page; let start_index = (page) * pics_per_page;
let end_index = start_index + pics_per_page; let end_index = start_index + pics_per_page;
// manage a vector with all the image paths
let mut image_file_paths = Vec::<PathBuf>::new(); let mut image_file_paths = Vec::<PathBuf>::new();
for entry in fs::read_dir(folder_path)? {
for entry in fs::read_dir(&folder_path)? {
let entry = entry?; let entry = entry?;
let file_path = entry.path(); let subfolder_path = entry.path();
let file_name_str = file_path.file_name().unwrap_or_default().to_str().unwrap();
if size == 0.5 { if subfolder_path.is_dir() {
//skip 1024 and 512 if let Some(subfolder_name) = subfolder_path.file_name().and_then(|n| n.to_str()) {
if file_name_str.contains("512x512") || file_name_str.contains("1024x1024") { let mut final_path = subfolder_path.clone();
continue;
} let file_name = match size {
} else if size == 1.0 { 0.5 => format!("{}-thumb-256x146.png", subfolder_name),
if file_name_str.contains("256x256") || file_name_str.contains("1024x1024") { 1.0 => format!("{}-thumb-512x293.png", subfolder_name),
println!("sdfasdf"); 1.5 => format!("{}-thumb-1024x585.png", subfolder_name),
continue; _ => continue,
} };
} else if size == 1.5 {
if file_name_str.contains("256x256") || file_name_str.contains("512x512") { final_path.push(file_name);
continue; println!("{}", final_path.as_os_str().to_str().unwrap());
image_file_paths.push(final_path);
} }
} }
image_file_paths.push(file_path.clone());
} }
for (i, file_path) in image_file_paths.iter().enumerate() { for (i, file_path) in image_file_paths.iter().enumerate() {
if file_path.is_file() {
if i < start_index as usize { if i < start_index as usize {
continue; continue;
} }
if file_path.is_file() {
let file = std::fs::File::open(file_path)?; let file = std::fs::File::open(file_path)?;
let mut bufreader = std::io::BufReader::new(&file); let mut bufreader = std::io::BufReader::new(&file);
let exifreader = exif::Reader::new(); let exifreader = exif::Reader::new();
let exif = exifreader.read_from_container(&mut bufreader)?; let exif = match exifreader.read_from_container(&mut bufreader) {
for f in exif.fields() { Ok(exif) => Some(exif),
println!( Err(_) => None, // Handle cases where EXIF data isn't available
"{} {} {}", };
f.tag,
f.ifd_num,
f.display_value().with_unit(&exif)
);
}
let file_name = file_path.file_name().unwrap_or_default(); let file_name = file_path.file_name().unwrap_or_default();
let file_name_str = file_name.to_str().unwrap(); let file_name_str = file_name.to_str().unwrap_or_default();
// Check if the file has a supported image extension
if let Some(extension) = file_path.extension() { if let Some(extension) = file_path.extension() {
if let Some(extension_str) = extension.to_str() { if let Some(extension_str) = extension.to_str() {
if image::ImageFormat::from_extension(extension_str).is_some() { if image::ImageFormat::from_extension(extension_str).is_some() {
// Process the image // Re-create the BufReader for image processing since it may have been consumed
let img = image::open(&file_path).expect("Failed to open image"); let img = image::open(file_path).expect("Failed to open image");
let (x, y) = img.dimensions(); let (x, y) = img.dimensions();
pics.push(PicStruct { pics.push(PicStruct {
key: file_name_str.to_string(), // Could not be unique if two images were created at exactly key: file_name_str.to_string(),
// the same time
width: x as i32, width: x as i32,
height: y as i32, height: y as i32,
url: file_name.to_string_lossy().into(), url: file_name.to_string_lossy().into(),
}); });
if i + 1 >= end_index as usize { if i + 1 >= end_index as usize {
break; break;
} }
@ -255,69 +242,6 @@ pub async fn get_pics2(page: i32, size: f64) -> Result<Vec<PicStruct>, ServerFnE
} }
} }
// Send different picture resolutions depending on selected size
// sizes 256 512 1024
// Iterate over the entries in the folder
/*for (index, entry) in fs::read_dir(folder_path)
.expect("Failed to read directory")
.enumerate()
{
if let Ok(entry) = entry {
if index < start_index as usize {
// Skip pictures before the start index
continue;
}
let file_path = entry.path();
let file_name = file_path.file_name().unwrap_or_default();
let file_name_str = file_name.to_str().unwrap();
// Skip the wrong sizes
if size == 0.5 {
//skip 1024 and 512
if file_name_str.contains("512x512") || file_name_str.contains("1024x1024") {
continue;
}
} else if size == 1.0 {
if file_name_str.contains("256x256") || file_name_str.contains("1024x1024") {
continue;
}
} else if size == 1.5 {
if file_name_str.contains("256x256") || file_name_str.contains("512x512") {
continue;
}
}
// Check if the entry is a file
if file_path.is_file() {
// Check if the file has a supported image extension
if let Some(extension) = file_path.extension() {
if let Some(extension_str) = extension.to_str() {
if image::ImageFormat::from_extension(extension_str).is_some() {
// Process the image
let img = image::open(&file_path).expect("Failed to open image");
let (x, y) = img.dimensions();
pics.push(PicStruct {
key: file_name_str.to_string(), // Could not be unique if two images were created at exactly
// the same time
width: x as i32,
height: y as i32,
url: file_name.to_string_lossy().into(),
});
// Break if we have reached the end index
if index + 1 >= end_index as usize {
break;
}
}
}
}
}
}
}*/
println!("{:#?}", pics);
Ok(pics) Ok(pics)
} }