aboutsummaryrefslogtreecommitdiff
path: root/src/api/s3_get.rs
diff options
context:
space:
mode:
authorQuentin <quentin@deuxfleurs.fr>2020-11-19 14:39:30 +0100
committerQuentin <quentin@deuxfleurs.fr>2020-11-19 14:39:30 +0100
commitfc427b0b66241785ecd3fe23ec3e4ac72aaed1da (patch)
treeea406de04a7a11c09c6d758ca97ba53fc2bdfbe4 /src/api/s3_get.rs
parent6076d869b14aa38059d54a2dece222ad7b9da3bc (diff)
parent044d7988779aadac9290b58ceb077c1211670aed (diff)
downloadgarage-fc427b0b66241785ecd3fe23ec3e4ac72aaed1da.tar.gz
garage-fc427b0b66241785ecd3fe23ec3e4ac72aaed1da.zip
Merge branch 'master' into feature/website
Diffstat (limited to 'src/api/s3_get.rs')
-rw-r--r--src/api/s3_get.rs50
1 files changed, 18 insertions, 32 deletions
diff --git a/src/api/s3_get.rs b/src/api/s3_get.rs
index 68558dbe..a68c485b 100644
--- a/src/api/s3_get.rs
+++ b/src/api/s3_get.rs
@@ -5,13 +5,13 @@ use futures::stream::*;
use hyper::body::Bytes;
use hyper::{Body, Request, Response, StatusCode};
-use garage_util::error::Error;
-
use garage_table::EmptyKey;
use garage_model::garage::Garage;
use garage_model::object_table::*;
+use crate::error::*;
+
fn object_headers(
version: &ObjectVersion,
version_meta: &ObjectVersionMeta,
@@ -41,25 +41,20 @@ pub async fn handle_head(
bucket: &str,
key: &str,
) -> Result<Response<Body>, Error> {
- let object = match garage
+ let object = garage
.object_table
.get(&bucket.to_string(), &key.to_string())
.await?
- {
- None => return Err(Error::NotFound),
- Some(o) => o,
- };
+ .ok_or(Error::NotFound)?;
- let version = match object
+ let version = object
.versions()
.iter()
.rev()
.filter(|v| v.is_data())
.next()
- {
- Some(v) => v,
- None => return Err(Error::NotFound),
- };
+ .ok_or(Error::NotFound)?;
+
let version_meta = match &version.state {
ObjectVersionState::Complete(ObjectVersionData::Inline(meta, _)) => meta,
ObjectVersionState::Complete(ObjectVersionData::FirstBlock(meta, _)) => meta,
@@ -80,25 +75,20 @@ pub async fn handle_get(
bucket: &str,
key: &str,
) -> Result<Response<Body>, Error> {
- let object = match garage
+ let object = garage
.object_table
.get(&bucket.to_string(), &key.to_string())
.await?
- {
- None => return Err(Error::NotFound),
- Some(o) => o,
- };
+ .ok_or(Error::NotFound)?;
- let last_v = match object
+ let last_v = object
.versions()
.iter()
.rev()
.filter(|v| v.is_complete())
.next()
- {
- Some(v) => v,
- None => return Err(Error::NotFound),
- };
+ .ok_or(Error::NotFound)?;
+
let last_v_data = match &last_v.state {
ObjectVersionState::Complete(x) => x,
_ => unreachable!(),
@@ -111,11 +101,8 @@ pub async fn handle_get(
let range = match req.headers().get("range") {
Some(range) => {
- let range_str = range
- .to_str()
- .map_err(|e| Error::BadRequest(format!("Invalid range header: {}", e)))?;
- let mut ranges = http_range::HttpRange::parse(range_str, last_v_meta.size)
- .map_err(|_e| Error::BadRequest(format!("Invalid range")))?;
+ let range_str = range.to_str()?;
+ let mut ranges = http_range::HttpRange::parse(range_str, last_v_meta.size)?;
if ranges.len() > 1 {
return Err(Error::BadRequest(format!("Multiple ranges not supported")));
} else {
@@ -149,10 +136,7 @@ pub async fn handle_get(
let get_next_blocks = garage.version_table.get(&last_v.uuid, &EmptyKey);
let (first_block, version) = futures::try_join!(read_first_block, get_next_blocks)?;
- let version = match version {
- Some(v) => v,
- None => return Err(Error::NotFound),
- };
+ let version = version.ok_or(Error::NotFound)?;
let mut blocks = version
.blocks()
@@ -210,7 +194,9 @@ pub async fn handle_get_range(
let body: Body = Body::from(bytes[begin as usize..end as usize].to_vec());
Ok(resp_builder.body(body)?)
} else {
- Err(Error::Message(format!("Internal error: requested range not present in inline bytes when it should have been")))
+ None.ok_or_internal_error(
+ "Requested range not present in inline bytes when it should have been",
+ )
}
}
ObjectVersionData::FirstBlock(_meta, _first_block_hash) => {