Skip to content

Commit

Permalink
Blossom URLs: Accept any URL-like format (including hostname) and fix…
Browse files Browse the repository at this point in the history
… into a proper URL with / path
  • Loading branch information
mikedilger committed Nov 21, 2024
1 parent 2e047fe commit 4334441
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
12 changes: 6 additions & 6 deletions gossip-lib/src/blossom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ impl Blossom {
/// Check if the data exists on the blossom server
pub async fn check_exists(
&self,
host: String,
base_url: String,
hash: HashOutput,
authorize: bool,
) -> Result<bool, Error> {
let url = format!("https://{}/{}", host, hash);
let url = format!("{}/{}", base_url, hash);
let mut req_builder = self.client.head(url);

if authorize {
Expand Down Expand Up @@ -145,11 +145,11 @@ impl Blossom {
/// with bytes(), bytes_stream(), chunk(), text(), or text_with_charset()
pub async fn download(
&self,
host: String,
base_url: String,
hash: HashOutput,
authorize: bool,
) -> Result<Response, Error> {
let url = format!("https://{}/{}", host, hash);
let url = format!("{}/{}", base_url, hash);
let mut req_builder = self.client.get(url);

if authorize {
Expand Down Expand Up @@ -181,7 +181,7 @@ impl Blossom {
pub async fn upload<T: Into<Body>>(
&self,
data: T,
host: String,
base_url: String,
hash: HashOutput,
content_type: Mime,
content_length: u64,
Expand All @@ -193,7 +193,7 @@ impl Blossom {
vec![hash],
)?;

let url = format!("https://{}/upload", host);
let url = format!("{}/upload", base_url);
let response = self
.client
.put(url)
Expand Down
21 changes: 18 additions & 3 deletions gossip-lib/src/overlord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,11 +1035,24 @@ impl Overlord {
}
};

let host = {
let base_url = {
let blossom_servers = GLOBALS.db().read_setting_blossom_servers();
let first = blossom_servers.split_whitespace().next();
match first {
Some(bs) => bs.to_owned(),
Some(bs) => {
use http::uri::{Parts, PathAndQuery, Scheme};
use http::Uri;

let uri = bs.parse::<Uri>()?;
let mut parts: Parts = uri.into_parts();
parts.path_and_query = Some(PathAndQuery::from_static("/")); // Force no path
if parts.scheme.is_none() {
// Default to https
parts.scheme = Some(Scheme::HTTPS);
}
let uri = Uri::from_parts(parts)?;
format!("{}", uri)
}
None => return Err(ErrorKind::General("Blossom not configured".to_owned()).into()),
}
};
Expand All @@ -1057,7 +1070,9 @@ impl Overlord {
let file = tokio::fs::File::open(&pathbuf).await?;

// upload
let result = blossom.upload(file, host, hash, mime, metadata.len()).await;
let result = blossom
.upload(file, base_url, hash, mime, metadata.len())
.await;
if let Ok(ref bd) = result {
println!("UPLOADED: {} -> {}", pathbuf.display(), &bd.url);
}
Expand Down

0 comments on commit 4334441

Please sign in to comment.