🏡 index : ~doyle/chartered.git

author Jordan Doyle <jordan@doyle.la> 2022-09-09 0:09:19.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-09-09 0:09:19.0 +01:00:00
commit
f23d2fe43191aa42f3b76bcd18a3850f89cb20be [patch]
tree
1ef13d22b41f2d8fcb7374e7e357c93202de1e4e
parent
62a0597ad7878b94921faebd5997b3c5cf6ddaac
download
f23d2fe43191aa42f3b76bcd18a3850f89cb20be.tar.gz

Support alternative S3-compatible crate stores



Diff

 .env                                                   |  1 -
 .gitignore                                             |  2 ++
 Cargo.lock                                             |  1 +
 chartered-fs/Cargo.toml                                |  1 +
 chartered-fs/src/lib.rs                                | 22 ++++++++++++++++++----
 chartered-frontend/src/components/FeaturedCrate.svelte |  2 +-
 6 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/.env b/.env
deleted file mode 100644
index 5f2cbfe..0000000 100644
--- a/.env
+++ /dev/null
@@ -1,1 +1,0 @@
DATABASE_URL=chartered.db
diff --git a/.gitignore b/.gitignore
index 2c83db1..b65c586 100644
--- a/.gitignore
+++ a/.gitignore
@@ -1,4 +1,6 @@
/target
.idea/
/chartered.db
.DS_Store
.env

diff --git a/Cargo.lock b/Cargo.lock
index d950d4d..c444881 100644
--- a/Cargo.lock
+++ a/Cargo.lock
@@ -669,6 +669,7 @@
 "async-trait",
 "aws-config",
 "aws-sdk-s3",
 "base64",
 "bytes",
 "http",
 "itertools",
diff --git a/chartered-fs/Cargo.toml b/chartered-fs/Cargo.toml
index a356d38..e2ffed4 100644
--- a/chartered-fs/Cargo.toml
+++ a/chartered-fs/Cargo.toml
@@ -9,6 +9,7 @@
async-trait = "0.1"
aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "release-2022-08-08", package = "aws-config" }
aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "release-2022-08-08", package = "aws-sdk-s3" }
base64 = "0.13"
bytes = "1.1"
http = "0.2"
itertools = "0.10"
diff --git a/chartered-fs/src/lib.rs b/chartered-fs/src/lib.rs
index 5b2a0e6..1ccefa2 100644
--- a/chartered-fs/src/lib.rs
+++ a/chartered-fs/src/lib.rs
@@ -10,6 +10,7 @@
    model::ObjectCannedAcl,
    presigning::config::PresigningConfig,
    types::{ByteStream, SdkError},
    Endpoint,
};
use bytes::Bytes;
use itertools::Itertools;
@@ -36,8 +37,8 @@
    UuidParse(#[from] uuid::Error),
    #[error("path missing from uri")]
    MissingPath,
    #[error("host missing from uri")]
    MissingHost,
    #[error("invalid uri: {0}")]
    InvalidUri(http::uri::InvalidUri),
    #[error("bucket missing from uri")]
    MissingBucket,
    #[error("invalid aws presigning config: {0}")]
@@ -56,13 +57,23 @@

        Ok(match uri.scheme() {
            "s3" => {
                let shared_config = aws_config::load_from_env().await;
                let mut shared_config = aws_config::from_env();

                if let Some(host) = uri.host() {
                    shared_config = shared_config.endpoint_resolver(Endpoint::immutable(
                        format!("https://{}", host)
                            .parse()
                            .map_err(Error::InvalidUri)?,
                    ));
                }

                let shared_config = shared_config.load().await;

                let client = aws_sdk_s3::Client::new(&shared_config);

                let mut path = uri.path_segments().ok_or(Error::MissingPath)?;

                Self::S3(S3 {
                    host: uri.host().ok_or(Error::MissingHost)?.to_string(),
                    bucket: path.next().ok_or(Error::MissingBucket)?.to_string(),
                    path: Itertools::intersperse(path, "/").collect(),
                    client,
@@ -198,7 +209,6 @@
#[derive(Debug)]
#[allow(dead_code)]
pub struct S3 {
    host: String,
    bucket: String,
    path: String,
    client: aws_sdk_s3::Client,
@@ -227,7 +237,7 @@
        self.client
            .put_object()
            .key(format!("{}/{}", self.path, file_ref.reference))
            .content_md5(format!("{:x}", md5::compute(&data)))
            .content_md5(base64::encode(&*md5::compute(&data)))
            .body(ByteStream::new(data.into()))
            .bucket(&self.bucket)
            .acl(ObjectCannedAcl::Private)
diff --git a/chartered-frontend/src/components/FeaturedCrate.svelte b/chartered-frontend/src/components/FeaturedCrate.svelte
index c67e2f9..4076305 100644
--- a/chartered-frontend/src/components/FeaturedCrate.svelte
+++ a/chartered-frontend/src/components/FeaturedCrate.svelte
@@ -8,7 +8,7 @@
    export let crate: Crate;
</script>

<a href={`/crates/${crate.organisation}/${crate.name}`} class="card flex items-center">
<a href={`/crates/${crate.organisation}/${crate.name}`} class="card flex items-center mb-3">
    <div class="flex-grow">
        <div class="font-semibold">
            {crate.organisation}/<span class="text-highlight">{crate.name}</span>