feature/tagging #16
36
src/main.rs
36
src/main.rs
@@ -8,15 +8,15 @@ use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
use futures::stream::StreamExt;
|
||||
use lazy_static::lazy_static;
|
||||
use prometheus::{self, IntGauge};
|
||||
use std::error::Error;
|
||||
use std::sync::mpsc::channel;
|
||||
use std::sync::Mutex;
|
||||
use std::{collections::HashMap, io::prelude::*};
|
||||
use std::{env, fs::File};
|
||||
use std::{
|
||||
io::ErrorKind,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::error::Error;
|
||||
use std::sync::Mutex;
|
||||
use walkdir::{DirEntry, WalkDir};
|
||||
|
||||
use actix_files::NamedFile;
|
||||
@@ -244,8 +244,13 @@ async fn favorites(
|
||||
claims: Claims,
|
||||
favorites_dao: Data<Mutex<Box<dyn FavoriteDao>>>,
|
||||
) -> impl Responder {
|
||||
match web::block(move || favorites_dao.lock()
|
||||
.expect("Unable to get FavoritesDao").get_favorites(claims.sub.parse::<i32>().unwrap())).await
|
||||
match web::block(move || {
|
||||
favorites_dao
|
||||
.lock()
|
||||
.expect("Unable to get FavoritesDao")
|
||||
.get_favorites(claims.sub.parse::<i32>().unwrap())
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(Ok(favorites)) => {
|
||||
let favorites = favorites
|
||||
@@ -275,7 +280,10 @@ async fn put_add_favorite(
|
||||
if let Ok(user_id) = claims.sub.parse::<i32>() {
|
||||
let path = body.path.clone();
|
||||
match web::block::<_, Result<usize, DbError>>(move || {
|
||||
favorites_dao.lock().expect("Unable to get FavoritesDao").add_favorite(user_id, &path)
|
||||
favorites_dao
|
||||
.lock()
|
||||
.expect("Unable to get FavoritesDao")
|
||||
.add_favorite(user_id, &path)
|
||||
})
|
||||
.await
|
||||
{
|
||||
@@ -311,7 +319,10 @@ async fn delete_favorite(
|
||||
if let Ok(user_id) = claims.sub.parse::<i32>() {
|
||||
let path = body.path.clone();
|
||||
web::block(move || {
|
||||
favorites_dao.lock().expect("Unable to get favorites dao").remove_favorite(user_id, path);
|
||||
favorites_dao
|
||||
.lock()
|
||||
.expect("Unable to get favorites dao")
|
||||
.remove_favorite(user_id, path);
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -430,8 +441,7 @@ fn main() -> std::io::Result<()> {
|
||||
dotenv::dotenv().ok();
|
||||
env_logger::init();
|
||||
|
||||
run_migrations(&mut connect())
|
||||
.expect("Failed to run migrations");
|
||||
run_migrations(&mut connect()).expect("Failed to run migrations");
|
||||
|
||||
create_thumbnails();
|
||||
watch_files();
|
||||
@@ -475,13 +485,15 @@ fn main() -> std::io::Result<()> {
|
||||
.service(
|
||||
web::resource("image/tags")
|
||||
.route(web::post().to(add_tag::<SqliteTagDao>))
|
||||
.route(web::get().to(get_all_tags::<SqliteTagDao>))
|
||||
.route(web::get().to(get_tags::<SqliteTagDao>))
|
||||
.route(web::delete().to(remove_tagged_photo::<SqliteTagDao>)),
|
||||
)
|
||||
.service(web::resource("image/tags/all").route(web::get().to(get_all_tags::<SqliteTagDao>)))
|
||||
.app_data(app_data.clone())
|
||||
.app_data::<Data<Mutex<SqliteUserDao>>>(Data::new(Mutex::new(user_dao)))
|
||||
.app_data::<Data<Mutex<Box<dyn FavoriteDao>>>>(Data::new(Mutex::new(Box::new(favorites_dao))))
|
||||
.app_data::<Data<Mutex<Box<dyn FavoriteDao>>>>(Data::new(Mutex::new(Box::new(
|
||||
favorites_dao,
|
||||
))))
|
||||
.app_data::<Data<Mutex<SqliteTagDao>>>(Data::new(Mutex::new(tag_dao)))
|
||||
.wrap(prometheus.clone())
|
||||
})
|
||||
@@ -492,7 +504,9 @@ fn main() -> std::io::Result<()> {
|
||||
})
|
||||
}
|
||||
|
||||
fn run_migrations(connection: &mut impl MigrationHarness<Sqlite>) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
fn run_migrations(
|
||||
connection: &mut impl MigrationHarness<Sqlite>,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
connection.run_pending_migrations(MIGRATIONS)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -3,7 +3,7 @@ use actix_web::{web, HttpResponse, Responder};
|
||||
use anyhow::Context;
|
||||
use chrono::Utc;
|
||||
use diesel::prelude::*;
|
||||
use log::info;
|
||||
use log::{debug, info};
|
||||
use schema::{tagged_photo, tags};
|
||||
use serde::Serialize;
|
||||
use std::borrow::BorrowMut;
|
||||
@@ -132,6 +132,7 @@ impl TagDao for SqliteTagDao {
|
||||
}
|
||||
|
||||
fn get_tags_for_path(&mut self, path: &str) -> anyhow::Result<Vec<Tag>> {
|
||||
debug!("Getting Tags for path: {:?}", path);
|
||||
tags::table
|
||||
.left_join(tagged_photo::table)
|
||||
.filter(tagged_photo::photo_name.eq(&path))
|
||||
@@ -149,6 +150,7 @@ impl TagDao for SqliteTagDao {
|
||||
.execute(&mut self.connection)
|
||||
.with_context(|| "Unable to insert tag in Sqlite")
|
||||
.and_then(|_| {
|
||||
debug!("Inserted tag: {:?}", name);
|
||||
no_arg_sql_function!(
|
||||
last_insert_rowid,
|
||||
diesel::sql_types::Integer,
|
||||
@@ -159,6 +161,7 @@ impl TagDao for SqliteTagDao {
|
||||
.with_context(|| "Unable to get last inserted tag from Sqlite")
|
||||
})
|
||||
.and_then(|id| {
|
||||
debug!("Got id: {:?} for inserted tag: {:?}", id, name);
|
||||
tags::table
|
||||
.left_join(tagged_photo::table)
|
||||
.filter(tagged_photo::id.eq(id))
|
||||
@@ -199,8 +202,9 @@ impl TagDao for SqliteTagDao {
|
||||
created_time: Utc::now().timestamp(),
|
||||
})
|
||||
.execute(self.connection.borrow_mut())
|
||||
.with_context(|| "Unable to insert tag into sqlite")
|
||||
.with_context(|| "Unable to tag file in sqlite")
|
||||
.and_then(|tagged_id| {
|
||||
debug!("Inserted tagged photo: {:?}", tagged_id);
|
||||
tagged_photo::table
|
||||
.find(tagged_id as i32)
|
||||
.first(self.connection.borrow_mut())
|
||||
|
||||
Reference in New Issue
Block a user