use crate::system::r2::sdk_message;
use crate::{Error, Operation, StoragePath};
use aws_config::{BehaviorVersion, Region};
use aws_sdk_s3::error::ProvideErrorMetadata;
use aws_sdk_s3::{Client, Config};
use dashmap::DashMap;
use std::sync::LazyLock;
use tokio::runtime::{Builder, Runtime};
static R2_CLIENTS: LazyLock<DashMap<String, Client>> = LazyLock::new(DashMap::new);
pub(in crate::system::r2) static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
Builder::new_multi_thread()
.enable_all()
.build()
.expect("failed to build tokio runtime")
});
#[must_use]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub(crate) struct R2Path<'a> {
pub(in crate::system::r2) path: &'a StoragePath,
pub(in crate::system::r2) account_id: &'a str,
pub(in crate::system::r2) bucket: &'a str,
pub(in crate::system::r2) key: &'a str,
}
impl<'a> R2Path<'a> {
const HTTPS_PREFIX: &'static str = "https://";
const R2_HOST: &'static str = ".r2.cloudflarestorage.com";
const R2_PREFIX: &'static str = ".r2.cloudflarestorage.com/";
}
impl<'a> R2Path<'a> {
pub(crate) fn is_r2_url(path: &str) -> bool {
let rest: &str = match path.split_once("://") {
Some(("http" | "https", rest)) => rest,
_ => return false,
};
let host: &str = rest.split_once('/').map_or(rest, |(host, _)| host);
host.ends_with(Self::R2_HOST)
}
pub(crate) fn base_len(path: &str) -> Option<usize> {
Self::parse_parts(path).map(|(_, _, key)| path.len() - key.len())
}
fn parse_parts(path: &str) -> Option<(&str, &str, &str)> {
let s: &str = path.strip_prefix(Self::HTTPS_PREFIX)?;
let (account_id, s) = s.split_at(s.find('.')?);
let s: &str = s.strip_prefix(Self::R2_PREFIX)?;
let (bucket, key) = s.split_once('/')?;
if Self::is_account_id(account_id) && Self::is_bucket(bucket) {
Some((account_id, bucket, key))
} else {
None
}
}
fn is_account_id(account_id: &str) -> bool {
!account_id.is_empty() && account_id.bytes().all(|b| b.is_ascii_alphanumeric())
}
fn is_bucket(bucket: &str) -> bool {
!bucket.is_empty()
&& bucket
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
}
}
impl<'a> R2Path<'a> {
pub(crate) fn new(path: &'a StoragePath) -> Option<Self> {
Self::parse_parts(path.path()).map(|(account_id, bucket, key)| Self {
path,
account_id,
bucket,
key,
})
}
}
impl<'a> R2Path<'a> {
pub(crate) async fn get_client(account_id: &str) -> Client {
if let Some(client) = R2_CLIENTS.get(account_id) {
return client.value().clone();
}
let client: Client = Self::create_client(account_id).await;
R2_CLIENTS
.entry(account_id.to_string())
.or_insert(client)
.clone()
}
async fn create_client(account_id: &str) -> Client {
let endpoint: String = format!("https://{}.r2.cloudflarestorage.com", account_id);
let config: Config = aws_sdk_s3::config::Builder::from(
&aws_config::load_defaults(BehaviorVersion::latest()).await,
)
.endpoint_url(endpoint)
.region(Region::new("auto"))
.build();
Client::from_conf(config)
}
}
impl<'a> R2Path<'a> {
pub(in crate::system::r2) fn sdk_error<E>(self, operation: Operation, error: E) -> Error
where
E: ProvideErrorMetadata + std::error::Error,
{
Error::from_message(self.path.clone(), operation, sdk_message(&error))
}
}
#[cfg(test)]
mod tests {
use crate::R2Path;
#[test]
fn parse_parts() {
let path: &str = "https://account.r2.cloudflarestorage.com/bucket/folder/file.txt";
let parts: Option<(&str, &str, &str)> = R2Path::parse_parts(path);
assert_eq!(parts, Some(("account", "bucket", "folder/file.txt")));
let path: &str = "https://account.r2.cloudflarestorage.com/bucket/";
assert_eq!(R2Path::parse_parts(path), Some(("account", "bucket", "")));
}
#[test]
fn parse_parts_not_r2() {
let paths: [&str; 8] = [
"https://account.r2.cloudflarestorage.com/bucket",
"https://account.example.com/bucket/",
"http://account.r2.cloudflarestorage.com/bucket/",
"/unix/path",
"https://evil/x.r2.cloudflarestorage.com/bucket/",
"https://.r2.cloudflarestorage.com/bucket/",
"https://account.r2.cloudflarestorage.com/Bucket/",
"https://account.r2.cloudflarestorage.com//",
];
for path in paths {
assert_eq!(R2Path::parse_parts(path), None, "{path}");
}
}
#[test]
fn is_r2_url() {
for path in [
"https://account.r2.cloudflarestorage.com/bucket/",
"https://account.r2.cloudflarestorage.com/bucket",
"https://account.r2.cloudflarestorage.com",
"http://account.r2.cloudflarestorage.com/bucket/",
"https://.r2.cloudflarestorage.com/bucket/",
"https://account.r2.cloudflarestorage.com/Bucket/",
] {
assert!(R2Path::is_r2_url(path), "{path}");
}
for path in [
"https://example.com/bucket/",
"https://evil/x.r2.cloudflarestorage.com/bucket/",
"ftp://account.r2.cloudflarestorage.com/bucket/",
"/unix/path",
"",
] {
assert!(!R2Path::is_r2_url(path), "{path}");
}
}
#[test]
fn base_len() {
let path: &str = "https://account.r2.cloudflarestorage.com/bucket/file.txt";
assert_eq!(R2Path::base_len(path), Some(path.len() - "file.txt".len()));
assert_eq!(R2Path::base_len("/unix/path"), None);
}
}