mapserver: mkdirs

This commit is contained in:
Pascal Engélibert 2022-02-21 17:28:09 +01:00
parent 2082f7e211
commit 57fcb38e25
Signed by: tuxmain
GPG Key ID: 3504BC6D362F7DCA
2 changed files with 18 additions and 8 deletions

View File

@ -1,3 +1,4 @@
// TODO x,y bounds
pub struct Config {
/// Must start and end with `/`
pub base_url: String,
@ -6,6 +7,7 @@ pub struct Config {
pub mapper_threads: usize,
pub minetestmapper_args: Vec<String>,
pub minetestmapper_path: String,
/// Should end with `/`
pub output_path: String,
pub tile_size: usize,
pub world_path: String,

View File

@ -2,8 +2,8 @@
mod config;
use crossbeam_channel::{Receiver, Sender};
use log::{debug, error, info, warn};
use crossbeam_channel::Sender;
use log::{debug, error, warn};
use parking_lot::RwLock;
use std::{collections::HashMap, io::Read, sync::Arc, time::SystemTime};
use tide::Request;
@ -48,10 +48,15 @@ fn run_minetestmapper(
z: i32,
x: i32,
y: i32,
tile_dir: &str,
tile_path: &str,
config: Arc<config::Config>,
) -> Result<(), Error> {
debug!("Generating tile ({},{},{})", z, x, y);
std::fs::create_dir_all(tile_dir).map_err(|e| {
error!("Generating tile ({},{},{}): {}", z, x, y, e);
Error::Io(e)
})?;
std::process::Command::new(&config.minetestmapper_path)
.args([
"-i",
@ -89,6 +94,7 @@ fn generate_tile(
z: i32,
x: i32,
y: i32,
tile_dir: &str,
tile_path: &str,
tasks: Arc<RwLock<HashMap<(i32, i32, i32), Sender<()>>>>,
config: Arc<config::Config>,
@ -109,7 +115,7 @@ fn generate_tile(
let res: Result<(), Error> = try {
if z == config.zoom_default {
run_minetestmapper(z, x, y, tile_path, config)?;
run_minetestmapper(z, x, y, tile_dir, tile_path, config)?;
} else {
Err(Error::Todo)?;
}
@ -130,11 +136,12 @@ fn resp_tile(
z: i32,
x: i32,
y: i32,
tile_dir: &str,
tile_path: &str,
tasks: Arc<RwLock<HashMap<(i32, i32, i32), Sender<()>>>>,
config: Arc<config::Config>,
) -> Result<Vec<u8>, Error> {
generate_tile(z, x, y, tile_path, tasks, config)?;
generate_tile(z, x, y, tile_dir, tile_path, tasks, config)?;
read_tile_file(tile_path)
}
@ -147,7 +154,8 @@ async fn req_tile(
let x: i32 = req.param("x")?.parse()?;
let y: i32 = req.param("y")?.parse()?;
let tile_path = format!("{}/{}/{}/{}.png", config.output_path, z, x, y);
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() {
@ -157,7 +165,7 @@ async fn req_tile(
.unwrap()
.as_secs() > config.cache_age
{
match resp_tile(z, x, y, &tile_path, tasks, config) {
match resp_tile(z, x, y, &tile_dir, &tile_path, tasks, config) {
Ok(tile_content) => Ok(tide::Response::builder(200)
.content_type(http_types::mime::PNG)
.body(tile_content)
@ -176,7 +184,7 @@ async fn req_tile(
}
Err(e) => {
warn!("Cannot get modified time of `{}`: {}", tile_path, e);
match resp_tile(z, x, y, &tile_path, tasks, config) {
match resp_tile(z, x, y, &tile_dir, &tile_path, tasks, config) {
Ok(tile_content) => Ok(tide::Response::builder(200)
.content_type(http_types::mime::PNG)
.body(tile_content)
@ -191,7 +199,7 @@ async fn req_tile(
} else {
warn!("Cannot get metadata of `{}`: {}", tile_path, e);
}
match resp_tile(z, x, y, &tile_path, tasks, config) {
match resp_tile(z, x, y, &tile_dir, &tile_path, tasks, config) {
Ok(tile_content) => Ok(tide::Response::builder(200)
.content_type(http_types::mime::PNG)
.body(tile_content)