Skip to content

Commit

Permalink
Change to return LemmyError
Browse files Browse the repository at this point in the history
  • Loading branch information
minorninth authored and Nutomic committed Jun 29, 2023
1 parent a3d6444 commit 322d84b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
13 changes: 10 additions & 3 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub trait Perform {
}

/// Converts the captcha to a base64 encoded wav audio file
pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> Result<String, Box<dyn std::error::Error>> {
pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> Result<String, LemmyError> {
let letters = captcha.as_wav();

// Decode each wav file, concatenate the samples
Expand All @@ -34,12 +34,19 @@ pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> Result<String, Box<dyn
let mut cursor = Cursor::new(letter.unwrap_or_default());
let (header, samples) = wav::read(&mut cursor)?;
any_header = Some(header);
concat_samples.extend(samples.as_sixteen().ok_or("Expected 16-bit samples")?);
let samples16 = samples.as_sixteen();
if samples16.is_none() {
return Err(LemmyError::from_message("couldnt_create_audio_captcha"));
}
concat_samples.extend(samples16.unwrap());
}

// Encode the concatenated result as a wav file
let mut output_buffer = Cursor::new(vec![]);
let _ = wav::write(any_header.ok_or("No valid letters")?,
if any_header.is_none() {
return Err(LemmyError::from_message("couldnt_create_audio_captcha"));
}
let _ = wav::write(any_header.unwrap(),
&wav::BitDepth::Sixteen(concat_samples),
&mut output_buffer);

Expand Down
6 changes: 1 addition & 5 deletions crates/api/src/local_user/get_captcha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use lemmy_db_schema::source::{
local_site::LocalSite,
};
use lemmy_utils::error::LemmyError;
use tracing::error;

#[async_trait::async_trait(?Send)]
impl Perform for GetCaptcha {
Expand All @@ -34,10 +33,7 @@ impl Perform for GetCaptcha {

let png = captcha.as_base64().expect("failed to generate captcha");

let wav = captcha_as_wav_base64(&captcha).unwrap_or_else(|err| {
error!("failed to generate audio captcha {}", err);
String::new()
});
let wav = captcha_as_wav_base64(&captcha)?;

let captcha_form: CaptchaAnswerForm = CaptchaAnswerForm { answer };
// Stores the captcha item in the db
Expand Down

0 comments on commit 322d84b

Please sign in to comment.