upend/src/routes.rs

65 lines
1.7 KiB
Rust
Raw Normal View History

2020-09-07 21:21:54 +02:00
use std::path::PathBuf;
2020-08-27 01:07:25 +02:00
use actix::prelude::*;
use actix_files::NamedFile;
use actix_web::{error, get, post, web, Error, HttpResponse};
use log::debug;
use serde::Deserialize;
2020-08-27 01:07:25 +02:00
use crate::hash::{decode, Hash};
#[derive(Clone)]
2020-08-27 01:07:25 +02:00
pub struct State {
pub directory: PathBuf,
pub db: Addr<crate::database::DbExecutor>,
2020-09-07 21:21:54 +02:00
pub hasher: Addr<crate::hash::HasherWorker>,
2020-08-27 01:07:25 +02:00
}
#[get("/raw/{hash}")]
pub async fn get_raw(state: web::Data<State>, hash: web::Path<String>) -> Result<NamedFile, Error> {
let response = state
.db
.send(crate::database::RetrieveByHash {
hash: Hash(decode(hash.into_inner()).map_err(error::ErrorInternalServerError)?),
2020-08-27 01:07:25 +02:00
})
.await?;
debug!("{:?}", response);
2020-08-27 01:07:25 +02:00
match response {
Ok(result) => match result {
Some(path) => Ok(NamedFile::open(path)?),
None => Err(error::ErrorNotFound("NOT FOUND")),
},
Err(e) => Err(error::ErrorInternalServerError(e)),
}
}
#[derive(Deserialize)]
pub struct LookupRequest {
query: String,
}
#[get("/api/lookup")]
pub async fn get_lookup(
state: web::Data<State>,
web::Query(info): web::Query<LookupRequest>,
) -> Result<HttpResponse, Error> {
let response = state
.db
.send(crate::database::LookupByFilename { query: info.query })
.await?;
2020-08-30 22:14:24 +02:00
Ok(HttpResponse::Ok().json(response.map_err(error::ErrorInternalServerError)?))
}
2020-08-27 01:07:25 +02:00
#[post("/api/refresh")]
pub async fn api_refresh(state: web::Data<State>) -> Result<HttpResponse, Error> {
actix::spawn(crate::filesystem::reimport_directory(
state.directory.clone(),
state.db.clone(),
state.hasher.clone(),
));
2020-08-27 01:07:25 +02:00
Ok(HttpResponse::Ok().finish())
}