Skip to content

Commit

Permalink
fix: #238 inject randomness into the audio filenames to avoid that on…
Browse files Browse the repository at this point in the history
…e process deletes a wav file while another is still accessing it
  • Loading branch information
mbi committed Dec 17, 2024
1 parent a33dca3 commit 1c92409
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions captcha/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ def captcha_audio(request, key):
text = text.replace("*", "times").replace("-", "minus").replace("+", "plus")
else:
text = ", ".join(list(text))
path = str(os.path.join(tempfile.gettempdir(), "%s.wav" % key))
subprocess.call([settings.CAPTCHA_FLITE_PATH, "-t", text, "-o", path])
path = str(
os.path.join(
tempfile.gettempdir(), f"{key}_{random.randint(100_000, 999_999)}.wav"
)
)
subprocess.run([settings.CAPTCHA_FLITE_PATH, "-t", text, "-o", path])

# Add arbitrary noise if sox is installed
if settings.CAPTCHA_SOX_PATH:
Expand All @@ -180,9 +184,12 @@ def captcha_audio(request, key):
sample_rate = "8000"

arbnoisepath = str(
os.path.join(tempfile.gettempdir(), "%s_arbitrary.wav") % key
os.path.join(
tempfile.gettempdir(),
f"{key}_{random.randint(100_000, 999_999)}_noise.wav",
)
)
subprocess.call(
subprocess.run(
[
settings.CAPTCHA_SOX_PATH,
"-r",
Expand All @@ -196,8 +203,13 @@ def captcha_audio(request, key):
"-15",
]
)
mergedpath = str(os.path.join(tempfile.gettempdir(), "%s_merged.wav") % key)
subprocess.call(
mergedpath = str(
os.path.join(
tempfile.gettempdir(),
f"{key}_{random.randint(100_000, 999_999)}_merged.wav",
)
)
subprocess.run(
[
settings.CAPTCHA_SOX_PATH,
"-m",
Expand Down
2 changes: 1 addition & 1 deletion testproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@
CAPTCHA_SOX_PATH = os.environ.get("CAPTCHA_SOX_PATH", None)
CAPTCHA_BACKGROUND_COLOR = "transparent"
CAPTCHA_LETTER_COLOR_FUNCT = "captcha.helpers.random_letter_color_challenge"
# CAPTCHA_BACKGROUND_COLOR = "#ffffff"
CAPTCHA_BACKGROUND_COLOR = "#ffffff"

0 comments on commit 1c92409

Please sign in to comment.