object_store_opendal/service/
core.rs1use std::borrow::Cow;
19
20use object_store::{
21 Attribute, AttributeValue, GetOptions, GetRange, ObjectMeta, PutOptions, PutResult,
22};
23use opendal::raw::*;
24use opendal::*;
25
26pub fn parse_op_stat(args: &OpStat) -> Result<GetOptions> {
28 let mut options = GetOptions {
29 head: true, ..Default::default()
31 };
32
33 if let Some(version) = args.version() {
34 options.version = Some(version.to_string());
35 }
36
37 if let Some(if_match) = args.if_match() {
38 options.if_match = Some(if_match.to_string());
39 }
40
41 if let Some(if_none_match) = args.if_none_match() {
42 options.if_none_match = Some(if_none_match.to_string());
43 }
44
45 if let Some(if_modified_since) = args.if_modified_since() {
46 options.if_modified_since = Some(if_modified_since);
47 }
48
49 if let Some(if_unmodified_since) = args.if_unmodified_since() {
50 options.if_unmodified_since = Some(if_unmodified_since);
51 }
52
53 Ok(options)
54}
55
56pub fn parse_op_read(args: &OpRead) -> Result<GetOptions> {
58 let mut options = GetOptions::default();
59
60 if let Some(version) = args.version() {
61 options.version = Some(version.to_string());
62 }
63
64 if let Some(if_match) = args.if_match() {
65 options.if_match = Some(if_match.to_string());
66 }
67
68 if let Some(if_none_match) = args.if_none_match() {
69 options.if_none_match = Some(if_none_match.to_string());
70 }
71
72 if let Some(if_modified_since) = args.if_modified_since() {
73 options.if_modified_since = Some(if_modified_since);
74 }
75
76 if let Some(if_unmodified_since) = args.if_unmodified_since() {
77 options.if_unmodified_since = Some(if_unmodified_since);
78 }
79
80 if !args.range().is_full() {
81 let range = args.range();
82 match range.size() {
83 Some(size) => {
84 options.range = Some(GetRange::Bounded(range.offset()..range.offset() + size));
85 }
86 None => {
87 options.range = Some(GetRange::Offset(range.offset()));
88 }
89 }
90 }
91
92 Ok(options)
93}
94
95pub fn parse_op_write(args: &OpWrite) -> Result<PutOptions> {
97 let mut opts = PutOptions::default();
98
99 if let Some(content_type) = args.content_type() {
100 opts.attributes.insert(
101 Attribute::ContentType,
102 AttributeValue::from(content_type.to_string()),
103 );
104 }
105
106 if let Some(content_disposition) = args.content_disposition() {
107 opts.attributes.insert(
108 Attribute::ContentDisposition,
109 AttributeValue::from(content_disposition.to_string()),
110 );
111 }
112
113 if let Some(cache_control) = args.cache_control() {
114 opts.attributes.insert(
115 Attribute::CacheControl,
116 AttributeValue::from(cache_control.to_string()),
117 );
118 }
119
120 if let Some(user_metadata) = args.user_metadata() {
121 for (key, value) in user_metadata {
122 opts.attributes.insert(
123 Attribute::Metadata(Cow::from(key.to_string())),
124 AttributeValue::from(value.to_string()),
125 );
126 }
127 }
128 Ok(opts)
129}
130
131pub fn format_put_multipart_options(opts: PutOptions) -> object_store::PutMultipartOptions {
133 object_store::PutMultipartOptions {
134 attributes: opts.attributes,
135 ..Default::default()
136 }
137}
138
139pub fn format_put_result(result: PutResult) -> Metadata {
141 let mut metadata = Metadata::new(EntryMode::FILE);
142 if let Some(etag) = &result.e_tag {
143 metadata.set_etag(etag);
144 }
145 if let Some(version) = &result.version {
146 metadata.set_version(version);
147 }
148 metadata
149}
150
151pub fn format_metadata(meta: &ObjectMeta) -> Metadata {
153 let mut metadata = Metadata::new(EntryMode::FILE);
154 metadata.set_content_length(meta.size);
155 metadata.set_last_modified(meta.last_modified);
156 if let Some(etag) = &meta.e_tag {
157 metadata.set_etag(etag);
158 }
159 if let Some(version) = &meta.version {
160 metadata.set_version(version);
161 }
162 metadata
163}