images
This commit is contained in:
@@ -50,8 +50,9 @@ def export_questions_to_zip() -> Tuple[bytes, str]:
|
|||||||
'created_at': category.created_at.isoformat() if category.created_at else None
|
'created_at': category.created_at.isoformat() if category.created_at else None
|
||||||
})
|
})
|
||||||
|
|
||||||
# Get base folder for file operations
|
# Get folders for file operations
|
||||||
base_folder = Path(current_app.config.get('UPLOAD_FOLDER')).parent.parent
|
images_folder = Path(current_app.config.get('UPLOAD_FOLDER'))
|
||||||
|
audio_folder = Path(current_app.config.get('AUDIO_FOLDER'))
|
||||||
|
|
||||||
# Export questions with media files
|
# Export questions with media files
|
||||||
missing_files = []
|
missing_files = []
|
||||||
@@ -78,7 +79,7 @@ def export_questions_to_zip() -> Tuple[bytes, str]:
|
|||||||
question_data['image_filename'] = image_filename
|
question_data['image_filename'] = image_filename
|
||||||
|
|
||||||
# Copy image file to temp directory
|
# Copy image file to temp directory
|
||||||
source_path = os.path.join(base_folder, question.image_path.lstrip('/'))
|
source_path = os.path.join(images_folder, image_filename)
|
||||||
dest_path = os.path.join(images_dir, image_filename)
|
dest_path = os.path.join(images_dir, image_filename)
|
||||||
|
|
||||||
if os.path.exists(source_path):
|
if os.path.exists(source_path):
|
||||||
@@ -97,7 +98,7 @@ def export_questions_to_zip() -> Tuple[bytes, str]:
|
|||||||
question_data['audio_filename'] = audio_filename
|
question_data['audio_filename'] = audio_filename
|
||||||
|
|
||||||
# Copy audio file to temp directory
|
# Copy audio file to temp directory
|
||||||
source_path = os.path.join(base_folder, question.audio_path.lstrip('/'))
|
source_path = os.path.join(audio_folder, audio_filename)
|
||||||
dest_path = os.path.join(audio_dir, audio_filename)
|
dest_path = os.path.join(audio_dir, audio_filename)
|
||||||
|
|
||||||
if os.path.exists(source_path):
|
if os.path.exists(source_path):
|
||||||
@@ -249,10 +250,11 @@ def clear_all_data():
|
|||||||
|
|
||||||
def clear_all_media_files():
|
def clear_all_media_files():
|
||||||
"""Delete all images and audio files from static directories."""
|
"""Delete all images and audio files from static directories."""
|
||||||
base_folder = Path(current_app.config.get('UPLOAD_FOLDER')).parent.parent
|
# Get folders from config
|
||||||
|
images_dir = str(Path(current_app.config.get('UPLOAD_FOLDER')))
|
||||||
|
audio_dir = str(Path(current_app.config.get('AUDIO_FOLDER')))
|
||||||
|
|
||||||
# Clear images
|
# Clear images
|
||||||
images_dir = os.path.join(base_folder, 'backend', 'static', 'images')
|
|
||||||
if os.path.exists(images_dir):
|
if os.path.exists(images_dir):
|
||||||
for filename in os.listdir(images_dir):
|
for filename in os.listdir(images_dir):
|
||||||
if filename == '.gitkeep':
|
if filename == '.gitkeep':
|
||||||
@@ -265,7 +267,6 @@ def clear_all_media_files():
|
|||||||
print(f"Warning: Failed to delete {file_path}: {e}")
|
print(f"Warning: Failed to delete {file_path}: {e}")
|
||||||
|
|
||||||
# Clear audio
|
# Clear audio
|
||||||
audio_dir = os.path.join(base_folder, 'backend', 'static', 'audio')
|
|
||||||
if os.path.exists(audio_dir):
|
if os.path.exists(audio_dir):
|
||||||
for filename in os.listdir(audio_dir):
|
for filename in os.listdir(audio_dir):
|
||||||
if filename == '.gitkeep':
|
if filename == '.gitkeep':
|
||||||
@@ -311,10 +312,9 @@ def import_questions_from_zip(zip_path: str) -> dict:
|
|||||||
with open(manifest_path, 'r') as f:
|
with open(manifest_path, 'r') as f:
|
||||||
manifest = json.load(f)
|
manifest = json.load(f)
|
||||||
|
|
||||||
# Get base folder for file operations
|
# Get destination folders from config
|
||||||
base_folder = Path(current_app.config.get('UPLOAD_FOLDER')).parent.parent
|
images_dest_dir = str(Path(current_app.config.get('UPLOAD_FOLDER')))
|
||||||
images_dest_dir = os.path.join(base_folder, 'backend', 'static', 'images')
|
audio_dest_dir = str(Path(current_app.config.get('AUDIO_FOLDER')))
|
||||||
audio_dest_dir = os.path.join(base_folder, 'backend', 'static', 'audio')
|
|
||||||
|
|
||||||
# Ensure destination directories exist
|
# Ensure destination directories exist
|
||||||
os.makedirs(images_dest_dir, exist_ok=True)
|
os.makedirs(images_dest_dir, exist_ok=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user