diff --git a/server/service/src/main.rs b/server/service/src/main.rs index 9bbfbe6..51caf02 100644 --- a/server/service/src/main.rs +++ b/server/service/src/main.rs @@ -28,7 +28,7 @@ enum SortKey { Size, Seeders, Leechers, - Date, + Scraped, } impl Default for SortKey { @@ -44,7 +44,7 @@ impl fmt::Display for SortKey { Self::Size => "size_bytes", Self::Seeders => "seeders", Self::Leechers => "leechers", - Self::Date => "scraped_date", + Self::Scraped => "scraped_date", }; write!(f, "{}", s) } @@ -190,7 +190,7 @@ fn search_query( ); let res = if type_ == "file" { - let results = torrent_file_search(conn, q, size, offset)?; + let results = torrent_file_search(conn, q, sort_key, sort_dir, size, offset)?; serde_json::to_value(&results).unwrap() } else { let results = torrent_search(conn, q, sort_key, sort_dir, size, offset)?; @@ -329,10 +329,13 @@ struct File { fn torrent_file_search( conn: r2d2::PooledConnection, query: &str, + sort_key: SortKey, + sort_dir: SortDirection, size: usize, offset: usize, ) -> Result, Error> { - let stmt_str = "select * from files where path like '%' || ?1 || '%' limit ?2, ?3"; + // `sort_key` and `sort_dir` are already sanitized and should not be escaped: + let stmt_str = format!("select * from files where path like '%' || ?1 || '%' order by {} {} limit ?2, ?3", sort_key, sort_dir); let mut stmt = conn.prepare(&stmt_str).unwrap(); let file_iter = stmt.query_map( params![ @@ -405,7 +408,7 @@ mod tests { let manager = SqliteConnectionManager::file(super::torrents_db_file()); let pool = r2d2::Pool::builder().max_size(15).build(manager).unwrap(); let conn = pool.get().unwrap(); - let results = super::torrent_search(conn, "sherlock", 10, 0, SortKey::Name, SortDirection::Desc); + let results = super::torrent_search(conn, "sherlock", super::SortKey::Name, super::SortDirection::Desc, 10, 0); assert!(results.unwrap().len() > 2); // println!("Query took {:?} seconds.", end - start); } diff --git a/server/ui/src/components/search.tsx b/server/ui/src/components/search.tsx index 57b16cf..24326cd 100644 --- a/server/ui/src/components/search.tsx +++ b/server/ui/src/components/search.tsx @@ -40,7 +40,7 @@ class SortableLink extends Component { if (searchParams.sort_dir === "Asc") { return ; } - return l + return ; } ) @@ -163,7 +163,7 @@ export class Search extends Component { - +