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
|
// 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;
#[macro_use]
extern crate rocket_contrib;
#[macro_use]
extern crate serde_derive;
mod database;
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(tb: State<Mutex<TagDatabase>>) -> &'static str {
"Hello, world!"
}
#[derive(Serialize)]
struct ImageResult {
filename: String,
tags: Vec<String>,
}
#[get("/posts?<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(),
)
}
/// 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();
let tb_path = bin.parent().unwrap().join("birka.db");
let tb = TagDatabase::new(tb_path.to_str().unwrap()).unwrap();
for image in tb.images_by_tags(&vec![][..]).unwrap() {
let src = format!("{}/{}", image.orig_dir, image.filename);
let dst = format!("{}/{}", image_dir.to_str().unwrap(), image.filename);
symlink(&src, &dst).ok();
let stem = Path::new(&dst).file_stem().unwrap().to_str().unwrap();
let thumb_path = format!("{}_thumb.png", stem);
let thumb_path = Path::new(&thumb_path);
if !thumb_path.exists() {
let im = image::open(&Path::new(&src)).unwrap();
let fout = &mut File::create(&thumb_path).unwrap();
im.thumbnail(100, 100)
.write_to(fout, ImageFormat::Png)
.unwrap();
}
}
tb
}
fn main() {
let tb = create_image_directory();
rocket::ignite()
.manage(Mutex::new(tb))
.mount("/", routes![index, posts])
.launch();
}
|