Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concatenation of audio captcha wav files #3350

Merged
merged 9 commits into from Jun 30, 2023

Conversation

minorninth
Copy link
Contributor

This is for after the captcha code is merged again. No rush.

The existing code doesn't work, it tries to concatenate the raw bytes of the wav files, which results in only the first letter being played and the rest ignored.

Please let me know if you'd prefer different patterns or more error checking, or if you'd prefer to do it without introducing a dependency. Happy to adjust the code to your preferences.

@dessalines dessalines marked this pull request as draft June 26, 2023 16:08
@dessalines
Copy link
Member

Thanks! I'll mark this as draft until we get the captcha stuff merged.

@Nutomic Nutomic marked this pull request as ready for review June 27, 2023 12:26
@Nutomic
Copy link
Member

Nutomic commented Jun 27, 2023

Captcha is merged now.

let (header, samples) = wav::read(&mut cursor)
.expect("Unable to parse wav file");
any_header = Some(header);
concat_samples.extend(samples.as_sixteen().expect("Expected 16-bit samples"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldnt use expect as this can crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change it to propagate errors instead. Should I have the function return a Result, or just an empty string on failure as before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, take a look - I restructured it so that it will log errors and return an empty string if something goes wrong

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should definitely return a result so that the client knows something went wrong. Checking for empty string would be very awkward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, fixed so that captcha_as_wav_base64 returns a result. I changed GetCaptcha to log the error and populate an empty string for wav, because that way it doesn't crash and break the png captcha too. In practice the client handles that gracefully and doesn't display the audio captcha in that case.

Maybe a follow-up would be to change the CaptchaResponse struct so that it's optional. Would that be worth it?

@@ -22,18 +24,26 @@ pub trait Perform {
}

/// Converts the captcha to a base64 encoded wav audio file
pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> String {
pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> Result<String, Box<dyn std::error::Error>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at the error type for the other functions in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, switched to LemmyError and added a translation: LemmyNet/lemmy-translations#76

@@ -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| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at how errors are handled elsewhere in similar files.

@Nutomic Nutomic enabled auto-merge (squash) June 29, 2023 09:08
@Nutomic
Copy link
Member

Nutomic commented Jun 29, 2023

Confirmed working, thanks!

}
let _ = wav::write(any_header.unwrap(),
&wav::BitDepth::Sixteen(concat_samples),
&mut output_buffer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you ignoring an error here? Might have to put ? instead.

Also you need to run cargo +nightly fmt --all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch. Handled that error too, and formatted.

auto-merge was automatically disabled June 29, 2023 22:01

Head branch was pushed to by a user without write access

@Nutomic
Copy link
Member

Nutomic commented Jun 30, 2023

You have an unused import use wav;

@minorninth
Copy link
Contributor Author

You have an unused import use wav;

Fixed

@Nutomic Nutomic merged commit fcc010b into LemmyNet:main Jun 30, 2023
1 check passed
@Nutomic
Copy link
Member

Nutomic commented Jun 30, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants