Skip to content

Commit a6738ca

Browse files
author
Martin
committed
Fixed temp file bug for dotzero files
1 parent 2cad022 commit a6738ca

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

converter_app/models.py

-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ def __init__(self, file):
222222
def __enter__(self):
223223
# Create the temporary directory when the context is entered
224224
self._temp_dir = tempfile.mkdtemp()
225-
print(f"Temporary directory created: {self._temp_dir}")
226225
return self._temp_dir
227226

228227
def __exit__(self, exc_type, exc_val, exc_tb):
@@ -288,7 +287,6 @@ def is_tar_archive(self) -> bool:
288287

289288
def __del__(self):
290289
self.__exit__(None, None, None)
291-
print(f"Object {self.name} is being destroyed.")
292290

293291

294292

converter_app/readers/brucker_dotzero.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import logging
2+
import os
3+
import tempfile
24

35
import opusFC
46
from numpy import ndarray
@@ -20,6 +22,7 @@ def __init__(self, file, *tar_files):
2022
super().__init__(file, *tar_files)
2123
self._dotzero_file = None
2224
self._dx_name = None
25+
self._has_temp_copy = False
2326

2427
def check(self):
2528
"""
@@ -40,12 +43,21 @@ def check(self):
4043
dx_file = next((x for x in self.file_content if x.suffix.lower() == '.dx'), None)
4144
if dx_file is not None:
4245
self._dx_name = dx_file.name[:-3]
43-
44-
elif self.file.suffix.lower() in dotzero_extentions and opusFC.isOpusFile(self.file.fp.filename):
45-
self._dotzero_file = self.file.fp.filename
46+
elif self.file.suffix.lower() in dotzero_extentions:
47+
with tempfile.NamedTemporaryFile(suffix='.0', delete=False) as temp_file:
48+
self._dotzero_file = temp_file.name # You can now access the file path
49+
self.file.fp.save(self._dotzero_file)
50+
self._has_temp_copy = True
51+
if opusFC.isOpusFile(self._dotzero_file):
52+
return True
53+
return False
4654

4755
return self._dotzero_file is not None
4856

57+
def __del__(self):
58+
if self._has_temp_copy:
59+
os.remove(self._dotzero_file)
60+
4961
def _add_to_meta(self, table, src, k=None):
5062
if k is None:
5163
k = []

0 commit comments

Comments
 (0)