summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/database.rs38
-rw-r--r--src/main.rs50
2 files changed, 61 insertions, 27 deletions
diff --git a/src/database.rs b/src/database.rs
index d27646b..7b69a21 100644
--- a/src/database.rs
+++ b/src/database.rs
@@ -26,9 +26,9 @@ pub struct TagDatabase(Connection);
/// Tag query with a sense of "state" for pagination.
#[derive(Debug)]
pub struct Query<'a> {
- tags: &'a [&'a str],
- last_id: Option<i64>,
- limit: Option<i64>,
+ pub tags: &'a [&'a str],
+ pub last_id: Option<i64>,
+ pub limit: Option<i64>,
}
impl TagDatabase {
@@ -144,6 +144,22 @@ impl TagDatabase {
Ok(())
}
+ /// Return the tags for the image specified by `image_id`.
+ pub fn tags_for_image(&self, image_id: i64) -> Result<Vec<String>> {
+ Ok(self
+ .0
+ .prepare(
+ "SELECT tags.name, COUNT(mapping.tag) as tag_count
+ FROM mapping
+ INNER JOIN images ON images.id = ?
+ INNER JOIN tags
+ GROUP BY tags.name;",
+ )?
+ .query_map(params![image_id], |row| row.get(0))?
+ .filter_map(|tag| tag.ok())
+ .collect())
+ }
+
/// Return the intersection of the sets of images matching each of `tags`.
pub fn query(&self, q: Query) -> Result<Vec<Image>> {
let ids: Vec<i64> = q
@@ -257,22 +273,6 @@ mod tests {
tb.initialize_tables()?;
Ok(tb)
}
-
- /// Return the tags for the image specified by `image_id`.
- pub fn tags_for_image(&self, image_id: i64) -> Result<Vec<String>> {
- Ok(self
- .0
- .prepare(
- "SELECT tags.name, COUNT(mapping.tag) as tag_count
- FROM mapping
- INNER JOIN images ON images.id = ?
- INNER JOIN tags
- GROUP BY tags.name;",
- )?
- .query_map(params![image_id], |row| row.get(0))?
- .filter_map(|tag| tag.ok())
- .collect())
- }
}
#[test]
diff --git a/src/main.rs b/src/main.rs
index b5665b9..a4f5946 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -24,30 +24,59 @@ extern crate image;
extern crate rocket;
#[macro_use]
extern crate rocket_contrib;
+#[macro_use]
+extern crate serde_derive;
mod database;
-use database::TagDatabase;
+use database::{Query, TagDatabase};
use image::{GenericImageView, ImageFormat};
+use rocket::State;
use rocket_contrib::json::{Json, JsonValue};
use std::env;
use std::fs;
use std::fs::File;
use std::os::unix::fs::symlink;
use std::path::Path;
+use std::sync::Mutex;
#[get("/")]
-fn index() -> &'static str {
+fn index(tb: State<Mutex<TagDatabase>>) -> &'static str {
"Hello, world!"
}
+#[derive(Serialize)]
+struct ImageResult {
+ filename: String,
+ tags: Vec<String>,
+}
+
#[get("/posts?<tags>")]
-fn posts(tags: String) -> Json<Vec<String>> {
- let tags: Vec<String> = tags.split(",").map(|s| s.into()).collect();
- Json(tags)
+fn posts(tb: State<Mutex<TagDatabase>>, tags: String) -> Json<Vec<ImageResult>> {
+ let tb = tb.inner().lock().unwrap();
+
+ let tags = tags.split(",").collect::<Vec<&str>>();
+ let results = tb
+ .query(Query {
+ tags: &tags[..],
+ last_id: Some(0),
+ limit: Some(50),
+ })
+ .unwrap();
+
+ Json(
+ results
+ .iter()
+ .map(|image| ImageResult {
+ filename: image.filename.clone(),
+ tags: tb.tags_for_image(image.id).unwrap(),
+ })
+ .collect(),
+ )
}
-fn create_image_directory() {
+/// Maybe initialize the directory for storing image symlinks and thumbnails.
+fn create_image_directory() -> TagDatabase {
let bin = env::current_exe().unwrap();
let image_dir = bin.parent().unwrap().join("image/");
fs::create_dir(&image_dir).ok();
@@ -72,9 +101,14 @@ fn create_image_directory() {
.unwrap();
}
}
+
+ tb
}
fn main() {
- create_image_directory();
- rocket::ignite().mount("/", routes![index, posts]).launch();
+ let tb = create_image_directory();
+ rocket::ignite()
+ .manage(Mutex::new(tb))
+ .mount("/", routes![index, posts])
+ .launch();
}