mirror of
https://github.com/ellmau/adf-obdd.git
synced 2025-12-20 09:39:38 +01:00
Allow file upload for ADF; fix some server bugs
This commit is contained in:
parent
da8e79147b
commit
9012f0ee23
97
Cargo.lock
generated
97
Cargo.lock
generated
@ -122,6 +122,44 @@ dependencies = [
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-multipart"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dee489e3c01eae4d1c35b03c4493f71cb40d93f66b14558feb1b1a807671cc4e"
|
||||
dependencies = [
|
||||
"actix-multipart-derive",
|
||||
"actix-utils",
|
||||
"actix-web",
|
||||
"bytes",
|
||||
"derive_more",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"httparse",
|
||||
"local-waker",
|
||||
"log",
|
||||
"memchr",
|
||||
"mime",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_plain",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-multipart-derive"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2ec592f234db8a253cf80531246a4407c8a70530423eea80688a6c5a44a110e7"
|
||||
dependencies = [
|
||||
"darling 0.14.4",
|
||||
"parse-size",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-router"
|
||||
version = "0.5.1"
|
||||
@ -278,6 +316,7 @@ dependencies = [
|
||||
"actix-cors",
|
||||
"actix-files",
|
||||
"actix-identity",
|
||||
"actix-multipart",
|
||||
"actix-session",
|
||||
"actix-web",
|
||||
"adf_bdd",
|
||||
@ -918,8 +957,18 @@ version = "0.13.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a01d95850c592940db9b8194bc39f4bc0e89dee5c4265e4b1807c34a9aba453c"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"darling_macro",
|
||||
"darling_core 0.13.4",
|
||||
"darling_macro 0.13.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling"
|
||||
version = "0.14.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850"
|
||||
dependencies = [
|
||||
"darling_core 0.14.4",
|
||||
"darling_macro 0.14.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -936,13 +985,38 @@ dependencies = [
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling_core"
|
||||
version = "0.14.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0"
|
||||
dependencies = [
|
||||
"fnv",
|
||||
"ident_case",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"strsim",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling_macro"
|
||||
version = "0.13.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"darling_core 0.13.4",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling_macro"
|
||||
version = "0.14.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
|
||||
dependencies = [
|
||||
"darling_core 0.14.4",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
@ -1881,6 +1955,12 @@ dependencies = [
|
||||
"windows-sys 0.45.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parse-size"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "944553dd59c802559559161f9816429058b869003836120e262e8caec061b7ae"
|
||||
|
||||
[[package]]
|
||||
name = "password-hash"
|
||||
version = "0.5.0"
|
||||
@ -2339,6 +2419,15 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_plain"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6018081315db179d0ce57b1fe4b62a12a0028c9cf9bbef868c9cf477b3c34ae"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_urlencoded"
|
||||
version = "0.7.1"
|
||||
@ -2367,7 +2456,7 @@ version = "1.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e182d6ec6f05393cc0e5ed1bf81ad6db3a8feedf8ee515ecdd369809bcce8082"
|
||||
dependencies = [
|
||||
"darling",
|
||||
"darling 0.13.4",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
|
||||
@ -25,6 +25,7 @@ argon2 = "0.5.0"
|
||||
actix-session = { version="0.7.2", features = ["cookie-session"] }
|
||||
names = "0.14.0"
|
||||
futures-util = "0.3.28"
|
||||
actix-multipart = "0.6.0"
|
||||
|
||||
[features]
|
||||
cors_for_local_development = []
|
||||
|
||||
@ -4,6 +4,7 @@ use std::sync::{Arc, RwLock};
|
||||
use std::time::Duration;
|
||||
|
||||
use actix_identity::Identity;
|
||||
use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};
|
||||
use actix_web::rt::spawn;
|
||||
use actix_web::rt::task::spawn_blocking;
|
||||
use actix_web::rt::time::timeout;
|
||||
@ -27,13 +28,13 @@ use crate::user::{username_exists, User};
|
||||
type Ac = Vec<Term>;
|
||||
type AcDb = Vec<String>;
|
||||
|
||||
#[derive(Copy, Clone, Deserialize, Serialize)]
|
||||
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
|
||||
pub(crate) enum Parsing {
|
||||
Naive,
|
||||
Hybrid,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub(crate) enum Strategy {
|
||||
Ground,
|
||||
Complete,
|
||||
@ -56,6 +57,7 @@ impl From<AcAndGraph> for Bson {
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Deserialize, Serialize)]
|
||||
#[serde(tag = "type", content = "content")]
|
||||
pub(crate) enum OptionWithError<T> {
|
||||
Some(T),
|
||||
Error(String),
|
||||
@ -182,11 +184,36 @@ pub(crate) struct AdfProblem {
|
||||
pub(crate) acs_per_strategy: AcsPerStrategy,
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
struct AddAdfProblemBody {
|
||||
name: Option<String>,
|
||||
#[derive(MultipartForm)]
|
||||
struct AddAdfProblemBodyMultipart {
|
||||
name: Text<String>,
|
||||
code: Option<Text<String>>, // Either Code or File is set
|
||||
file: Option<TempFile>, // Either Code or File is set
|
||||
parsing: Text<Parsing>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AddAdfProblemBodyPlain {
|
||||
name: String,
|
||||
code: String,
|
||||
parse_strategy: Parsing,
|
||||
parsing: Parsing,
|
||||
}
|
||||
|
||||
impl TryFrom<AddAdfProblemBodyMultipart> for AddAdfProblemBodyPlain {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(source: AddAdfProblemBodyMultipart) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
name: source.name.into_inner(),
|
||||
code: source
|
||||
.file
|
||||
.map(|f| std::io::read_to_string(f.file).expect("TempFile should be readable"))
|
||||
.or_else(|| source.code.map(|c| c.into_inner()))
|
||||
.and_then(|code| (!code.is_empty()).then_some(code))
|
||||
.ok_or("Either a file or the code has to be provided.")?,
|
||||
parsing: source.parsing.into_inner(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async fn adf_problem_exists(
|
||||
@ -233,9 +260,12 @@ async fn add_adf_problem(
|
||||
req: HttpRequest,
|
||||
app_state: web::Data<AppState>,
|
||||
identity: Option<Identity>,
|
||||
req_body: web::Json<AddAdfProblemBody>,
|
||||
req_body: MultipartForm<AddAdfProblemBodyMultipart>,
|
||||
) -> impl Responder {
|
||||
let adf_problem_input: AddAdfProblemBody = req_body.into_inner();
|
||||
let adf_problem_input: AddAdfProblemBodyPlain = match req_body.into_inner().try_into() {
|
||||
Ok(input) => input,
|
||||
Err(err) => return HttpResponse::BadRequest().body(err),
|
||||
};
|
||||
let adf_coll: mongodb::Collection<AdfProblem> = app_state
|
||||
.mongodb_client
|
||||
.database(DB_NAME)
|
||||
@ -291,16 +321,14 @@ async fn add_adf_problem(
|
||||
Some(Ok(username)) => username,
|
||||
};
|
||||
|
||||
let problem_name = match &adf_problem_input.name {
|
||||
Some(name) => {
|
||||
if adf_problem_exists(&adf_coll, name, &username).await {
|
||||
let problem_name = if !adf_problem_input.name.is_empty() {
|
||||
if adf_problem_exists(&adf_coll, &adf_problem_input.name, &username).await {
|
||||
return HttpResponse::Conflict()
|
||||
.body("ADF Problem with that name already exists. Please pick another one!");
|
||||
}
|
||||
|
||||
name.clone()
|
||||
}
|
||||
None => {
|
||||
adf_problem_input.name.clone()
|
||||
} else {
|
||||
let gen = Generator::with_naming(Name::Numbered);
|
||||
let candidates = gen.take(10);
|
||||
|
||||
@ -321,14 +349,13 @@ async fn add_adf_problem(
|
||||
return HttpResponse::InternalServerError().body("Could not generate new name.")
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let adf_problem: AdfProblem = AdfProblem {
|
||||
name: problem_name.clone(),
|
||||
username: username.clone(),
|
||||
code: adf_problem_input.code.clone(),
|
||||
parsing_used: adf_problem_input.parse_strategy,
|
||||
parsing_used: adf_problem_input.parsing,
|
||||
adf: SimplifiedAdfOpt::None,
|
||||
acs_per_strategy: AcsPerStrategy::default(),
|
||||
};
|
||||
@ -362,10 +389,11 @@ async fn add_adf_problem(
|
||||
std::thread::sleep(Duration::from_secs(20));
|
||||
|
||||
let parser = AdfParser::default();
|
||||
parser.parse()(&adf_problem_input.code)
|
||||
.map_err(|_| "ADF could not be parsed, double check your input!")?;
|
||||
let parse_result = parser.parse()(&adf_problem_input.code)
|
||||
.map_err(|_| "ADF could not be parsed, double check your input!");
|
||||
|
||||
let lib_adf = match adf_problem_input.parse_strategy {
|
||||
let result = parse_result.map(|_| {
|
||||
let lib_adf = match adf_problem_input.parsing {
|
||||
Parsing::Naive => Adf::from_parser(&parser),
|
||||
Parsing::Hybrid => {
|
||||
let bd_adf = BdAdf::from_parser(&parser);
|
||||
@ -378,13 +406,16 @@ async fn add_adf_problem(
|
||||
graph: lib_adf.into_double_labeled_graph(None),
|
||||
};
|
||||
|
||||
(SimplifiedAdf::from_lib_adf(lib_adf), ac_and_graph)
|
||||
});
|
||||
|
||||
app_state
|
||||
.currently_running
|
||||
.lock()
|
||||
.unwrap()
|
||||
.remove(&running_info);
|
||||
|
||||
Ok::<_, &str>((SimplifiedAdf::from_lib_adf(lib_adf), ac_and_graph))
|
||||
result
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@ -17,13 +17,14 @@ pub(crate) const DB_NAME: &str = "adf-obdd";
|
||||
pub(crate) const USER_COLL: &str = "users";
|
||||
pub(crate) const ADF_COLL: &str = "adf-problems";
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize)]
|
||||
#[serde(tag = "type", content = "content")]
|
||||
pub(crate) enum Task {
|
||||
Parse,
|
||||
Solve(Strategy),
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub(crate) struct RunningInfo {
|
||||
pub(crate) username: String,
|
||||
pub(crate) adf_name: String,
|
||||
|
||||
@ -43,6 +43,12 @@ async fn main() -> std::io::Result<()> {
|
||||
// cookie secret ket
|
||||
let secret_key = Key::generate();
|
||||
|
||||
// needs to be set outside of httpserver closure to only create it once!
|
||||
let app_data = web::Data::new(AppState {
|
||||
mongodb_client: client.clone(),
|
||||
currently_running: Mutex::new(HashSet::new()),
|
||||
});
|
||||
|
||||
HttpServer::new(move || {
|
||||
let app = App::new();
|
||||
|
||||
@ -62,10 +68,7 @@ async fn main() -> std::io::Result<()> {
|
||||
#[cfg(not(feature = "cors_for_local_development"))]
|
||||
let cookie_secure = true;
|
||||
|
||||
app.app_data(web::Data::new(AppState {
|
||||
mongodb_client: client.clone(),
|
||||
currently_running: Mutex::new(HashSet::new()),
|
||||
}))
|
||||
app.app_data(app_data.clone())
|
||||
.wrap(IdentityMiddleware::default())
|
||||
.wrap(
|
||||
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
|
||||
|
||||
@ -110,10 +110,11 @@ async fn delete_account(
|
||||
.database(DB_NAME)
|
||||
.collection(ADF_COLL);
|
||||
|
||||
match identity.map(|id| id.id()) {
|
||||
None => HttpResponse::Unauthorized().body("You need to login to delete your account."),
|
||||
Some(Err(err)) => HttpResponse::InternalServerError().body(err.to_string()),
|
||||
Some(Ok(username)) => {
|
||||
match identity {
|
||||
None => HttpResponse::Unauthorized().body("You are not logged in."),
|
||||
Some(id) => match id.id() {
|
||||
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
|
||||
Ok(username) => {
|
||||
// Delete all adfs created by user
|
||||
match adf_coll
|
||||
.delete_many(doc! { "username": &username }, None)
|
||||
@ -134,7 +135,10 @@ async fn delete_account(
|
||||
.body("Account could not be deleted."),
|
||||
Ok(DeleteResult {
|
||||
deleted_count: 1, ..
|
||||
}) => HttpResponse::Ok().body("Account deleted."),
|
||||
}) => {
|
||||
id.logout();
|
||||
HttpResponse::Ok().body("Account deleted.")
|
||||
}
|
||||
Ok(_) => unreachable!(
|
||||
"delete_one removes at most one entry so all cases are covered already"
|
||||
),
|
||||
@ -143,6 +147,7 @@ async fn delete_account(
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -237,12 +242,13 @@ async fn user_info(app_state: web::Data<AppState>, identity: Option<Identity>) -
|
||||
.database(DB_NAME)
|
||||
.collection(USER_COLL);
|
||||
|
||||
match identity.map(|id| id.id()) {
|
||||
match identity {
|
||||
None => {
|
||||
HttpResponse::Unauthorized().body("You need to login get your account information.")
|
||||
}
|
||||
Some(Err(err)) => HttpResponse::InternalServerError().body(err.to_string()),
|
||||
Some(Ok(username)) => {
|
||||
Some(id) => match id.id() {
|
||||
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
|
||||
Ok(username) => {
|
||||
match user_coll
|
||||
.find_one(doc! { "username": &username }, None)
|
||||
.await
|
||||
@ -255,10 +261,14 @@ async fn user_info(app_state: web::Data<AppState>, identity: Option<Identity>) -
|
||||
|
||||
HttpResponse::Ok().json(info)
|
||||
}
|
||||
Ok(None) => HttpResponse::NotFound().body("Logged in user does not exist anymore."),
|
||||
Ok(None) => {
|
||||
id.logout();
|
||||
HttpResponse::NotFound().body("Logged in user does not exist anymore.")
|
||||
}
|
||||
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user