-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add get_object API closes #4 * cargo fix * update doc
- Loading branch information
Showing
12 changed files
with
119 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::apis::get_object::get_object; | ||
use crate::apis::list_buckets; | ||
use actix_router::{Path, ResourceDef, Url}; | ||
use actix_web::{get, web, Error, HttpRequest, HttpResponse}; | ||
|
||
#[get("/{tail}*")] | ||
pub async fn handle_get( | ||
_: web::Path<String>, | ||
req: HttpRequest, | ||
data: web::Data<crate::AppState>, | ||
) -> Result<HttpResponse, Error> { | ||
let mut path = Path::new(Url::new(req.uri().clone())); | ||
|
||
let routes = [ | ||
("/{bucket}/{object_name}", "bucket and object"), | ||
("/", "get bucket"), | ||
]; | ||
|
||
for (pattern, description) in &routes { | ||
if ResourceDef::new(*pattern).capture_match_info(&mut path) { | ||
return match (path.get("bucket"), path.get("object_name")) { | ||
(Some(bucket), Some(object_name)) if *description == "bucket and object" => { | ||
get_object(bucket.to_string(), object_name.to_string(), data).await | ||
} | ||
(None, None) if *description == "get bucket" => { | ||
list_buckets::list_buckets(data).await | ||
} | ||
_ => return Ok(HttpResponse::InternalServerError().finish()), | ||
}; | ||
} | ||
} | ||
|
||
Err(actix_web::error::ErrorNotFound("Not Found")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use actix_web::{error, head, web, Error, HttpRequest, HttpResponse}; | ||
use std::fs::File; | ||
use std::io::Read; | ||
|
||
pub async fn get_object( | ||
bucket_name: String, | ||
object_name: String, | ||
data: web::Data<crate::AppState>, | ||
) -> Result<HttpResponse, Error> { | ||
let working_folder = &data.working_folder; | ||
let filepath = format!("{}/{}/{}", working_folder, bucket_name, object_name); | ||
|
||
let mut file = File::open(filepath)?; | ||
if file.metadata()?.is_file() { | ||
let mut content = Vec::new(); | ||
file.read_to_end(&mut content)?; | ||
return Ok(HttpResponse::Ok().body(content)); | ||
} | ||
|
||
Err(error::ErrorNotFound("Not Found")) | ||
} | ||
|
||
#[head("/{bucket_name}/{object_name}")] | ||
pub async fn get_object_head( | ||
req: HttpRequest, | ||
data: web::Data<crate::AppState>, | ||
) -> Result<HttpResponse, Error> { | ||
let working_folder = &data.working_folder; | ||
let bucket_name = req.match_info().get("bucket_name").unwrap(); | ||
let object_name = req.match_info().get("object_name").unwrap(); | ||
|
||
let filepath = format!("{}/{}/{}", working_folder, bucket_name, object_name); | ||
let file = File::open(&filepath)?; | ||
|
||
if file.metadata()?.is_file() { | ||
let guess = mime_guess::from_path(&filepath).first_or_octet_stream(); | ||
let mut resp = HttpResponse::Ok(); | ||
resp.append_header(("Content-Length", file.metadata()?.len().to_string())); | ||
resp.append_header(("Content-Type", guess.to_string())); | ||
resp.append_header(( | ||
"Last-Modified", | ||
crate::utils::get_timestamp(file.metadata()?.modified()?), | ||
)); | ||
|
||
return Ok(resp.finish()); | ||
} | ||
|
||
Ok(HttpResponse::Ok().finish()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
pub mod create_bucket; | ||
pub mod get_handler; | ||
pub mod list_buckets; | ||
pub mod put_object; | ||
pub mod put_handler; | ||
|
||
mod create_bucket; | ||
pub mod get_object; | ||
mod put_object; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters