move some files around

This commit is contained in:
Lennart
2024-06-30 19:48:26 +02:00
parent ebf7f62c3b
commit 56876decd0
7 changed files with 12 additions and 7 deletions

View File

@@ -0,0 +1,26 @@
use crate::resource::ResourceService;
use actix_web::web::Path;
use actix_web::HttpRequest;
use actix_web::HttpResponse;
use actix_web::Responder;
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
pub async fn route_delete<A: CheckAuthentication, R: ResourceService + ?Sized>(
path_components: Path<R::PathComponents>,
req: HttpRequest,
auth: AuthInfoExtractor<A>,
) -> Result<impl Responder, R::Error> {
let auth_info = auth.inner;
let path_components = path_components.into_inner();
let no_trash = req
.headers()
.get("X-No-Trashbin")
.map(|val| matches!(val.to_str(), Ok("1")))
.unwrap_or(false);
let resource_service = R::new(&req, &auth_info, path_components.clone()).await?;
resource_service.delete_file(!no_trash).await?;
Ok(HttpResponse::Ok().body(""))
}

View File

@@ -0,0 +1,7 @@
pub mod delete;
pub mod propfind;
pub mod proppatch;
pub use delete::route_delete;
pub use propfind::route_propfind;
pub use proppatch::route_proppatch;

View File

@@ -0,0 +1,94 @@
use crate::depth_extractor::Depth;
use crate::namespace::Namespace;
use crate::resource::HandlePropfind;
use crate::resource::ResourceService;
use crate::xml::MultistatusElement;
use crate::xml::TagList;
use crate::Error;
use actix_web::web::{Data, Path};
use actix_web::HttpRequest;
use actix_web::Responder;
use log::debug;
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
use serde::Deserialize;
// This is not the final place for this struct
pub struct ServicePrefix(pub String);
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct PropElement {
#[serde(flatten)]
pub prop: TagList,
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
pub enum PropfindType {
Propname,
Allprop,
Prop(PropElement),
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
struct PropfindElement {
#[serde(rename = "$value")]
prop: PropfindType,
}
pub async fn route_propfind<A: CheckAuthentication, R: ResourceService + ?Sized>(
path_components: Path<R::PathComponents>,
body: String,
req: HttpRequest,
prefix: Data<ServicePrefix>,
auth: AuthInfoExtractor<A>,
depth: Depth,
) -> Result<impl Responder, R::Error> {
debug!("{body}");
let auth_info = auth.inner;
let prefix = prefix.0.to_owned();
let path_components = path_components.into_inner();
let path = req.path().to_owned();
let resource_service = R::new(&req, &auth_info, path_components.clone()).await?;
// A request body is optional. If empty we MUST return all props
let propfind: PropfindElement = if !body.is_empty() {
quick_xml::de::from_str(&body).map_err(Error::XmlDecodeError)?
} else {
PropfindElement {
prop: PropfindType::Allprop,
}
};
let props = match propfind.prop {
PropfindType::Allprop => {
vec!["allprop".to_owned()]
}
PropfindType::Propname => {
// TODO: Implement
return Err(Error::InternalError.into());
}
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags.into(),
};
let props: Vec<&str> = props.iter().map(String::as_str).collect();
let mut member_responses = Vec::new();
if depth != Depth::Zero {
for (path, member) in resource_service.get_members(auth_info).await? {
member_responses.push(member.propfind(&prefix, path, props.clone()).await?);
}
}
let resource = resource_service.get_file().await?;
let response = resource.propfind(&prefix, path, props).await?;
Ok(MultistatusElement {
responses: vec![response],
member_responses,
ns_dav: Namespace::Dav.as_str(),
ns_caldav: Namespace::CalDAV.as_str(),
ns_ical: Namespace::ICal.as_str(),
})
}

View File

@@ -0,0 +1,164 @@
use std::str::FromStr;
use crate::namespace::Namespace;
use crate::resource::InvalidProperty;
use crate::resource::Resource;
use crate::resource::ResourceService;
use crate::resource::{PropstatElement, PropstatResponseElement, PropstatType};
use crate::xml::MultistatusElement;
use crate::xml::TagList;
use crate::xml::TagName;
use crate::Error;
use actix_web::http::StatusCode;
use actix_web::Responder;
use actix_web::{web::Path, HttpRequest};
use log::debug;
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
use serde::{Deserialize, Serialize};
// https://docs.rs/quick-xml/latest/quick_xml/de/index.html#normal-enum-variant
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
struct PropertyElement<T> {
#[serde(rename = "$value")]
prop: T,
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
struct SetPropertyElement<T> {
prop: PropertyElement<T>,
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
struct RemovePropertyElement {
prop: PropertyElement<TagName>,
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
enum Operation<T> {
Set(SetPropertyElement<T>),
Remove(RemovePropertyElement),
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
struct PropertyupdateElement<T> {
#[serde(rename = "$value", default = "Vec::new")]
operations: Vec<Operation<T>>,
}
pub async fn route_proppatch<A: CheckAuthentication, R: ResourceService + ?Sized>(
path: Path<R::PathComponents>,
body: String,
req: HttpRequest,
auth: AuthInfoExtractor<A>,
) -> Result<impl Responder, R::Error> {
let auth_info = auth.inner;
let path_components = path.into_inner();
let href = req.path().to_owned();
let resource_service = R::new(&req, &auth_info, path_components.clone()).await?;
debug!("{body}");
let PropertyupdateElement::<<R::File as Resource>::Prop> { operations } =
quick_xml::de::from_str(&body).map_err(Error::XmlDecodeError)?;
// Extract all set property names without verification
// Weird workaround because quick_xml doesn't allow untagged enums
let propnames: Vec<String> = quick_xml::de::from_str::<PropertyupdateElement<TagName>>(&body)
.map_err(Error::XmlDecodeError)?
.operations
.into_iter()
.map(|op_el| match op_el {
Operation::Set(set_el) => set_el.prop.prop.into(),
// If we can't remove a nonexisting property then that's no big deal
Operation::Remove(remove_el) => remove_el.prop.prop.into(),
})
.collect();
let mut resource = resource_service.get_file().await?;
let mut props_ok = Vec::new();
let mut props_conflict = Vec::new();
let mut props_not_found = Vec::new();
for (operation, propname) in operations.into_iter().zip(propnames) {
match operation {
Operation::Set(SetPropertyElement {
prop: PropertyElement { prop },
}) => {
if prop.invalid_property() {
props_not_found.push(propname);
continue;
}
match resource.set_prop(prop) {
Ok(()) => {
props_ok.push(propname);
}
Err(Error::PropReadOnly) => {
props_conflict.push(propname);
}
Err(err) => {
// TODO: Think about error handling?
return Err(err.into());
}
}
}
Operation::Remove(_remove_el) => {
match <<R::File as Resource>::PropName as FromStr>::from_str(&propname) {
Ok(prop) => {
match resource.remove_prop(prop) {
Ok(()) => {
props_ok.push(propname);
}
Err(Error::PropReadOnly) => {
props_conflict.push(propname);
}
Err(err) => {
// TODO: Think about error handling?
return Err(err.into());
}
}
}
Err(_) => {
// I guess removing a nonexisting property should be successful :)
props_ok.push(propname);
}
};
}
}
}
if props_not_found.is_empty() && props_conflict.is_empty() {
// Only save if no errors occured
resource_service.save_file(resource).await?;
}
Ok(MultistatusElement {
responses: vec![PropstatResponseElement {
href,
propstat: vec![
PropstatType::Normal(PropstatElement {
prop: TagList::from(props_ok),
status: format!("HTTP/1.1 {}", StatusCode::OK),
}),
PropstatType::NotFound(PropstatElement {
prop: TagList::from(props_not_found),
status: format!("HTTP/1.1 {}", StatusCode::NOT_FOUND),
}),
PropstatType::Conflict(PropstatElement {
prop: TagList::from(props_conflict),
status: format!("HTTP/1.1 {}", StatusCode::CONFLICT),
}),
],
}],
// Dummy just for typing
member_responses: Vec::<String>::new(),
ns_dav: Namespace::Dav.as_str(),
ns_caldav: Namespace::CalDAV.as_str(),
ns_ical: Namespace::ICal.as_str(),
})
}