refactor: Let BlobObject::from_name() take &str (#6571)

This way, all the callers don't have to call to_string()
This commit is contained in:
Hocuri
2025-02-25 11:29:31 +01:00
committed by GitHub
parent 3df693a1bb
commit c5cf16f32a
4 changed files with 9 additions and 9 deletions

View File

@@ -155,7 +155,7 @@ impl<'a> BlobObject<'a> {
return Err(format_err!("bad blob name: {}", rel_path.display())); return Err(format_err!("bad blob name: {}", rel_path.display()));
} }
let name = rel_path.to_str().context("wrong name")?; let name = rel_path.to_str().context("wrong name")?;
BlobObject::from_name(context, name.to_string()) BlobObject::from_name(context, name)
} }
/// Returns a [BlobObject] for an existing blob. /// Returns a [BlobObject] for an existing blob.
@@ -164,13 +164,13 @@ impl<'a> BlobObject<'a> {
/// prefixed, as returned by [BlobObject::as_name]. This is how /// prefixed, as returned by [BlobObject::as_name]. This is how
/// you want to create a [BlobObject] for a filename read from the /// you want to create a [BlobObject] for a filename read from the
/// database. /// database.
pub fn from_name(context: &'a Context, name: String) -> Result<BlobObject<'a>> { pub fn from_name(context: &'a Context, name: &str) -> Result<BlobObject<'a>> {
let name: String = match name.starts_with("$BLOBDIR/") { let name = match name.starts_with("$BLOBDIR/") {
true => name.splitn(2, '/').last().unwrap().to_string(), true => name.splitn(2, '/').last().unwrap(),
false => name, false => name,
}; };
if !BlobObject::is_acceptible_blob_name(&name) { if !BlobObject::is_acceptible_blob_name(name) {
return Err(format_err!("not an acceptable blob name: {}", &name)); return Err(format_err!("not an acceptable blob name: {}", name));
} }
Ok(BlobObject { Ok(BlobObject {
blobdir: context.get_blobdir(), blobdir: context.get_blobdir(),

View File

@@ -1699,7 +1699,7 @@ async fn build_body_file(context: &Context, msg: &Message) -> Result<MimePart<'s
async fn build_avatar_file(context: &Context, path: &str) -> Result<String> { async fn build_avatar_file(context: &Context, path: &str) -> Result<String> {
let blob = match path.starts_with("$BLOBDIR/") { let blob = match path.starts_with("$BLOBDIR/") {
true => BlobObject::from_name(context, path.to_string())?, true => BlobObject::from_name(context, path)?,
false => BlobObject::from_path(context, path.as_ref())?, false => BlobObject::from_path(context, path.as_ref())?,
}; };
let body = fs::read(blob.to_abs_path()).await?; let body = fs::read(blob.to_abs_path()).await?;

View File

@@ -167,7 +167,7 @@ async fn http_cache_get(context: &Context, url: &str) -> Result<Option<(Response
}; };
let is_stale = now > stale_timestamp; let is_stale = now > stale_timestamp;
let blob_object = BlobObject::from_name(context, blob_name)?; let blob_object = BlobObject::from_name(context, &blob_name)?;
let blob_abs_path = blob_object.to_abs_path(); let blob_abs_path = blob_object.to_abs_path();
let blob = match fs::read(blob_abs_path) let blob = match fs::read(blob_abs_path)
.await .await

View File

@@ -367,7 +367,7 @@ impl Params {
return Ok(None); return Ok(None);
}; };
ensure!(val.starts_with("$BLOBDIR/")); ensure!(val.starts_with("$BLOBDIR/"));
let blob = BlobObject::from_name(context, val.to_string())?; let blob = BlobObject::from_name(context, val)?;
Ok(Some(blob)) Ok(Some(blob))
} }