use crate::{datetime_to_timestamp, timestamp_to_datetime};
use object_store::{
Attribute, AttributeValue, GetOptions, GetRange, ObjectMeta, PutOptions, PutResult,
};
use opendal::raw::*;
use opendal::*;
use std::borrow::Cow;
pub fn parse_op_stat(args: &OpStat) -> Result<GetOptions> {
let mut options = GetOptions {
head: true, ..Default::default()
};
if let Some(version) = args.version() {
options.version = Some(version.to_string());
}
if let Some(if_match) = args.if_match() {
options.if_match = Some(if_match.to_string());
}
if let Some(if_none_match) = args.if_none_match() {
options.if_none_match = Some(if_none_match.to_string());
}
if let Some(if_modified_since) = args.if_modified_since() {
options.if_modified_since = timestamp_to_datetime(if_modified_since);
}
if let Some(if_unmodified_since) = args.if_unmodified_since() {
options.if_unmodified_since = timestamp_to_datetime(if_unmodified_since);
}
Ok(options)
}
pub fn parse_op_read(args: &OpRead) -> Result<GetOptions> {
let mut options = GetOptions::default();
if let Some(version) = args.version() {
options.version = Some(version.to_string());
}
if let Some(if_match) = args.if_match() {
options.if_match = Some(if_match.to_string());
}
if let Some(if_none_match) = args.if_none_match() {
options.if_none_match = Some(if_none_match.to_string());
}
if let Some(if_modified_since) = args.if_modified_since() {
options.if_modified_since = timestamp_to_datetime(if_modified_since);
}
if let Some(if_unmodified_since) = args.if_unmodified_since() {
options.if_unmodified_since = timestamp_to_datetime(if_unmodified_since);
}
if !args.range().is_full() {
let range = args.range();
match range.size() {
Some(size) => {
options.range = Some(GetRange::Bounded(range.offset()..range.offset() + size));
}
None => {
options.range = Some(GetRange::Offset(range.offset()));
}
}
}
Ok(options)
}
pub fn parse_op_write(args: &OpWrite) -> Result<PutOptions> {
let mut opts = PutOptions::default();
if let Some(content_type) = args.content_type() {
opts.attributes.insert(
Attribute::ContentType,
AttributeValue::from(content_type.to_string()),
);
}
if let Some(content_disposition) = args.content_disposition() {
opts.attributes.insert(
Attribute::ContentDisposition,
AttributeValue::from(content_disposition.to_string()),
);
}
if let Some(cache_control) = args.cache_control() {
opts.attributes.insert(
Attribute::CacheControl,
AttributeValue::from(cache_control.to_string()),
);
}
if let Some(user_metadata) = args.user_metadata() {
for (key, value) in user_metadata {
opts.attributes.insert(
Attribute::Metadata(Cow::from(key.to_string())),
AttributeValue::from(value.to_string()),
);
}
}
Ok(opts)
}
pub fn format_put_multipart_options(opts: PutOptions) -> object_store::PutMultipartOptions {
object_store::PutMultipartOptions {
attributes: opts.attributes,
..Default::default()
}
}
pub fn format_put_result(result: PutResult) -> Metadata {
let mut metadata = Metadata::new(EntryMode::FILE);
if let Some(etag) = &result.e_tag {
metadata.set_etag(etag);
}
if let Some(version) = &result.version {
metadata.set_version(version);
}
metadata
}
pub fn format_metadata(meta: &ObjectMeta) -> Metadata {
let mut metadata = Metadata::new(EntryMode::FILE);
metadata.set_content_length(meta.size);
if let Some(last_modified) = datetime_to_timestamp(meta.last_modified) {
metadata.set_last_modified(last_modified);
}
if let Some(etag) = &meta.e_tag {
metadata.set_etag(etag);
}
if let Some(version) = &meta.version {
metadata.set_version(version);
}
metadata
}