feat: Automatically create dir while uploading

This commit is contained in:
sigoden 2022-05-29 10:53:19 +08:00
parent 8900dde7e7
commit c8a25b54ab

View file

@ -100,7 +100,8 @@ impl InnerService {
} }
async fn handle_static(&self, req: Request) -> BoxResult<Response> { async fn handle_static(&self, req: Request) -> BoxResult<Response> {
let path = match self.get_file_path(req.uri().path())? { let req_path = req.uri().path();
let path = match self.get_file_path(req_path)? {
Some(path) => path, Some(path) => path,
None => return Ok(status_code!(StatusCode::FORBIDDEN)), None => return Ok(status_code!(StatusCode::FORBIDDEN)),
}; };
@ -110,24 +111,39 @@ impl InnerService {
if req.uri().query().map(|v| v == "zip").unwrap_or_default() { if req.uri().query().map(|v| v == "zip").unwrap_or_default() {
self.handle_send_dir_zip(path.as_path()).await self.handle_send_dir_zip(path.as_path()).await
} else { } else {
self.handle_send_dir(path.as_path()).await self.handle_send_dir(path.as_path(), true).await
} }
} else { } else {
self.handle_send_file(path.as_path()).await self.handle_send_file(path.as_path()).await
} }
} }
Err(_) => Ok(status_code!(StatusCode::NOT_FOUND)), Err(_) => {
if req_path.ends_with('/') {
self.handle_send_dir(path.as_path(), false).await
} else {
Ok(status_code!(StatusCode::NOT_FOUND))
}
}
} }
} }
async fn handle_upload(&self, mut req: Request) -> BoxResult<Response> { async fn handle_upload(&self, mut req: Request) -> BoxResult<Response> {
let forbidden = status_code!(StatusCode::FORBIDDEN);
let path = match self.get_file_path(req.uri().path())? { let path = match self.get_file_path(req.uri().path())? {
Some(path) => path, Some(path) => path,
None => return Ok(status_code!(StatusCode::FORBIDDEN)), None => return Ok(forbidden),
}; };
if !fs::metadata(&path.parent().unwrap()).await?.is_dir() { match path.parent() {
return Ok(status_code!(StatusCode::FORBIDDEN)); Some(parent) => match fs::metadata(parent).await {
Ok(meta) => {
if !meta.is_dir() {
return Ok(forbidden);
}
}
Err(_) => fs::create_dir_all(parent).await?,
},
None => return Ok(forbidden),
} }
let mut file = fs::File::create(path).await?; let mut file = fs::File::create(path).await?;
@ -160,17 +176,18 @@ impl InnerService {
Ok(status_code!(StatusCode::OK)) Ok(status_code!(StatusCode::OK))
} }
async fn handle_send_dir(&self, path: &Path) -> BoxResult<Response> { async fn handle_send_dir(&self, path: &Path, exist: bool) -> BoxResult<Response> {
let mut rd = fs::read_dir(path).await?;
let mut paths: Vec<PathItem> = vec![]; let mut paths: Vec<PathItem> = vec![];
while let Some(entry) = rd.next_entry().await? { if exist {
let entry_path = entry.path(); let mut rd = fs::read_dir(path).await?;
if let Ok(item) = self.get_path_item(entry_path).await { while let Some(entry) = rd.next_entry().await? {
paths.push(item); let entry_path = entry.path();
if let Ok(item) = self.get_path_item(entry_path).await {
paths.push(item);
}
} }
paths.sort_unstable();
} }
paths.sort_unstable();
let breadcrumb = self.get_breadcrumb(path); let breadcrumb = self.get_breadcrumb(path);
let data = SendDirData { let data = SendDirData {
breadcrumb, breadcrumb,