Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added memories! #222

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
"axios": "^1.7.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"framer-motion": "^11.16.3",
"ldrs": "^1.0.2",
"lucide-react": "^0.400.0",
"react": "^18.2.0",
"react-colorful": "^5.6.1",
"react-dnd": "^16.0.1",
"react-dnd-html5-backend": "^16.0.1",
"react-dom": "^18.2.0",
"react-image-crop": "^11.0.7",
"react-icons": "^5.4.0",
"react-image-crop": "^11.0.7",
"react-router-dom": "^6.24.1",
"tailwind-merge": "^2.3.0",
"tailwindcss-animate": "^1.0.7"
Expand Down
3 changes: 3 additions & 0 deletions frontend/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion frontend/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ tauri-build = { version = "2.0.0-beta", features = [] }
tauri = { version = "2.0.0-beta", features = [ "protocol-asset"] }
walkdir = "2.3"
serde_json = "1"
serde = { version = "1.0", features = ["derive"] }
anyhow = "1.0"
tauri-plugin-fs = "2.0.0-beta.7"
tauri-plugin-shell = "2.0.0-beta.5"
tauri-plugin-dialog = "2.0.0-beta.9"
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
chrono = "0.4"
chrono = { version = "0.4.26", features = ["serde"] }
image = "0.24.6"
base64 = "0.21.0"
rand = "0.8.5"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
Expand Down
1 change: 1 addition & 0 deletions frontend/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn main() {
services::delete_cache,
services::share_file,
services::save_edited_image,
services::get_random_memories,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
73 changes: 73 additions & 0 deletions frontend/src-tauri/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ pub use cache_service::CacheService;
use chrono::{DateTime, Datelike, Utc};
pub use file_service::FileService;
use image::{DynamicImage, GenericImageView, ImageBuffer, Rgba};
use rand::seq::SliceRandom;
use std::collections::HashSet;
use serde::{Serialize, Deserialize};
use std::path::Path;

#[derive(Debug, Serialize, Deserialize)]
pub struct MemoryImage {
path: String,
#[serde(with = "chrono::serde::ts_seconds")]
created_at: DateTime<Utc>,
}

#[tauri::command]
pub fn get_folders_with_images(
Expand Down Expand Up @@ -285,6 +296,68 @@ fn adjust_brightness_contrast(img: &DynamicImage, brightness: i32, contrast: i32
DynamicImage::ImageRgba8(adjusted_img)
}

#[tauri::command]
pub fn get_random_memories(directories: Vec<String>, count: usize) -> Result<Vec<MemoryImage>, String> {
let mut all_images = Vec::new();
let mut used_paths = HashSet::new();

for dir in directories {
let images = get_images_from_directory(&dir)?;
all_images.extend(images);
}

let mut rng = rand::thread_rng();
all_images.shuffle(&mut rng);

let selected_images = all_images
.into_iter()
.filter(|img| used_paths.insert(img.path.clone()))
.take(count)
.collect();

Ok(selected_images)
}

fn get_images_from_directory(dir: &str) -> Result<Vec<MemoryImage>, String> {
let path = Path::new(dir);
if !path.is_dir() {
return Err(format!("{} is not a directory", dir));
}

let mut images = Vec::new();

for entry in std::fs::read_dir(path).map_err(|e| e.to_string())? {
let entry = entry.map_err(|e| e.to_string())?;
let path = entry.path();

if path.is_dir() {
// Recursively call get_images_from_directory for subdirectories
let sub_images = get_images_from_directory(path.to_str().unwrap())?;
images.extend(sub_images);
} else if path.is_file() && is_image_file(&path) {
if let Ok(metadata) = std::fs::metadata(&path) {
if let Ok(created) = metadata.created() {
let created_at: DateTime<Utc> = created.into();
images.push(MemoryImage {
path: path.to_string_lossy().into_owned(),
created_at,
});
}
}
}
}

Ok(images)
}

fn is_image_file(path: &Path) -> bool {
let extensions = ["jpg", "jpeg", "png", "gif"];
path.extension()
.and_then(|ext| ext.to_str())
.map(|ext| extensions.contains(&ext.to_lowercase().as_str()))
.unwrap_or(false)
}

#[tauri::command]
pub fn delete_cache(cache_service: State<'_, CacheService>) -> bool {
cache_service.delete_all_caches()
Expand Down
Loading