fix(record): fix origin_file may be used after closing
This commit is contained in:
parent
7180f7d34b
commit
4f432c0401
@ -755,26 +755,31 @@ def download_group_avatar(uid: str):
|
||||
|
||||
|
||||
def download_voice(voice_url: str):
|
||||
with tempfile.NamedTemporaryFile() as origin_file:
|
||||
try:
|
||||
with urllib.request.build_opener() as opener:
|
||||
urllib.request.install_opener(opener)
|
||||
urllib.request.urlretrieve(voice_url, origin_file.name)
|
||||
except (URLError, HTTPError, ContentTooShortError) as e:
|
||||
logging.getLogger(__name__).warning("Error occurs when downloading files: " + str(e))
|
||||
return _("Error occurs when downloading files: ") + str(e)
|
||||
if origin_file.seek(0, 2) <= 0:
|
||||
raise EOFError("File downloaded is Empty")
|
||||
origin_file.seek(0)
|
||||
silk_header = origin_file.read(10)
|
||||
origin_file.seek(0)
|
||||
if b"#!SILK_V3" in silk_header:
|
||||
with tempfile.NamedTemporaryFile() as pcm_file:
|
||||
pilk.decode(origin_file.name, pcm_file.name)
|
||||
audio_file = tempfile.NamedTemporaryFile()
|
||||
pydub.AudioSegment.from_raw(file=pcm_file, sample_width=2, frame_rate=24000, channels=1).export(
|
||||
audio_file, format="ogg", codec="libopus", parameters=["-vbr", "on"]
|
||||
)
|
||||
else:
|
||||
audio_file = origin_file
|
||||
return audio_file
|
||||
origin_file = tempfile.NamedTemporaryFile()
|
||||
try:
|
||||
opener = urllib.request.build_opener()
|
||||
urllib.request.install_opener(opener)
|
||||
urllib.request.urlretrieve(voice_url, origin_file.name)
|
||||
except Exception as e:
|
||||
logging.getLogger(__name__).warning("Error occurs when downloading files: " + str(e))
|
||||
origin_file.close()
|
||||
raise e
|
||||
finally:
|
||||
opener.close()
|
||||
if origin_file.seek(0, 2) <= 0:
|
||||
origin_file.close()
|
||||
raise EOFError("File downloaded is Empty")
|
||||
origin_file.seek(0)
|
||||
silk_header = origin_file.read(10)
|
||||
origin_file.seek(0)
|
||||
if b"#!SILK_V3" in silk_header:
|
||||
with tempfile.NamedTemporaryFile() as pcm_file:
|
||||
pilk.decode(origin_file.name, pcm_file.name)
|
||||
origin_file.close()
|
||||
audio_file = tempfile.NamedTemporaryFile()
|
||||
pydub.AudioSegment.from_raw(file=pcm_file, sample_width=2, frame_rate=24000, channels=1).export(
|
||||
audio_file, format="ogg", codec="libopus", parameters=["-vbr", "on"]
|
||||
)
|
||||
else:
|
||||
audio_file = origin_file
|
||||
return audio_file
|
||||
|
Loading…
Reference in New Issue
Block a user