58 lines
1.6 KiB
Rust
58 lines
1.6 KiB
Rust
use actix::prelude::*;
|
|
use actix_files::NamedFile;
|
|
use actix_web::{error, get, post, web, Error, HttpResponse};
|
|
use serde::Deserialize;
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Clone)]
|
|
pub struct State {
|
|
pub directory: PathBuf,
|
|
pub db: Addr<crate::database::DbExecutor>,
|
|
pub hasher: Addr<crate::dataops::HasherWorker>,
|
|
}
|
|
|
|
#[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.into_inner(),
|
|
})
|
|
.await?;
|
|
|
|
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?;
|
|
|
|
Ok(HttpResponse::Ok().json(response.map_err(|e| error::ErrorInternalServerError(e))?))
|
|
}
|
|
|
|
#[post("/api/refresh")]
|
|
pub async fn api_refresh(state: web::Data<State>) -> Result<HttpResponse, Error> {
|
|
crate::dataops::update_directory(&state.directory, &state.db, &state.hasher)
|
|
.await
|
|
.map_err(|_| error::ErrorInternalServerError("UPDATE ERROR"))?;
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
}
|