Fix asset providing

This commit is contained in:
Savanni D'Gerinel 2024-11-28 21:32:13 -05:00
parent b382c68382
commit d3db9d60c2
5 changed files with 26 additions and 14 deletions

View File

@ -9,6 +9,6 @@ tasks:
cmds:
- cargo watch -x test
server:
dev:
cmds:
- cargo watch -x run

View File

@ -1,7 +1,5 @@
use std::{
collections::{hash_map::Iter, HashMap},
fmt::{self, Display},
io::Read,
collections::{hash_map::Iter, HashMap}, fmt::{self, Display}, fs, io::Read, path::PathBuf
};
use mime::Mime;
@ -36,6 +34,12 @@ impl From<std::io::Error> for Error {
#[typeshare]
pub struct AssetId(String);
impl AssetId {
pub fn as_str<'a>(&'a self) -> &'a str {
&self.0
}
}
impl Display for AssetId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "AssetId({})", self.0)
@ -75,19 +79,25 @@ pub struct FsAssets {
}
impl FsAssets {
pub fn new() -> Self {
Self {
assets: HashMap::new(),
}
}
pub fn new(path: PathBuf) -> Self {
let dir = fs::read_dir(path).unwrap();
let mut assets = HashMap::new();
fn assets<'a>(&'a self) -> impl Iterator<Item = &'a AssetId> {
self.assets.keys()
for dir_ent in dir {
println!("{:?}", dir_ent);
let path = dir_ent.unwrap().path();
let file_name = path.file_name().unwrap().to_str().unwrap();
assets.insert(AssetId::from(file_name), path.to_str().unwrap().to_owned());
}
Self {
assets,
}
}
}
impl Assets for FsAssets {
fn assets<'a>(&'a self) -> AssetIter<'a> {
println!("FsAssets assets: {:?}", self.assets);
AssetIter(self.assets.iter())
}

View File

@ -99,6 +99,7 @@ impl Core {
}
pub fn available_images(&self) -> Vec<AssetId> {
println!("available_images");
self.0
.read()
.unwrap()

View File

@ -63,7 +63,7 @@ pub async fn handle_available_images(core: Core) -> impl Reply {
let image_paths: Vec<String> = core
.available_images()
.into_iter()
.map(|path| format!("{}", path))
.map(|path| format!("{}", path.as_str()))
.collect();
Ok(Response::builder()
@ -116,6 +116,7 @@ pub async fn handle_connect_websocket(
client_id: String,
) -> impl Reply {
ws.on_upgrade(move |socket| {
println!("upgrading websocket");
let core = core.clone();
async move {
let (mut ws_sender, _) = socket.split();

View File

@ -5,7 +5,7 @@ use handlers::{
};
use std::{
convert::Infallible,
net::{IpAddr, Ipv4Addr, SocketAddr},
net::{IpAddr, Ipv4Addr, SocketAddr}, path::PathBuf,
};
use warp::{
// header,
@ -96,7 +96,7 @@ async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible
#[tokio::main]
pub async fn main() {
let core = core::Core::new(FsAssets::new());
let core = core::Core::new(FsAssets::new(PathBuf::from("/home/savanni/Pictures")));
let log = warp::log("visions::api");
let route_image = warp::path!("api" / "v1" / "image" / String)