Skip to content

Commit

Permalink
fix: working custom protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Feb 10, 2022
1 parent 11c7883 commit 9e7c051
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 61 deletions.
53 changes: 5 additions & 48 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@ use notify::Watcher;
use regex::Regex;
use serde_json::json;
use std::sync::Mutex;
use std::time::Duration;

use std::env;
use std::fs::File;
use std::io::Write;

use std::path::Path;
use tauri::api::{
path::{resolve_path, BaseDirectory},
shell,
};
use tauri::{CustomMenuItem, Menu, MenuEntry, MenuItem, Submenu, WindowBuilder, WindowUrl};
use tauri::{Manager, State, Window};

use crate::tiles::{get_tile_data, get_tile_details, TileMeta};
use crate::utils::{decode, encode, get_blank_image, get_data_format, DataFormat};
use crate::utils::{decode, get_blank_image, get_data_format, DataFormat};

lazy_static! {
static ref TILE_URL_RE: Regex = Regex::new(
Expand All @@ -50,7 +46,6 @@ struct MBTiles {
#[allow(dead_code)]
static NOT_FOUND: &[u8] = b"Not Found";
static CONTENT_TYPE: &[u8] = b"Content-Type";
static CONTENT_ENCODING: &[u8] = b"Content-Encoding";

// the payload type must implement `Serialize`.
#[derive(serde::Serialize)]
Expand All @@ -69,16 +64,14 @@ fn setup_mbtiles(
app_handle: tauri::AppHandle,
window: Window,
) {
let app_dir = app_handle.path_resolver().app_dir();
let mut data = mbtiles.lock().unwrap();
if !data.metadata.is_none() {
let connection = data.metadata.take().unwrap().connection_pool.get().unwrap();
// let connection = data.metadata.take().unwrap().connection_pool.get().unwrap();
data.metadata = None;
data.path = None;
}
if !path.is_none() {
let c_path = path.clone().unwrap();
println!("c_path {}", c_path);
if c_path.starts_with("asset://") {
data.path = Some(c_path.replace("asset://", ""));
// data.path = match resolve_path(
Expand All @@ -93,7 +86,6 @@ fn setup_mbtiles(
} else {
data.path = Some(path.clone().unwrap());
}
println!("data.ath {}", data.path.as_ref().unwrap());
if !data.path.is_none() {
let file_path = Path::new(data.path.as_ref().unwrap());
data.metadata = match get_tile_details(&file_path) {
Expand All @@ -118,7 +110,6 @@ fn setup_mbtiles(
// }
// }
// }));
println!("data.metadata {}", data.path.as_ref().unwrap());
window
.emit(
"mbtiles",
Expand Down Expand Up @@ -151,11 +142,6 @@ fn main() {
.min_inner_size(400.0, 200.0)
.skip_taskbar(false)
.fullscreen(false);

// if let Err(e) = server::run(win) {
// error!("Server error: {}", e);
// std::process::exit(1);
// }
return (win, webview);
})
.menu(Menu::with_items([
Expand Down Expand Up @@ -230,9 +216,6 @@ fn main() {
return response.status(404).body(NOT_FOUND.into());
}
let tile_meta = data.metadata.as_ref().unwrap();

println!("request.uri() {}", request.uri());

match TILE_URL_RE.captures(request.uri()) {
Some(matches) => {
// let tile_path = matches.name("tile_path").unwrap().as_str();
Expand All @@ -245,40 +228,19 @@ fn main() {
"pbf" => match get_tile_data(&tile_meta.connection_pool.get().unwrap(), z, x, y) {
Ok(data) => {
let data_in_format = get_data_format(&data);

// // Create a temporary file.
// let temp_directory = env::temp_dir();
// let temp_file = temp_directory.join(format!("tile.{}.{}.{}.pbf", z, x, y));
// let temp_file_path = String::from(temp_file.clone().as_path().to_string_lossy());

// // Open a file in write-only (ignoring errors).
// // This creates the file if it does not exist (and empty the file if it exists).
// let mut file = File::create(temp_file).unwrap();

// // Write a &str in the file (ignoring the result).
// file.write(&data);
println!(
"pbf {} {}",
request.uri(),
data_in_format.content_encoding(),
// temp_file_path
);
match data_in_format {
DataFormat::Gzip => Ok(
response
.header(CONTENT_TYPE, DataFormat::Pbf.content_type())
.header(CONTENT_ENCODING, data_in_format.content_encoding())
.status(200)
.mimetype(DataFormat::Pbf.content_type())
.header("Access-Control-Allow-Origin", "*")
.body(data)
.body(decode(data, data_in_format).unwrap())
.unwrap(),
),
_ => Ok(
response
.header(CONTENT_TYPE, DataFormat::Pbf.content_type())
.header("Access-Control-Allow-Origin", "*")
.header(CONTENT_ENCODING, DataFormat::Gzip.format())
.body(encode(&data))
.body(data)
.unwrap(),
),
}
Expand Down Expand Up @@ -311,16 +273,11 @@ fn main() {
"mbtiles://{}/tiles/{{z}}/{{x}}/{{y}}.{}",
tile_path,
tile_meta.tile_format.format(),
// query_string
)],
"tilejson": tile_meta.tilejson,
"scheme": tile_meta.scheme,
"id": tile_meta.id,
"format": tile_meta.tile_format,
// "grids": tile_meta.grid_format.map(|_| vec![format!(
// "{}/{}/tiles/{{z}}/{{x}}/{{y}}.json{}",
// base_url, tile_name, query_string
// )]),
"bounds": tile_meta.bounds,
"center": tile_meta.center,
"minzoom": tile_meta.minzoom,
Expand Down
8 changes: 0 additions & 8 deletions src-tauri/src/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,12 @@ pub fn get_data_format_via_query(
}

pub fn get_tile_details(path: &Path) -> Result<TileMeta> {
println!("get_tile_details {}", path.to_string_lossy());
let manager = SqliteConnectionManager::file(path).with_flags(OpenFlags::SQLITE_OPEN_READ_ONLY);
println!("manager done {}", path.to_string_lossy());
let connection_pool = match r2d2::Pool::new(manager) {
Ok(connection_pool) => connection_pool,
Err(err) => return Err(Error::Pool(err)),
};
println!("connection_pool done {}", path.to_string_lossy());
let tile_name = path.file_name().and_then(OsStr::to_str).unwrap();
println!("tile_name {}", tile_name);

let connection = connection_pool.get().unwrap();

Expand All @@ -106,7 +102,6 @@ pub fn get_tile_details(path: &Path) -> Result<TileMeta> {
};
match statement.query_row([], |row| Ok(row.get::<_, i8>(0).unwrap_or(0))) {
Ok(count) => {
println!("count {}", count);
if count < 2 {
return Err(Error::MissingTable(String::from(tile_name)));
}
Expand All @@ -122,7 +117,6 @@ pub fn get_tile_details(path: &Path) -> Result<TileMeta> {
},
Err(err) => return Err(err),
};
println!("tile_format {}", tile_format.content_type());

let mut metadata = TileMeta {
connection_pool,
Expand Down Expand Up @@ -150,11 +144,9 @@ pub fn get_tile_details(path: &Path) -> Result<TileMeta> {
.prepare(r#"SELECT name, value FROM metadata WHERE value IS NOT ''"#)
.unwrap();
let mut metadata_rows = statement.query([]).unwrap();
println!("checking metadata");

while let Some(row) = metadata_rows.next().unwrap() {
let label: String = row.get(0).unwrap();
println!("label metadata {}", label);
let value: String = row.get(1).unwrap();
match label.as_ref() {
"name" => metadata.name = Some(value),
Expand Down
10 changes: 5 additions & 5 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ impl DataFormat {
}
}

pub fn decode(data: Vec<u8>, data_type: DataFormat) -> Result<String> {
pub fn decode(data: Vec<u8>, data_type: DataFormat) -> Result<Vec<u8>> {
match data_type {
DataFormat::Gzip => {
let mut z = GzDecoder::new(&data[..]);
let mut s = String::new();
z.read_to_string(&mut s).unwrap();
let mut s = Vec::new();
z.read_to_end(&mut s).unwrap();
Ok(s)
}
DataFormat::Zlib => {
let mut z = ZlibDecoder::new(&data[..]);
let mut s = String::new();
z.read_to_string(&mut s).unwrap();
let mut s = Vec::new();
z.read_to_end(&mut s).unwrap();
Ok(s)
}
_ => Err(Error::InvalidDataFormat(String::from(data_type.format()))),
Expand Down

0 comments on commit 9e7c051

Please sign in to comment.