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 { pub struct Config {
/// Must start and end with `/` /// Must start and end with `/`
pub base_url: String, pub base_url: String,
@ -6,6 +7,7 @@ pub struct Config {
pub mapper_threads: usize, pub mapper_threads: usize,
pub minetestmapper_args: Vec<String>, pub minetestmapper_args: Vec<String>,
pub minetestmapper_path: String, pub minetestmapper_path: String,
/// Should end with `/`
pub output_path: String, pub output_path: String,
pub tile_size: usize, pub tile_size: usize,
pub world_path: String, pub world_path: String,

View File

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