Unverified Commit f7e9de53 authored by Sebastian Schüpbach's avatar Sebastian Schüpbach
Browse files

implement basic functionality

parent 6cdd0344
......@@ -13,16 +13,17 @@ use std::collections::HashMap;
use std::path::Path;
use crate::media_folder_utils::{visit_dirs, MediaFileCache};
use crate::service::MakeSvc;
use tokio::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
// TODO: Read path from settings file or args
let args = env::args();
let base_dir = args.skip(1).take(1).collect::<String>();
let media_cache = visit_dirs(&Path::new(&base_dir)).unwrap();
let server = Server::bind(&addr).serve(MakeSvc{});
let server = Server::bind(&addr).serve(MakeSvc{
base_dir: args.skip(1).take(1).collect::<String>(),
outdated_after: Duration::new(7200, 0),
});
if let Err(e) = server.await {
eprintln!("server error: {}", e);
......
......@@ -19,9 +19,9 @@ impl MediaFileCache {
}
}
pub fn is_outdated(&self, outdated_after: Duration) -> bool {
pub fn is_outdated(&self, outdated_after: &Duration) -> bool {
let now = Instant::now();
now.duration_since(self.created_on) >= outdated_after
now.duration_since(self.created_on) >= *outdated_after
}
pub fn merge(&mut self, other: MediaFileCache) -> () {
......
use hyper::{Body, Request, Response, StatusCode};
use hyper::service::Service;
use crate::media_folder_utils::MediaFileCache;
use crate::media_folder_utils::{MediaFileCache, visit_dirs};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use std::path::Path;
pub struct Svc {
media_cache: MediaFileCache,
base_dir: String,
outdated_after: Duration,
}
impl Svc {
fn fetch_file(&mut self, id: String, file_type: &str) -> Result<Response<Body>, hyper::Error> {
if self.media_cache.is_outdated(&self.outdated_after) {
let base_dir = self.base_dir.clone();
self.media_cache = visit_dirs(Path::new(&base_dir)).unwrap()
}
match self.media_cache.get_file_path(&id, file_type) {
Some(p) => Ok(Response::new(Body::from(p))),
None => {
let mut not_found = Response::default();
*not_found.status_mut() = StatusCode::NOT_FOUND;
Ok(not_found)
}
}
}
}
impl Service<Request<Body>> for Svc {
......@@ -19,22 +40,18 @@ impl Service<Request<Body>> for Svc {
}
fn call(&mut self, req: Request<Body>) -> Self::Future {
/* fn mk_response(s: String) -> Result<Response<Body>, hyper::Error> {
Ok(Response::builder().body(Body::from(s)).unwrap())
} */
fn fetch_file(path: String, file_type: &str) -> Result<Response<Body>, hyper::Error> {
Ok(Response::new(Body::from("awef")))
}
/* fn mk_response(s: String) -> Result<Response<Body>, hyper::Error> {
Ok(Response::builder().body(Body::from(s)).unwrap())
} */
let res: Result<Response<Body>, hyper::Error> = match req.uri().path().split("/").skip(1).take(1).collect::<String>().as_str() {
"media" => {
let path = req.uri().path().split("/").skip(2).take(1).collect::<String>();
fetch_file(path, "media")
self.fetch_file(path, "media")
}
"thumbnail" => {
let path = req.uri().path().split("/").skip(2).take(1).collect::<String>();
fetch_file(path, "media")
self.fetch_file(path, "media")
}
"refresh" => {
// refresh_cache
......@@ -52,6 +69,8 @@ impl Service<Request<Body>> for Svc {
}
pub struct MakeSvc {
pub base_dir: String,
pub outdated_after: Duration,
}
impl<T> Service<T> for MakeSvc {
......@@ -64,7 +83,9 @@ impl<T> Service<T> for MakeSvc {
}
fn call(&mut self, _: T) -> Self::Future {
let fut = async move { Ok(Svc { media_cache: MediaFileCache::new() }) };
let outdated_after = self.outdated_after.to_owned();
let base_dir = self.base_dir.to_owned();
let fut = async move { Ok(Svc { media_cache: MediaFileCache::new(), base_dir, outdated_after }) };
Box::pin(fut)
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment