2022-02-21 15:58:37 +01:00
|
|
|
#![feature(try_blocks)]
|
|
|
|
|
|
|
|
mod config;
|
|
|
|
|
2022-02-21 17:28:09 +01:00
|
|
|
use crossbeam_channel::Sender;
|
|
|
|
use log::{debug, error, warn};
|
2022-02-21 15:58:37 +01:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use std::{collections::HashMap, io::Read, sync::Arc, time::SystemTime};
|
|
|
|
use tide::Request;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Error {
|
2022-02-21 19:15:14 +01:00
|
|
|
Image(image::ImageError),
|
2022-02-21 15:58:37 +01:00
|
|
|
Io(std::io::Error),
|
|
|
|
Minetestmapper,
|
|
|
|
Todo,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::main]
|
|
|
|
async fn main() -> tide::Result<()> {
|
|
|
|
simplelog::TermLogger::init(
|
|
|
|
simplelog::LevelFilter::Info,
|
|
|
|
simplelog::Config::default(),
|
|
|
|
simplelog::TerminalMode::Mixed,
|
|
|
|
simplelog::ColorChoice::Auto,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let config = config::Config::default();
|
|
|
|
assert!(
|
|
|
|
config.base_url.starts_with('/') && config.base_url.ends_with('/'),
|
|
|
|
"`base_url` must start and end with `/`"
|
|
|
|
);
|
|
|
|
let config = Arc::new(config);
|
|
|
|
|
|
|
|
let tasks = Arc::new(RwLock::new(HashMap::new()));
|
|
|
|
|
|
|
|
let mut app = tide::new();
|
|
|
|
app.at(&format!("{}:z/:x/:y", config.base_url)).get({
|
|
|
|
let config = config.clone();
|
|
|
|
let tasks = tasks.clone();
|
|
|
|
move |req| req_tile(req, tasks.clone(), config.clone())
|
|
|
|
});
|
|
|
|
app.listen(&config.listen).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_minetestmapper(
|
|
|
|
z: i32,
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
2022-02-21 17:28:09 +01:00
|
|
|
tile_dir: &str,
|
2022-02-21 15:58:37 +01:00
|
|
|
tile_path: &str,
|
|
|
|
config: Arc<config::Config>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
debug!("Generating tile ({},{},{})", z, x, y);
|
2022-02-21 17:28:09 +01:00
|
|
|
std::fs::create_dir_all(tile_dir).map_err(|e| {
|
|
|
|
error!("Generating tile ({},{},{}): {}", z, x, y, e);
|
|
|
|
Error::Io(e)
|
|
|
|
})?;
|
2022-02-21 15:58:37 +01:00
|
|
|
std::process::Command::new(&config.minetestmapper_path)
|
|
|
|
.args([
|
|
|
|
"-i",
|
|
|
|
&config.world_path,
|
|
|
|
"-o",
|
|
|
|
tile_path,
|
|
|
|
"--geometry",
|
|
|
|
&format!(
|
|
|
|
"{}:{}+{}+{}",
|
|
|
|
x * config.tile_size as i32,
|
2022-02-21 18:00:48 +01:00
|
|
|
-1 - y * config.tile_size as i32,
|
2022-02-21 15:58:37 +01:00
|
|
|
config.tile_size,
|
|
|
|
config.tile_size
|
|
|
|
),
|
|
|
|
])
|
|
|
|
.args(&config.minetestmapper_args)
|
|
|
|
.output()
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("Generating tile ({},{},{}): {}", z, x, y, e);
|
|
|
|
Error::Minetestmapper
|
|
|
|
})?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_tile_file(tile_path: &str) -> Result<Vec<u8>, Error> {
|
|
|
|
let mut tile_content = Vec::new();
|
|
|
|
std::fs::File::open(&tile_path)
|
|
|
|
.map_err(Error::Io)?
|
2022-02-21 18:00:48 +01:00
|
|
|
.read_to_end(&mut tile_content)
|
2022-02-21 15:58:37 +01:00
|
|
|
.map_err(Error::Io)?; // TODO resp_tile if error
|
|
|
|
Ok(tile_content)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_tile(
|
|
|
|
z: i32,
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
2022-02-21 17:28:09 +01:00
|
|
|
tile_dir: &str,
|
2022-02-21 15:58:37 +01:00
|
|
|
tile_path: &str,
|
|
|
|
tasks: Arc<RwLock<HashMap<(i32, i32, i32), Sender<()>>>>,
|
|
|
|
config: Arc<config::Config>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let mut generate = true;
|
|
|
|
loop {
|
|
|
|
let mut tasks_guard = tasks.write();
|
|
|
|
if let Some(sender) = tasks_guard.get(&(z, x, y)) {
|
|
|
|
let sender = sender.clone();
|
|
|
|
drop(tasks_guard);
|
|
|
|
generate = false;
|
|
|
|
sender.send(()).ok();
|
|
|
|
} else {
|
|
|
|
if generate {
|
|
|
|
let (sender, receiver) = crossbeam_channel::bounded(0);
|
|
|
|
tasks_guard.insert((z, x, y), sender);
|
|
|
|
drop(tasks_guard);
|
|
|
|
|
|
|
|
let res: Result<(), Error> = try {
|
2022-02-21 19:15:14 +01:00
|
|
|
match z.cmp(&config.zoom_default) {
|
|
|
|
std::cmp::Ordering::Equal => {
|
|
|
|
run_minetestmapper(z, x, y, tile_dir, tile_path, config)?
|
|
|
|
}
|
|
|
|
std::cmp::Ordering::Less => {
|
2022-02-21 19:34:30 +01:00
|
|
|
let mut tile = image::ImageBuffer::<image::Rgb<u8>, Vec<u8>>::new(
|
|
|
|
config.tile_size as u32 * 2,
|
|
|
|
config.tile_size as u32 * 2,
|
|
|
|
);
|
|
|
|
if let Ok(ntile_path) =
|
|
|
|
get_dep_tile(z + 1, x * 2, y * 2, tasks.clone(), config.clone())
|
|
|
|
{
|
|
|
|
if let Ok(ntile) = image::open(ntile_path) {
|
|
|
|
image::imageops::replace(&mut tile, &ntile.into_rgb8(), 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Ok(ntile_path) =
|
|
|
|
get_dep_tile(z + 1, x * 2, y * 2 + 1, tasks.clone(), config.clone())
|
|
|
|
{
|
|
|
|
if let Ok(ntile) = image::open(ntile_path) {
|
|
|
|
image::imageops::replace(
|
|
|
|
&mut tile,
|
|
|
|
&ntile.into_rgb8(),
|
|
|
|
0,
|
|
|
|
config.tile_size as i64,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Ok(ntile_path) =
|
|
|
|
get_dep_tile(z + 1, x * 2 + 1, y * 2, tasks.clone(), config.clone())
|
|
|
|
{
|
|
|
|
if let Ok(ntile) = image::open(ntile_path) {
|
|
|
|
image::imageops::replace(
|
|
|
|
&mut tile,
|
|
|
|
&ntile.into_rgb8(),
|
|
|
|
config.tile_size as i64,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Ok(ntile_path) = get_dep_tile(
|
2022-02-21 19:15:14 +01:00
|
|
|
z + 1,
|
|
|
|
x * 2 + 1,
|
|
|
|
y * 2 + 1,
|
|
|
|
tasks.clone(),
|
|
|
|
config.clone(),
|
2022-02-21 19:34:30 +01:00
|
|
|
) {
|
|
|
|
if let Ok(ntile) = image::open(ntile_path) {
|
|
|
|
image::imageops::replace(
|
|
|
|
&mut tile,
|
|
|
|
&ntile.into_rgb8(),
|
|
|
|
config.tile_size as i64,
|
|
|
|
config.tile_size as i64,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
image::imageops::resize(
|
|
|
|
&tile,
|
|
|
|
config.tile_size as u32,
|
|
|
|
config.tile_size as u32,
|
|
|
|
image::imageops::Triangle,
|
|
|
|
)
|
|
|
|
.save(tile_path)
|
|
|
|
.map_err(Error::Image)?;
|
2022-02-21 19:15:14 +01:00
|
|
|
}
|
|
|
|
std::cmp::Ordering::Greater => Err(Error::Todo)?,
|
2022-02-21 15:58:37 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
tasks.write().remove(&(z, x, y));
|
|
|
|
receiver.recv().ok();
|
|
|
|
res?;
|
|
|
|
} else {
|
|
|
|
drop(tasks_guard);
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 19:15:14 +01:00
|
|
|
fn get_dep_tile(
|
|
|
|
z: i32,
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
tasks: Arc<RwLock<HashMap<(i32, i32, i32), Sender<()>>>>,
|
|
|
|
config: Arc<config::Config>,
|
|
|
|
) -> Result<String, Error> {
|
|
|
|
let tile_dir = format!("{}{}/{}", config.output_path, z, x);
|
|
|
|
let tile_path = format!("{}/{}.png", tile_dir, y);
|
|
|
|
|
|
|
|
match std::fs::metadata(&tile_path) {
|
|
|
|
Ok(metadata) => match metadata.modified() {
|
|
|
|
Ok(modified_time) => {
|
|
|
|
if SystemTime::now()
|
|
|
|
.duration_since(modified_time)
|
|
|
|
.unwrap()
|
|
|
|
.as_secs() > config.cache_age
|
|
|
|
{
|
|
|
|
generate_tile(z, x, y, &tile_dir, &tile_path, tasks, config)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Cannot get modified time of `{}`: {}", tile_path, e);
|
|
|
|
generate_tile(z, x, y, &tile_dir, &tile_path, tasks, config)?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
debug!("Tile not found `{}`", tile_path);
|
|
|
|
} else {
|
|
|
|
warn!("Cannot get metadata of `{}`: {}", tile_path, e);
|
|
|
|
}
|
|
|
|
generate_tile(z, x, y, &tile_dir, &tile_path, tasks, config)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(tile_path)
|
|
|
|
}
|
|
|
|
|
2022-02-21 15:58:37 +01:00
|
|
|
fn resp_tile(
|
|
|
|
z: i32,
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
2022-02-21 17:28:09 +01:00
|
|
|
tile_dir: &str,
|
2022-02-21 15:58:37 +01:00
|
|
|
tile_path: &str,
|
|
|
|
tasks: Arc<RwLock<HashMap<(i32, i32, i32), Sender<()>>>>,
|
|
|
|
config: Arc<config::Config>,
|
|
|
|
) -> Result<Vec<u8>, Error> {
|
2022-02-21 17:28:09 +01:00
|
|
|
generate_tile(z, x, y, tile_dir, tile_path, tasks, config)?;
|
2022-02-21 15:58:37 +01:00
|
|
|
read_tile_file(tile_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn req_tile(
|
|
|
|
req: Request<()>,
|
|
|
|
tasks: Arc<RwLock<HashMap<(i32, i32, i32), Sender<()>>>>,
|
|
|
|
config: Arc<config::Config>,
|
|
|
|
) -> tide::Result {
|
|
|
|
let z: i32 = req.param("z")?.parse()?;
|
2022-02-21 19:15:14 +01:00
|
|
|
|
|
|
|
if z > config.zoom_max || z < config.zoom_min {
|
|
|
|
return Ok(tide::StatusCode::Forbidden.into());
|
|
|
|
}
|
|
|
|
|
2022-02-21 15:58:37 +01:00
|
|
|
let x: i32 = req.param("x")?.parse()?;
|
2022-02-21 18:00:48 +01:00
|
|
|
let y = req.param("y")?;
|
|
|
|
let y = y.strip_suffix(".png").unwrap_or(y).parse()?;
|
2022-02-21 15:58:37 +01:00
|
|
|
|
2022-02-21 17:28:09 +01:00
|
|
|
let tile_dir = format!("{}{}/{}", config.output_path, z, x);
|
|
|
|
let tile_path = format!("{}/{}.png", tile_dir, y);
|
2022-02-21 15:58:37 +01:00
|
|
|
|
|
|
|
match std::fs::metadata(&tile_path) {
|
|
|
|
Ok(metadata) => match metadata.modified() {
|
|
|
|
Ok(modified_time) => {
|
|
|
|
if SystemTime::now()
|
|
|
|
.duration_since(modified_time)
|
|
|
|
.unwrap()
|
|
|
|
.as_secs() > config.cache_age
|
|
|
|
{
|
2022-02-21 17:28:09 +01:00
|
|
|
match resp_tile(z, x, y, &tile_dir, &tile_path, tasks, config) {
|
2022-02-21 15:58:37 +01:00
|
|
|
Ok(tile_content) => Ok(tide::Response::builder(200)
|
|
|
|
.content_type(http_types::mime::PNG)
|
|
|
|
.body(tile_content)
|
|
|
|
.build()),
|
|
|
|
Err(_) => Ok(tide::StatusCode::InternalServerError.into()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match read_tile_file(&tile_path) {
|
|
|
|
Ok(tile_content) => Ok(tide::Response::builder(200)
|
|
|
|
.content_type(http_types::mime::PNG)
|
|
|
|
.body(tile_content)
|
|
|
|
.build()),
|
|
|
|
Err(_) => Ok(tide::StatusCode::InternalServerError.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Cannot get modified time of `{}`: {}", tile_path, e);
|
2022-02-21 17:28:09 +01:00
|
|
|
match resp_tile(z, x, y, &tile_dir, &tile_path, tasks, config) {
|
2022-02-21 15:58:37 +01:00
|
|
|
Ok(tile_content) => Ok(tide::Response::builder(200)
|
|
|
|
.content_type(http_types::mime::PNG)
|
|
|
|
.body(tile_content)
|
|
|
|
.build()),
|
|
|
|
Err(_) => Ok(tide::StatusCode::InternalServerError.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
debug!("Tile not found `{}`", tile_path);
|
|
|
|
} else {
|
|
|
|
warn!("Cannot get metadata of `{}`: {}", tile_path, e);
|
|
|
|
}
|
2022-02-21 17:28:09 +01:00
|
|
|
match resp_tile(z, x, y, &tile_dir, &tile_path, tasks, config) {
|
2022-02-21 15:58:37 +01:00
|
|
|
Ok(tile_content) => Ok(tide::Response::builder(200)
|
|
|
|
.content_type(http_types::mime::PNG)
|
|
|
|
.body(tile_content)
|
|
|
|
.build()),
|
|
|
|
Err(_) => Ok(tide::StatusCode::InternalServerError.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|