Skip to content

Commit

Permalink
Return Result from captcha_as_wav_base64
Browse files Browse the repository at this point in the history
  • Loading branch information
minorninth authored and Nutomic committed Jun 29, 2023
1 parent 26017ff commit a3d6444
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
16 changes: 2 additions & 14 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use lemmy_api_common::{context::LemmyContext, utils::local_site_to_slur_regex};
use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_utils::{error::LemmyError, utils::slurs::check_slurs};
use std::io::Cursor;
use tracing::error;
use wav;

mod comment;
Expand All @@ -25,18 +24,7 @@ pub trait Perform {
}

/// Converts the captcha to a base64 encoded wav audio file
pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> String {
match captcha_as_wav(captcha) {
Ok(wav_bytes) => base64::encode(wav_bytes),
Err(e) => {
error!("Error generating captcha: {}", e);
String::new()
}
}
}

/// Concatenate the wav files from each individual letter into a single wav audio file
fn captcha_as_wav(captcha: &Captcha) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> Result<String, Box<dyn std::error::Error>> {
let letters = captcha.as_wav();

// Decode each wav file, concatenate the samples
Expand All @@ -55,7 +43,7 @@ fn captcha_as_wav(captcha: &Captcha) -> Result<Vec<u8>, Box<dyn std::error::Erro
&wav::BitDepth::Sixteen(concat_samples),
&mut output_buffer);

Ok(output_buffer.into_inner())
Ok(base64::encode(output_buffer.into_inner()))
}

/// Check size of report and remove whitespace
Expand Down
6 changes: 5 additions & 1 deletion crates/api/src/local_user/get_captcha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 @@ -33,7 +34,10 @@ impl Perform for GetCaptcha {

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

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

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

0 comments on commit a3d6444

Please sign in to comment.