Added the ability to move folder

Closes #48
This commit is contained in:
2022-10-14 13:03:04 +02:00
parent 643a5755c0
commit df361947cd
9 changed files with 101 additions and 10 deletions

View File

@@ -549,3 +549,43 @@ pub fn get_type(
_type: mime_guess::from_path(std::path::Path::new(&node.name)).first_or_octet_stream().to_string()
})
}
pub fn move_node(
span: &Span,
_: &mut Request,
db: &mut DBConnection,
info: UserInfo,
data: dto::requests::MoveNode
) -> Result<ResponseBox, AppError> {
let guard_lock = DBConnection::get_lock(info.0.id);
let _guard = guard_lock.write(span);
let target =
super::get_node_and_validate(span, &info.0, data.target, db).ok_or(AppError::BadRequest("Invalid target"))?;
if target.is_file {
return AppError::BadRequest("Invalid target").err();
}
let mut nodes = data
.nodes
.iter()
.map(|v| super::get_node_and_validate(span, &info.0, *v, db))
.into_iter()
.collect::<Option<Vec<db::Inode>>>()
.ok_or(AppError::BadRequest("Invalid node"))?;
for parent in super::get_node_path(span, target.clone(), db) {
if nodes.contains(&parent) {
return AppError::BadRequest("Can't move node into one of it's subfolders").err();
}
}
for child in db.get_children(span, target.id) {
if nodes.iter().any(|n| n.name == child.name) {
return AppError::BadRequest("Can't overwrite existing file").err();
}
}
nodes.iter_mut().for_each(|n| db.move_node(span, n, target.id));
get_reply(&dto::responses::Success { statusCode: 200 })
}