summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: d53d0d00ddde7e559ef9b5969cc80bfa0475a9e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright © 2020 Jakob L. Kreuze <zerodaysfordays@sdf.org>
//
// This file is part of бирка-тян.
//
// бирка-тян is free software; you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// бирка-тян is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with бирка-тян. If not, see <http://www.gnu.org/licenses/>.

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use]
extern crate anyhow;
extern crate image;
#[macro_use]
extern crate rocket;
extern crate rocket_contrib;
#[macro_use]
extern crate serde_derive;

mod database;

use anyhow::Result;
use database::{hash_file, Query, TagDatabase};
use image::ImageFormat;
use rocket::{Data, State};
use rocket_contrib::json::Json;
use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template;
use std::env;
use std::fs;
use std::fs::File;
use std::os::unix::fs::symlink;
use std::path::Path;
use std::sync::Mutex;

/// Maximum number of results returned by any API endpoint.
const RESULTS_PER_QUERY: i64 = 50;

/// Shorthand for the state that is passed to all handlers.
type SiteState<'a> = State<'a, Mutex<DatabaseConnection>>;

/// Collection of data necessary to serve images from the tag database.
struct DatabaseConnection {
    tb: TagDatabase,
    image_dir: String,
}

impl DatabaseConnection {
    fn new() -> Result<Self> {
        let bin = env::current_exe()?.canonicalize()?;
        let bin_dir = bin.parent().unwrap();
        let image_dir = String::from(bin_dir.join("image/").to_str().unwrap());
        let tb_path = bin_dir.join("birka.db");
        let tb = TagDatabase::new(tb_path.to_str().unwrap())?;
        Ok(DatabaseConnection { tb, image_dir })
    }
}

/// Information about an image, as returned by the 'posts' endpoint.
#[derive(Serialize)]
struct ImageResult {
    id: i64,
    filename: String,
    thumb_filename: String,
    tags: Vec<String>,
}

/// Return the file name of the thumbnail for the image at `filename`.
fn thumb_filename(filename: &str) -> String {
    String::from(format!(
        "{}_thumb.png",
        // Strip extension.
        if let Some(n) = filename.rfind('.') {
            &filename[..n]
        } else {
            &filename[..]
        }
    ))
}

#[get("/posts?<tags>&<last>")]
fn get_posts(conn: SiteState, tags: String, last: Option<i64>) -> Json<Vec<ImageResult>> {
    let tb = &conn.inner().lock().unwrap().tb;

    // Ensure that an empty vector is passed to `tb.query` if no tags were
    // specified.
    let tags = if !tags.is_empty() {
        tags.split(",").collect::<Vec<&str>>()
    } else {
        vec![]
    };
    let results = tb
        .query(Query {
            tags: &tags[..],
            last_id: last,
            limit: Some(RESULTS_PER_QUERY),
        })
        .unwrap();

    Json(
        results
            .iter()
            .map(|image| ImageResult {
                id: image.id,
                filename: image.filename.clone(),
                thumb_filename: thumb_filename(&image.filename),
                tags: tb.tags_for_image(image.id).unwrap(),
            })
            .collect(),
    )
}

#[post("/posts/<filename>", data = "<data>")]
fn put_posts(conn: SiteState, filename: String, data: Data) -> Json<&str> {
    let conn = conn.inner().lock().unwrap();
    let path = Path::new(&conn.image_dir).join(filename);
    data.stream_to_file(&path).unwrap();
    conn.tb
        .import_image(&path, &hash_file(&path).unwrap())
        .unwrap();

    Json("uploaded!")
}

#[get("/")]
fn index(conn: SiteState) -> Template {
    display_posts(conn, String::from(""), None)
}

#[get("/posts?<tags>&<last>")]
fn display_posts(conn: SiteState, tags: String, last: Option<i64>) -> Template {
    #[derive(Serialize)]
    struct TagResult {
        name: String,
        count: i64,
    }

    #[derive(Serialize)]
    struct Context {
        tags: Vec<TagResult>,
        images: Vec<ImageResult>,
        next_page: String,
    }

    let base_params = format!("/posts?tags={}", tags);
    let tag_results = {
        // This is done in a block because `tb` needs to go out of scope before
        // calling out to `get_posts` to perform the query. Otherwise, we
        // deadlock because the mutex is locked for the entirety of
        // `display_posts`.
        let tb = &conn.inner().lock().unwrap().tb;
        tb.top_tags(20)
            .unwrap()
            .iter()
            .map(|tup| TagResult {
                name: tup.1.clone(),
                count: tup.0,
            })
            .collect()
    };
    let images = get_posts(conn, tags, last).into_inner();
    let context = Context {
        tags: tag_results,
        next_page: if images.len() as i64 == RESULTS_PER_QUERY {
            let last_image = images.last().unwrap();
            format!("{}&last={}", base_params, last_image.id)
        } else {
            String::from("")
        },
        images,
    };
    Template::render("index", context)
}

/// Maybe initialize the directory for storing image symlinks and thumbnails.
fn create_image_directory(conn: &DatabaseConnection) {
    fs::create_dir(&conn.image_dir).ok();

    for image in conn.tb.images_by_tags(&vec![][..]).unwrap() {
        let src = Path::new(&image.orig_dir).join(&image.filename);
        let dst = Path::new(&conn.image_dir).join(&image.filename);
        symlink(&src, &dst).ok();

        let stem = dst.file_stem().unwrap().to_str().unwrap();
        let thumb_path = Path::new(&conn.image_dir).join(&thumb_filename(&stem));
        if !thumb_path.exists() {
            let im = image::open(&Path::new(&src)).unwrap();
            let out = &mut File::create(&thumb_path).unwrap();
            im.thumbnail(100, 100)
                .write_to(out, ImageFormat::Png)
                .unwrap();
        }
    }
}

fn main() {
    let conn = DatabaseConnection::new().unwrap();
    create_image_directory(&conn);
    rocket::ignite()
        .mount("/public", StaticFiles::from("./static/"))
        .mount("/image", StaticFiles::from(&conn.image_dir))
        .mount("/", routes![index, display_posts])
        .mount("/api", routes![get_posts, put_posts])
        .attach(Template::fairing())
        .manage(Mutex::new(conn))
        .launch();
}