add lookup endpoint, index on path and valid

feat/vaults
Tomáš Mládek 2020-08-27 01:29:44 +02:00
parent 96b666ab41
commit a67b970cd1
7 changed files with 51 additions and 2 deletions

1
Cargo.lock generated
View File

@ -1714,6 +1714,7 @@ dependencies = [
"env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"filebuffer 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)",
"tiny-keccak 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -23,6 +23,8 @@ actix-files = "0.2.2"
actix-rt = "1.0.0"
actix_derive = "0.3.2"
serde = { version = "1.0", features = ["derive"] }
filebuffer = "0.4.0"
walkdir = "2"
tiny-keccak = { version = "2.0", features = ["k12"] }

View File

@ -6,4 +6,6 @@ CREATE TABLE files (
valid BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE INDEX files_hash ON files(hash);
CREATE INDEX files_hash ON files(hash);
CREATE INDEX files_path ON files(path);
CREATE INDEX files_valid ON files(valid);

View File

@ -30,6 +30,12 @@ pub struct RetrieveByHash {
pub hash: String,
}
#[derive(Message)]
#[rtype(result = "Result<Vec<crate::models::File>>")]
pub struct LookupByFilename {
pub query: String,
}
impl Handler<InsertFile> for DbExecutor {
type Result = Result<usize>;
@ -61,6 +67,23 @@ impl Handler<RetrieveByHash> for DbExecutor {
}
}
impl Handler<LookupByFilename> for DbExecutor {
type Result = Result<Vec<crate::models::File>>;
fn handle(&mut self, msg: LookupByFilename, _: &mut Self::Context) -> Self::Result {
use crate::models::File;
use crate::schema::files::dsl::*;
let connection = &self.0.get()?;
let matches = files
.filter(path.like(format!("%{}%", msg.query)))
.load::<File>(connection)?;
Ok(matches)
}
}
#[derive(Debug)]
pub struct ConnectionOptions {
pub enable_foreign_keys: bool,

View File

@ -64,6 +64,7 @@ fn main() -> std::io::Result<()> {
})
.wrap(middleware::Logger::default())
.service(routes::get_raw)
.service(routes::get_lookup)
.service(routes::api_refresh)
})
.bind(&bind)?

View File

@ -1,6 +1,7 @@
use super::schema::files;
use serde::Serialize;
#[derive(Queryable)]
#[derive(Queryable, Serialize)]
pub struct File {
pub id: i32,
pub hash: String,

View File

@ -1,6 +1,7 @@
use actix::prelude::*;
use actix_files::NamedFile;
use actix_web::{error, get, post, web, Error, HttpResponse};
use serde::Deserialize;
use std::path::PathBuf;
pub struct State {
@ -27,6 +28,24 @@ pub async fn get_raw(state: web::Data<State>, hash: web::Path<String>) -> Result
}
}
#[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)