Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Add tag-driven auto-process watch folders with API trigger endpoint (#242)

Murtaza Nasira8d854a8102a
diff --git a/src/api/admin.py b/src/api/admin.py
index f4e3c0f8..1dac4f2f 100644
--- a/src/api/admin.py
+++ b/src/api/admin.py
@@ -1028,6 +1028,42 @@ def admin_update_auto_process_config():
+@admin_bp.route('/admin/auto-process/trigger', methods=['POST'])
+@login_required
+def admin_trigger_auto_process():
+ """Trigger an immediate auto-process scan cycle."""
+ if not current_user.is_admin:
+ return jsonify({'error': 'Unauthorized'}), 403
+
+ try:
+ from src.file_monitor import file_monitor
+ if not file_monitor or not file_monitor.running:
+ return jsonify({'error': 'Auto-processing is not running'}), 400
+
+ import threading
+ def run_scan():
+ try:
+ file_monitor._update_user_cache()
+ if file_monitor.mode == 'admin_only':
+ file_monitor._scan_admin_directory()
+ elif file_monitor.mode == 'user_directories':
+ file_monitor._scan_user_directories()
+ elif file_monitor.mode == 'single_user':
+ file_monitor._scan_single_user_directory()
+ except Exception as e:
+ file_monitor.logger.error(f"Error during triggered scan: {e}", exc_info=True)
+
+ thread = threading.Thread(target=run_scan, daemon=True)
+ thread.start()
+
+ return jsonify({'success': True, 'message': 'Scan triggered'})
+
+ except Exception as e:
+ current_app.logger.error(f"Error triggering auto-process scan: {e}")
+ return jsonify({'error': str(e)}), 500
+
+
+
@admin_bp.route('/admin/inquire/process-recordings', methods=['POST'])
@login_required
def admin_process_recordings_for_inquire():
diff --git a/src/api/auth.py b/src/api/auth.py
index 7414eca4..033474d1 100644
--- a/src/api/auth.py
+++ b/src/api/auth.py
@@ -699,7 +699,8 @@ def account():
admin_disabled_auto_summarization=admin_disabled_auto_summarization,
auto_summarization=current_user.auto_summarization if current_user.auto_summarization is not None else True,
user_language=user_language,
- enable_auto_export=ENABLE_AUTO_EXPORT)
+ enable_auto_export=ENABLE_AUTO_EXPORT,
+ enable_auto_processing=os.environ.get('ENABLE_AUTO_PROCESSING', 'false').lower() == 'true')
@auth_bp.route('/api/user/auto-speaker-labelling', methods=['POST'])
diff --git a/src/api/tags.py b/src/api/tags.py
index 084fb619..2645e7cc 100644
--- a/src/api/tags.py
+++ b/src/api/tags.py
@@ -12,6 +12,9 @@ from flask import Blueprint, render_template, request, redirect, url_for, flash,
from flask_login import login_required, current_user
from werkzeug.utils import secure_filename
+import re
+from pathlib import Path
+
from sqlalchemy.exc import IntegrityError
from src.database import db
@@ -27,6 +30,70 @@ ENABLE_AUTO_DELETION = os.environ.get('ENABLE_AUTO_DELETION', 'false').lower() =
USERS_CAN_DELETE = os.environ.get('USERS_CAN_DELETE', 'true').lower() == 'true'
ENABLE_INTERNAL_SHARING = os.environ.get('ENABLE_INTERNAL_SHARING', 'false').lower() == 'true'
USE_ASR_ENDPOINT = os.environ.get('USE_ASR_ENDPOINT', 'false').lower() == 'true'
+ENABLE_AUTO_PROCESSING = os.environ.get('ENABLE_AUTO_PROCESSING', 'false').lower() == 'true'
+
+
+def _sanitize_folder_name(name):
+ """Convert a tag name to a filesystem-safe folder name."""
+ # Lowercase and replace spaces/underscores with hyphens
+ folder = name.lower().strip()
+ folder = re.sub(r'[\s_]+', '-', folder)
+ # Remove anything that isn't alphanumeric, hyphen, or dot
+ folder = re.sub(r'[^a-z0-9\-.]', '', folder)
+ # Collapse multiple hyphens
+ folder = re.sub(r'-{2,}', '-', folder)
+ # Strip leading/trailing hyphens
+ folder = folder.strip('-')
+ return folder or 'tag'
+
+
+def _get_auto_process_base_dir(user):
+ """Return the base auto-process directory for a user based on AUTO_PROCESS_MODE."""
+ watch_dir = os.environ.get('AUTO_PROCESS_WATCH_DIR', '/data/auto-process')
+ mode = os.environ.get('AUTO_PROCESS_MODE', 'admin_only')
+
+ if mode == 'user_directories':
+ return Path(watch_dir) / f'user{user.id}'
+ else:
+ # admin_only and single_user both use the base directory
+ return Path(watch_dir)
+
+
+def _ensure_auto_process_folder(tag, user):
+ """Create the auto-process watch folder for a tag. Returns (success, path_or_error)."""
+ if not tag.auto_process_folder_name:
+ return False, 'No folder name set'
+
+ base_dir = _get_auto_process_base_dir(user)
+ folder_path = base_dir / tag.auto_process_folder_name
+
+ try:
+ folder_path.mkdir(parents=True, exist_ok=True)
+ return True, str(folder_path)
+ except OSError as e:
+ return False, str(e)
+
+
+def _remove_auto_process_folder(tag, user):
+ """Remove the auto-process watch folder if it's empty. Returns (removed, message)."""
+ if not tag.auto_process_folder_name:
+ return False, 'No folder name set'
+
+ base_dir = _get_auto_process_base_dir(user)
+ folder_path = base_dir / tag.auto_process_folder_name
+
+ if not folder_path.exists():
+ return True, 'Folder does not exist'
+
+ try:
+ # Only remove if empty
+ if not any(folder_path.iterdir()):
+ folder_path.rmdir()
+ return True, 'Folder removed'
+ else:
+ return False, 'Folder not empty, leaving in place'
+ except OSError as e:
+ return False, str(e)
# Global helpers (will be injected from app)
has_recording_access = None
@@ -139,6 +206,16 @@ def create_tag():
if not template:
return jsonify({'error': 'Export template not found'}), 404
+ # Handle auto-process
+ is_auto_process = data.get('is_auto_process', False)
+ auto_process_folder_name = None
+ if is_auto_process:
+ if not ENABLE_AUTO_PROCESSING:
+ return jsonify({'error': 'Auto-processing is not enabled'}), 400
+ if group_id:
+ return jsonify({'error': 'Watch folders are not available for group tags'}), 400
+ auto_process_folder_name = _sanitize_folder_name(data['name'])
+
tag = Tag(
name=data['name'],
user_id=current_user.id,
@@ -155,7 +232,9 @@ def create_tag():
auto_share_on_apply=data.get('auto_share_on_apply', True) if group_id else True,
share_with_group_lead=data.get('share_with_group_lead', True) if group_id else True,
naming_template_id=naming_template_id,
- export_template_id=export_template_id
+ export_template_id=export_template_id,
+ is_auto_process=is_auto_process,
+ auto_process_folder_name=auto_process_folder_name
)
db.session.add(tag)
@@ -167,7 +246,19 @@ def create_tag():
current_app.logger.error(f"Tag creation failed due to integrity constraint: {str(e)}")
return jsonify({'error': 'A tag with this name already exists'}), 400
- return jsonify(tag.to_dict()), 201
+ response = tag.to_dict()
+
+ # Create the watch folder after commit
+ if is_auto_process:
+ success, path_or_error = _ensure_auto_process_folder(tag, current_user)
+ if success:
+ response['auto_process_folder_path'] = path_or_error
+ current_app.logger.info(f"Created auto-process folder for tag '{tag.name}': {path_or_error}")
+ else:
+ response['auto_process_folder_warning'] = f'Could not create watch folder: {path_or_error}'
+ current_app.logger.warning(f"Failed to create auto-process folder for tag '{tag.name}': {path_or_error}")
+
+ return jsonify(response), 201
@@ -276,6 +367,22 @@ def update_tag(tag_id):
return jsonify({'error': 'Export template not found'}), 404
tag.export_template_id = export_template_id if export_template_id else None
+ # Handle auto-process toggle
+ if 'is_auto_process' in data:
+ new_value = bool(data['is_auto_process'])
+ if new_value and not tag.is_auto_process:
+ # Enabling auto-process
+ if not ENABLE_AUTO_PROCESSING:
+ return jsonify({'error': 'Auto-processing is not enabled'}), 400
+ if tag.group_id:
+ return jsonify({'error': 'Watch folders are not available for group tags'}), 400
+ if not tag.auto_process_folder_name:
+ tag.auto_process_folder_name = _sanitize_folder_name(tag.name)
+ tag.is_auto_process = True
+ elif not new_value and tag.is_auto_process:
+ # Disabling auto-process
+ tag.is_auto_process = False
+
tag.updated_at = datetime.utcnow()
try:
@@ -285,7 +392,20 @@ def update_tag(tag_id):
current_app.logger.error(f"Tag update failed due to integrity constraint: {str(e)}")
return jsonify({'error': 'A tag with this name already exists'}), 400
- return jsonify(tag.to_dict())
+ response = tag.to_dict()
+
+ # Create or remove folder after commit
+ if 'is_auto_process' in data:
+ if tag.is_auto_process:
+ success, path_or_error = _ensure_auto_process_folder(tag, current_user)
+ if success:
+ response['auto_process_folder_path'] = path_or_error
+ else:
+ response['auto_process_folder_warning'] = f'Could not create watch folder: {path_or_error}'
+ else:
+ _remove_auto_process_folder(tag, current_user)
+
+ return jsonify(response)
@@ -312,6 +432,12 @@ def delete_tag(tag_id):
if tag.user_id != current_user.id:
return jsonify({'error': 'You do not have permission to delete this tag'}), 403
+ # Clean up auto-process folder if enabled
+ if tag.is_auto_process:
+ user = db.session.get(User, tag.user_id)
+ if user:
+ _remove_auto_process_folder(tag, user)
+
db.session.delete(tag)
db.session.commit()
return jsonify({'success': True})
diff --git a/src/file_monitor.py b/src/file_monitor.py
index f7755fba..2ca5b8be 100644
--- a/src/file_monitor.py
+++ b/src/file_monitor.py
@@ -136,44 +136,74 @@ class FileMonitor:
if not self._admin_user_id:
self.logger.warning("No admin user found, skipping admin directory scan")
return
-
+
self._scan_directory_for_user(self.base_watch_directory, self._admin_user_id)
-
+ self._scan_tag_subdirectories(self.base_watch_directory, self._admin_user_id)
+
def _scan_user_directories(self):
"""Scan user-specific subdirectories."""
if not self.base_watch_directory.exists():
return
-
+
# Look for user directories (e.g., user123, user456)
for item in self.base_watch_directory.iterdir():
if not item.is_dir():
continue
-
+
# Extract user ID from directory name
user_id = self._extract_user_id_from_dirname(item.name)
if user_id and user_id in self._valid_users:
self._scan_directory_for_user(item, user_id)
+ self._scan_tag_subdirectories(item, user_id)
elif item.name.startswith('user'):
self.logger.warning(f"Found user directory '{item.name}' but user ID {user_id} is not valid")
-
+
def _scan_single_user_directory(self):
"""Scan directory for a single configured user."""
default_username = os.environ.get('AUTO_PROCESS_DEFAULT_USERNAME')
if not default_username:
self.logger.warning("AUTO_PROCESS_DEFAULT_USERNAME not configured for single_user mode")
return
-
+
user_id = self._username_to_id.get(default_username)
if user_id:
self._scan_directory_for_user(self.base_watch_directory, user_id)
+ self._scan_tag_subdirectories(self.base_watch_directory, user_id)
else:
self.logger.warning(f"Configured default username '{default_username}' is not valid")
+
+ def _scan_tag_subdirectories(self, directory, user_id):
+ """Scan subdirectories that match auto-process tag folders."""
+ if not directory.exists():
+ return
+
+ from src.app import app
+ from src.models import Tag
+
+ with app.app_context():
+ for item in directory.iterdir():
+ if not item.is_dir():
+ continue
+
+ # Skip hidden dirs and user directories (e.g., user123)
+ if item.name.startswith('.') or self._extract_user_id_from_dirname(item.name) is not None:
+ continue
+
+ # Look up matching auto-process tag
+ tag = Tag.query.filter_by(
+ user_id=user_id,
+ is_auto_process=True,
+ auto_process_folder_name=item.name
+ ).first()
+
+ if tag:
+ self._scan_directory_for_user(item, user_id, tag_id=tag.id)
- def _scan_directory_for_user(self, directory, user_id):
+ def _scan_directory_for_user(self, directory, user_id, tag_id=None):
"""Scan a specific directory for files to process for a specific user."""
if not directory.exists():
return
-
+
for file_path in directory.iterdir():
if not file_path.is_file():
continue
@@ -181,7 +211,7 @@ class FileMonitor:
# Skip hidden files, processing files, or non-supported files
if file_path.name.startswith('.') or file_path.suffix == '.processing':
continue
-
+
if file_path.suffix.lower() not in self.potential_audio_extensions:
continue
@@ -198,7 +228,7 @@ class FileMonitor:
# --- Atomic Lock via Rename ---
processing_path = file_path.with_suffix(file_path.suffix + '.processing')
-
+
try:
file_path.rename(processing_path)
self.logger.info(f"Acquired lock for {file_path}, renamed to {processing_path}")
@@ -211,7 +241,7 @@ class FileMonitor:
# --- Process the locked file ---
try:
- self._process_file(processing_path, user_id)
+ self._process_file(processing_path, user_id, tag_id=tag_id)
except Exception as e:
self.logger.error(f"Error processing file {processing_path}: {e}", exc_info=True)
# If processing fails, unlock the file by renaming it back
@@ -279,13 +309,14 @@ class FileMonitor:
except (OSError, FileNotFoundError):
return False
- def _process_file(self, processing_path, user_id):
+ def _process_file(self, processing_path, user_id, tag_id=None):
"""
Process a single locked audio file for a specific user.
-
+
Args:
processing_path (Path): Path to the locked audio file (e.g., file.mp3.processing)
user_id (int): ID of the user to assign the recording to
+ tag_id (int, optional): Tag ID to apply to the recording
"""
# Import Flask components inside function to avoid circular imports
from src.app import app, db, Recording, User, transcribe_audio_task
@@ -431,13 +462,43 @@ class FileMonitor:
self.logger.info(f"Created recording record with ID: {recording.id} for user: {user.username}")
+ # Apply tag if specified
+ job_params = {}
+ if tag_id:
+ from src.models import Tag, RecordingTag
+ tag = db.session.get(Tag, tag_id)
+ if tag:
+ recording_tag = RecordingTag(
+ recording_id=recording.id,
+ tag_id=tag_id,
+ order=0
+ )
+ db.session.add(recording_tag)
+ db.session.commit()
+ self.logger.info(f"Applied tag '{tag.name}' (id={tag_id}) to recording {recording.id}")
+
+ # Pass tag settings to job params
+ if tag.default_hotwords:
+ job_params['hotwords'] = tag.default_hotwords
+ if tag.default_initial_prompt:
+ job_params['initial_prompt'] = tag.default_initial_prompt
+ if tag.default_language:
+ job_params['language'] = tag.default_language
+ if tag.default_min_speakers:
+ job_params['min_speakers'] = tag.default_min_speakers
+ if tag.default_max_speakers:
+ job_params['max_speakers'] = tag.default_max_speakers
+ if tag.custom_prompt:
+ job_params['custom_prompt'] = tag.custom_prompt
+ job_params['tag_id'] = tag_id
+
# Queue for background processing
from src.services.job_queue import job_queue
job_queue.enqueue(
user_id=user.id,
recording_id=recording.id,
job_type='transcribe',
- params={},
+ params=job_params,
is_new_upload=True
)
@@ -485,9 +546,45 @@ def start_file_monitor():
)
file_monitor.start()
app.logger.info(f"Automated file processing started in '{mode}' mode")
+
+ # Ensure all auto-process tag folders exist
+ _ensure_tag_folders_on_startup(app, watch_dir, mode)
else:
app.logger.info("Automated file processing is disabled")
+
+def _ensure_tag_folders_on_startup(app, watch_dir, mode):
+ """Ensure all auto-process tag folders exist on startup."""
+ with app.app_context():
+ try:
+ from src.database import db
+ from src.models import Tag
+ from src.models.user import User
+ tags = Tag.query.filter_by(is_auto_process=True).all()
+ if not tags:
+ return
+
+ for tag in tags:
+ user = db.session.get(User, tag.user_id)
+ if not user:
+ continue
+
+ if mode == 'user_directories':
+ base_dir = Path(watch_dir) / f'user{user.id}'
+ else:
+ base_dir = Path(watch_dir)
+
+ if tag.auto_process_folder_name:
+ folder_path = base_dir / tag.auto_process_folder_name
+ try:
+ folder_path.mkdir(parents=True, exist_ok=True)
+ except OSError as e:
+ app.logger.error(f"Could not create auto-process folder {folder_path}: {e}")
+
+ app.logger.info(f"Verified {len(tags)} auto-process tag folder(s)")
+ except Exception as e:
+ app.logger.warning(f"Error ensuring tag folders on startup: {e}")
+
def stop_file_monitor():
"""Stop the file monitor."""
global file_monitor
diff --git a/src/init_db.py b/src/init_db.py
index d91217e5..c6542bc7 100644
--- a/src/init_db.py
+++ b/src/init_db.py
@@ -226,6 +226,12 @@ def initialize_database(app):
except Exception as e:
app.logger.warning(f"Could not migrate protected tags to retention_days=-1: {e}")
+ # Auto-process watch folder columns for tags
+ if add_column_if_not_exists(engine, 'tag', 'is_auto_process', 'BOOLEAN DEFAULT 0'):
+ app.logger.info("Added is_auto_process column to tag table")
+ if add_column_if_not_exists(engine, 'tag', 'auto_process_folder_name', 'VARCHAR(100)'):
+ app.logger.info("Added auto_process_folder_name column to tag table")
+
if add_column_if_not_exists(engine, 'tag', 'auto_share_on_apply', 'BOOLEAN DEFAULT 1'):
app.logger.info("Added auto_share_on_apply column to tag table")
diff --git a/src/models/organization.py b/src/models/organization.py
index d20339a5..5bf4724c 100644
--- a/src/models/organization.py
+++ b/src/models/organization.py
@@ -193,6 +193,10 @@ class Tag(db.Model):
# Export template for recordings with this tag
export_template_id = db.Column(db.Integer, db.ForeignKey('export_template.id', ondelete='SET NULL'), nullable=True)
+ # Auto-process watch folder settings
+ is_auto_process = db.Column(db.Boolean, default=False)
+ auto_process_folder_name = db.Column(db.String(100), nullable=True)
+
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
@@ -235,6 +239,8 @@ class Tag(db.Model):
'naming_template_name': self.naming_template.name if self.naming_template else None,
'export_template_id': self.export_template_id,
'export_template_name': self.export_template.name if self.export_template else None,
+ 'is_auto_process': self.is_auto_process or False,
+ 'auto_process_folder_name': self.auto_process_folder_name,
'created_at': self.created_at.isoformat() if self.created_at else None,
'recording_count': len(self.recording_associations)
}
diff --git a/static/locales/de.json b/static/locales/de.json
index 3d01b22e..d0d3ecf1 100644
--- a/static/locales/de.json
+++ b/static/locales/de.json
@@ -562,7 +562,13 @@
"noNamingTemplate": "Keine Vorlage (Standard oder KI-Titel verwenden)",
"tagName": "Tag-Name *",
"tagNamePlaceholder": "z.B. Meetings, Interviews",
- "title": "Tag Bearbeiten"
+ "title": "Tag Bearbeiten",
+ "watchFolder": "Watch Folder",
+ "watchFolderPath": "Drop files in:",
+ "watchFolderGroupTagError": "Watch folders are not available for group tags",
+ "watchFolderCreated": "Watch folder created",
+ "watchFolderRemoved": "Watch folder removed",
+ "watchFolderError": "Could not create watch folder"
},
"errors": {
"audioExtractionFailed": "Audio Extraction Failed",
diff --git a/static/locales/en.json b/static/locales/en.json
index d3828ec4..848c3033 100644
--- a/static/locales/en.json
+++ b/static/locales/en.json
@@ -562,7 +562,13 @@
"noNamingTemplate": "No template (use user default or AI title)",
"tagName": "Tag Name *",
"tagNamePlaceholder": "e.g., Meetings, Interviews",
- "title": "Edit Tag"
+ "title": "Edit Tag",
+ "watchFolder": "Watch Folder",
+ "watchFolderPath": "Drop files in:",
+ "watchFolderGroupTagError": "Watch folders are not available for group tags",
+ "watchFolderCreated": "Watch folder created",
+ "watchFolderRemoved": "Watch folder removed",
+ "watchFolderError": "Could not create watch folder"
},
"errors": {
"audioExtractionFailed": "Audio Extraction Failed",
diff --git a/static/locales/es.json b/static/locales/es.json
index e32c7b12..fc4e8515 100644
--- a/static/locales/es.json
+++ b/static/locales/es.json
@@ -562,7 +562,13 @@
"noNamingTemplate": "Sin plantilla (usar predeterminado o título IA)",
"tagName": "Nombre de la Etiqueta *",
"tagNamePlaceholder": "ej., Reuniones, Entrevistas",
- "title": "Editar Etiqueta"
+ "title": "Editar Etiqueta",
+ "watchFolder": "Watch Folder",
+ "watchFolderPath": "Drop files in:",
+ "watchFolderGroupTagError": "Watch folders are not available for group tags",
+ "watchFolderCreated": "Watch folder created",
+ "watchFolderRemoved": "Watch folder removed",
+ "watchFolderError": "Could not create watch folder"
},
"errors": {
"audioExtractionFailed": "Audio Extraction Failed",
diff --git a/static/locales/fr.json b/static/locales/fr.json
index dc950b5d..8864eb19 100644
--- a/static/locales/fr.json
+++ b/static/locales/fr.json
@@ -562,7 +562,13 @@
"noNamingTemplate": "Pas de modèle (utiliser le défaut ou le titre IA)",
"tagName": "Nom de l'Étiquette *",
"tagNamePlaceholder": "ex., Réunions, Entretiens",
- "title": "Modifier l'Étiquette"
+ "title": "Modifier l'Étiquette",
+ "watchFolder": "Watch Folder",
+ "watchFolderPath": "Drop files in:",
+ "watchFolderGroupTagError": "Watch folders are not available for group tags",
+ "watchFolderCreated": "Watch folder created",
+ "watchFolderRemoved": "Watch folder removed",
+ "watchFolderError": "Could not create watch folder"
},
"errors": {
"audioExtractionFailed": "Audio Extraction Failed",
diff --git a/static/locales/ru.json b/static/locales/ru.json
index 917748ca..5b724e57 100644
--- a/static/locales/ru.json
+++ b/static/locales/ru.json
@@ -562,7 +562,13 @@
"noNamingTemplate": "Без шаблона (использовать по умолчанию или ИИ-заголовок)",
"tagName": "Название тега *",
"tagNamePlaceholder": "например, Встречи, Интервью",
- "title": "Редактирование тега"
+ "title": "Редактирование тега",
+ "watchFolder": "Watch Folder",
+ "watchFolderPath": "Drop files in:",
+ "watchFolderGroupTagError": "Watch folders are not available for group tags",
+ "watchFolderCreated": "Watch folder created",
+ "watchFolderRemoved": "Watch folder removed",
+ "watchFolderError": "Could not create watch folder"
},
"errors": {
"audioExtractionFailed": "Audio Extraction Failed",
diff --git a/static/locales/zh.json b/static/locales/zh.json
index b12f1653..5a7c35ed 100644
--- a/static/locales/zh.json
+++ b/static/locales/zh.json
@@ -639,7 +639,13 @@
"noNamingTemplate": "无模板(使用用户默认或 AI 标题)",
"tagName": "标签名称 *",
"tagNamePlaceholder": "例如,会议、访谈",
- "title": "编辑标签"
+ "title": "编辑标签",
+ "watchFolder": "Watch Folder",
+ "watchFolderPath": "Drop files in:",
+ "watchFolderGroupTagError": "Watch folders are not available for group tags",
+ "watchFolderCreated": "Watch folder created",
+ "watchFolderRemoved": "Watch folder removed",
+ "watchFolderError": "Could not create watch folder"
},
"errors": {
"audioExtractionFailed": "Audio Extraction Failed",
diff --git a/templates/account.html b/templates/account.html
index d125d484..6794ecc9 100644
--- a/templates/account.html
+++ b/templates/account.html
@@ -2052,6 +2052,24 @@ curl "https://your-speakr-instance.com/api/recordings?token=YOUR_TOKEN_HERE"</co
</div>
</div>
+ {% if enable_auto_processing %}
+ <!-- Auto-process watch folder toggle -->
+ <div id="tagAutoProcessRow" class="flex items-center justify-between py-2 px-3 bg-[var(--bg-tertiary)] rounded-md">
+ <div class="flex items-center space-x-2">
+ <i class="fas fa-folder-open text-[var(--text-muted)] text-sm"></i>
+ <label for="tagAutoProcess" class="text-sm font-medium text-[var(--text-secondary)] cursor-pointer" data-i18n="editTagModal.watchFolder">Watch Folder</label>
+ </div>
+ <label class="relative inline-flex items-center cursor-pointer">
+ <input type="checkbox" id="tagAutoProcess" class="sr-only peer">
+ <div class="w-9 h-5 bg-[var(--bg-quaternary)] peer-focus:outline-none peer-focus:ring-2 peer-focus:ring-[var(--border-focus)] rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-[var(--bg-success)]"></div>
+ </label>
+ </div>
+ <div id="tagAutoProcessPath" class="hidden px-3 py-2 bg-[var(--bg-tertiary)] rounded-md -mt-2">
+ <span class="text-xs text-[var(--text-muted)]" data-i18n="editTagModal.watchFolderPath">Drop files in:</span>
+ <code id="tagAutoProcessPathValue" class="block text-xs text-[var(--text-primary)] font-mono mt-1 break-all"></code>
+ </div>
+ {% endif %}
+
<!-- Tab Navigation -->
<div class="border-b border-[var(--border-primary)]">
<nav class="flex -mb-px space-x-1" id="tagModalTabs">
@@ -4705,6 +4723,25 @@ curl "https://your-speakr-instance.com/api/recordings?token=YOUR_TOKEN_HERE"</co
if (document.getElementById('tagExportTemplate')) {
document.getElementById('tagExportTemplate').value = tag.export_template_id || '';
}
+
+ // Populate auto-process toggle
+ const autoProcessCheckbox = document.getElementById('tagAutoProcess');
+ if (autoProcessCheckbox) {
+ autoProcessCheckbox.checked = tag.is_auto_process || false;
+ const pathEl = document.getElementById('tagAutoProcessPath');
+ const pathValueEl = document.getElementById('tagAutoProcessPathValue');
+ if (tag.is_auto_process && tag.auto_process_folder_name) {
+ pathEl?.classList.remove('hidden');
+ if (pathValueEl) pathValueEl.textContent = tag.auto_process_folder_name;
+ } else {
+ pathEl?.classList.add('hidden');
+ }
+ // Hide auto-process for group tags
+ const autoProcessRow = document.getElementById('tagAutoProcessRow');
+ if (autoProcessRow) {
+ autoProcessRow.style.display = tag.group_id ? 'none' : '';
+ }
+ }
} else {
// Create mode
isEditingTag = false;
@@ -4750,6 +4787,15 @@ curl "https://your-speakr-instance.com/api/recordings?token=YOUR_TOKEN_HERE"</co
if (document.getElementById('tagExportTemplate')) {
document.getElementById('tagExportTemplate').value = '';
}
+
+ // Reset auto-process
+ const autoProcessCheckbox = document.getElementById('tagAutoProcess');
+ if (autoProcessCheckbox) {
+ autoProcessCheckbox.checked = false;
+ document.getElementById('tagAutoProcessPath')?.classList.add('hidden');
+ const autoProcessRow = document.getElementById('tagAutoProcessRow');
+ if (autoProcessRow) autoProcessRow.style.display = '';
+ }
}
// Load naming templates for dropdown
@@ -4908,7 +4954,8 @@ curl "https://your-speakr-instance.com/api/recordings?token=YOUR_TOKEN_HERE"</co
default_max_speakers: formData.get('default_max_speakers') ? parseInt(formData.get('default_max_speakers')) : null,
default_hotwords: formData.get('default_hotwords') || null,
default_initial_prompt: formData.get('default_initial_prompt') || null,
- retention_days: retentionDays
+ retention_days: retentionDays,
+ is_auto_process: document.getElementById('tagAutoProcess')?.checked || false
};
// Add group-related fields
@@ -4962,7 +5009,15 @@ curl "https://your-speakr-instance.com/api/recordings?token=YOUR_TOKEN_HERE"</co
tags.push(savedTag);
showToast(`Tag "${savedTag.name}" created successfully`);
}
-
+
+ // Show auto-process folder notifications
+ if (savedTag.auto_process_folder_path) {
+ showToast(translations?.editTagModal?.watchFolderCreated || 'Watch folder created');
+ }
+ if (savedTag.auto_process_folder_warning) {
+ showToast(translations?.editTagModal?.watchFolderError || 'Could not create watch folder', 'error');
+ }
+
displayTags();
closeTagModal();
} catch (error) {
diff --git a/tests/test_auto_process_tags.py b/tests/test_auto_process_tags.py
new file mode 100644
index 00000000..011d6986
--- /dev/null
+++ b/tests/test_auto_process_tags.py
@@ -0,0 +1,602 @@
+#!/usr/bin/env python3
+"""
+Tests for auto-process tag folders feature (Issue #242).
+
+Covers:
+- Model fields and database schema
+- Tag API create/update/delete with auto-process
+- File monitor tag subdirectory scanning
+- Edge cases (sanitization, multiple tags, feature disabled)
+
+Run with: python tests/test_auto_process_tags.py
+"""
+
+import os
+import sys
+import shutil
+import tempfile
+import unittest
+from unittest.mock import patch, MagicMock
+from datetime import datetime
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from src.app import app, db
+from src.models import User, Recording, Tag, RecordingTag
+import src.api.tags as tags_module
+
+
+def _enable_auto_processing():
+ """Context manager-like helper to patch ENABLE_AUTO_PROCESSING at module level."""
+ return patch.object(tags_module, 'ENABLE_AUTO_PROCESSING', True)
+
+
+def _disable_auto_processing():
+ return patch.object(tags_module, 'ENABLE_AUTO_PROCESSING', False)
+
+
+# ---------------------------------------------------------------------------
+# Layer 1: Model & Database
+# ---------------------------------------------------------------------------
+
+class TestTagAutoProcessModel(unittest.TestCase):
+ """Test Tag model auto-process fields."""
+
+ def setUp(self):
+ self.app_context = app.app_context()
+ self.app_context.push()
+
+ def tearDown(self):
+ self.app_context.pop()
+
+ def test_tag_is_auto_process_defaults_to_false(self):
+ """New tags should default is_auto_process to False."""
+ tag = Tag(name='test', user_id=1)
+ self.assertFalse(tag.is_auto_process)
+
+ def test_tag_auto_process_folder_name_persists(self):
+ """auto_process_folder_name should be settable."""
+ tag = Tag(name='test', user_id=1, auto_process_folder_name='my-folder')
+ self.assertEqual(tag.auto_process_folder_name, 'my-folder')
+
+ def test_tag_to_dict_includes_auto_process_fields(self):
+ """to_dict() should include is_auto_process and auto_process_folder_name."""
+ tag = Tag(name='test', user_id=1, is_auto_process=True, auto_process_folder_name='test-folder')
+ tag.group = None
+ tag.naming_template = None
+ tag.export_template = None
+ tag.recording_associations = []
+ d = tag.to_dict()
+ self.assertIn('is_auto_process', d)
+ self.assertTrue(d['is_auto_process'])
+ self.assertIn('auto_process_folder_name', d)
+ self.assertEqual(d['auto_process_folder_name'], 'test-folder')
+
+ def test_tag_auto_process_column_exists(self):
+ """Verify the columns exist in the database schema."""
+ from sqlalchemy import inspect
+ inspector = inspect(db.engine)
+ columns = [col['name'] for col in inspector.get_columns('tag')]
+ self.assertIn('is_auto_process', columns)
+ self.assertIn('auto_process_folder_name', columns)
+
+
+# ---------------------------------------------------------------------------
+# Layer 2: API Tests
+# ---------------------------------------------------------------------------
+
+class TestTagAutoProcessAPI(unittest.TestCase):
+ """Test Tag API endpoints with auto-process."""
+
+ def setUp(self):
+ self.app_context = app.app_context()
+ self.app_context.push()
+ app.config['WTF_CSRF_ENABLED'] = False
+ self.client = app.test_client()
+ self.temp_dir = tempfile.mkdtemp()
+
+ # Create test user
+ self.user = User.query.filter_by(username='autotest_user').first()
+ if not self.user:
+ self.user = User(
+ username='autotest_user',
+ password='fakehash',
+ is_admin=True
+ )
+ db.session.add(self.user)
+ db.session.commit()
+
+ # Log in
+ with self.client.session_transaction() as sess:
+ sess['_user_id'] = str(self.user.id)
+
+ def tearDown(self):
+ Tag.query.filter_by(user_id=self.user.id).delete()
+ db.session.commit()
+ shutil.rmtree(self.temp_dir, ignore_errors=True)
+ self.app_context.pop()
+
+ def test_create_tag_with_auto_process_creates_folder(self):
+ """Creating a tag with is_auto_process=true should create a folder."""
+ with _enable_auto_processing(), \
+ patch.dict(os.environ, {'AUTO_PROCESS_WATCH_DIR': self.temp_dir, 'AUTO_PROCESS_MODE': 'admin_only'}):
+ resp = self.client.post('/api/tags', json={
+ 'name': 'Auto Test Tag',
+ 'is_auto_process': True
+ }, content_type='application/json')
+
+ self.assertIn(resp.status_code, [200, 201])
+ data = resp.get_json()
+ self.assertTrue(data.get('is_auto_process'))
+ self.assertEqual(data.get('auto_process_folder_name'), 'auto-test-tag')
+ self.assertTrue(os.path.isdir(os.path.join(self.temp_dir, 'auto-test-tag')))
+
+ def test_create_tag_default_no_auto_process(self):
+ """Creating a tag without is_auto_process should not enable it."""
+ resp = self.client.post('/api/tags', json={
+ 'name': 'Normal Tag'
+ }, content_type='application/json')
+
+ self.assertIn(resp.status_code, [200, 201])
+ data = resp.get_json()
+ self.assertFalse(data.get('is_auto_process'))
+ self.assertIsNone(data.get('auto_process_folder_name'))
+
+ def test_update_tag_enable_auto_process(self):
+ """Enabling auto-process on an existing tag should create folder."""
+ # Create tag without auto-process first
+ resp = self.client.post('/api/tags', json={
+ 'name': 'Update Test'
+ }, content_type='application/json')
+ self.assertIn(resp.status_code, [200, 201])
+ tag_id = resp.get_json()['id']
+
+ with _enable_auto_processing(), \
+ patch.dict(os.environ, {'AUTO_PROCESS_WATCH_DIR': self.temp_dir, 'AUTO_PROCESS_MODE': 'admin_only'}):
+ resp = self.client.put(f'/api/tags/{tag_id}', json={
+ 'is_auto_process': True
+ }, content_type='application/json')
+
+ self.assertEqual(resp.status_code, 200)
+ self.assertTrue(resp.get_json()['is_auto_process'])
+ self.assertTrue(os.path.isdir(os.path.join(self.temp_dir, 'update-test')))
+
+ def test_update_tag_disable_auto_process_removes_empty_folder(self):
+ """Disabling auto-process should remove the empty folder."""
+ with _enable_auto_processing(), \
+ patch.dict(os.environ, {'AUTO_PROCESS_WATCH_DIR': self.temp_dir, 'AUTO_PROCESS_MODE': 'admin_only'}):
+ resp = self.client.post('/api/tags', json={
+ 'name': 'Disable Test',
+ 'is_auto_process': True
+ }, content_type='application/json')
+ self.assertIn(resp.status_code, [200, 201])
+ tag_id = resp.get_json()['id']
+ folder_path = os.path.join(self.temp_dir, 'disable-test')
+ self.assertTrue(os.path.isdir(folder_path))
+
+ resp = self.client.put(f'/api/tags/{tag_id}', json={
+ 'is_auto_process': False
+ }, content_type='application/json')
+
+ self.assertEqual(resp.status_code, 200)
+ self.assertFalse(resp.get_json()['is_auto_process'])
+ self.assertFalse(os.path.exists(folder_path))
+
+ def test_auto_process_rejected_for_group_tags(self):
+ """Group tags should not be allowed to have auto-process."""
+ from src.models import Group, GroupMembership
+ group = Group.query.filter_by(name='autotest_group').first()
+ if not group:
+ group = Group(name='autotest_group')
+ db.session.add(group)
+ db.session.commit()
+ membership = GroupMembership.query.filter_by(group_id=group.id, user_id=self.user.id).first()
+ if not membership:
+ membership = GroupMembership(group_id=group.id, user_id=self.user.id, role='admin')
+ db.session.add(membership)
+ db.session.commit()
+
+ with _enable_auto_processing(), \
+ patch.dict(os.environ, {'AUTO_PROCESS_WATCH_DIR': self.temp_dir, 'AUTO_PROCESS_MODE': 'admin_only'}):
+ resp = self.client.post('/api/tags', json={
+ 'name': 'Group Auto Tag',
+ 'group_id': group.id,
+ 'is_auto_process': True
+ }, content_type='application/json')
+
+ self.assertEqual(resp.status_code, 400)
+ self.assertIn('not available for group tags', resp.get_json()['error'])
+
+ # Clean up
+ db.session.delete(membership)
+ db.session.delete(group)
+ db.session.commit()
+
+ def test_auto_process_rejected_when_feature_disabled(self):
+ """Auto-process should be rejected when ENABLE_AUTO_PROCESSING is false."""
+ with _disable_auto_processing():
+ resp = self.client.post('/api/tags', json={
+ 'name': 'Disabled Feature Tag',
+ 'is_auto_process': True
+ }, content_type='application/json')
+
+ self.assertEqual(resp.status_code, 400)
+ self.assertIn('not enabled', resp.get_json()['error'])
+
+ def test_delete_tag_removes_auto_process_folder(self):
+ """Deleting an auto-process tag should remove its empty folder."""
+ with _enable_auto_processing(), \
+ patch.dict(os.environ, {'AUTO_PROCESS_WATCH_DIR': self.temp_dir, 'AUTO_PROCESS_MODE': 'admin_only'}):
+ resp = self.client.post('/api/tags', json={
+ 'name': 'Delete Test',
+ 'is_auto_process': True
+ }, content_type='application/json')
+ self.assertIn(resp.status_code, [200, 201])
+ tag_id = resp.get_json()['id']
+ folder_path = os.path.join(self.temp_dir, 'delete-test')
+ self.assertTrue(os.path.isdir(folder_path))
+
+ resp = self.client.delete(f'/api/tags/{tag_id}')
+
+ self.assertEqual(resp.status_code, 200)
+ self.assertFalse(os.path.exists(folder_path))
+
+ def test_folder_name_unchanged_on_tag_rename(self):
+ """Renaming a tag should not change its auto_process_folder_name."""
+ with _enable_auto_processing(), \
+ patch.dict(os.environ, {'AUTO_PROCESS_WATCH_DIR': self.temp_dir, 'AUTO_PROCESS_MODE': 'admin_only'}):
+ resp = self.client.post('/api/tags', json={
+ 'name': 'Original Name',
+ 'is_auto_process': True
+ }, content_type='application/json')
+ self.assertIn(resp.status_code, [200, 201])
+ tag_id = resp.get_json()['id']
+ original_folder = resp.get_json()['auto_process_folder_name']
+
+ resp = self.client.put(f'/api/tags/{tag_id}', json={
+ 'name': 'Renamed Tag'
+ }, content_type='application/json')
+
+ self.assertEqual(resp.status_code, 200)
+ self.assertEqual(resp.get_json()['auto_process_folder_name'], original_folder)
+
+ def test_trigger_endpoint_requires_admin(self):
+ """Non-admin users should get 403 from trigger endpoint."""
+ non_admin = User.query.filter_by(username='autotest_nonadmin').first()
+ if not non_admin:
+ non_admin = User(
+ username='autotest_nonadmin',
+ password='fakehash',
+ is_admin=False
+ )
+ db.session.add(non_admin)
+ db.session.commit()
+
+ with self.client.session_transaction() as sess:
+ sess['_user_id'] = str(non_admin.id)
+
+ resp = self.client.post('/admin/auto-process/trigger',
+ content_type='application/json')
+ self.assertEqual(resp.status_code, 403)
+
+ db.session.delete(non_admin)
+ db.session.commit()
+
+ def test_trigger_endpoint_returns_400_when_not_running(self):
+ """Trigger should return 400 when file monitor is not running."""
+ from src import file_monitor as fm_module
+ original = fm_module.file_monitor
+ fm_module.file_monitor = None
+ try:
+ resp = self.client.post('/admin/auto-process/trigger',
+ content_type='application/json')
+ self.assertEqual(resp.status_code, 400)
+ finally:
+ fm_module.file_monitor = original
+
+
+# ---------------------------------------------------------------------------
+# Layer 3: File Monitor Integration
+# ---------------------------------------------------------------------------
+
+class TestFileMonitorTagSubdirs(unittest.TestCase):
+ """Test file monitor tag subdirectory scanning."""
+
+ def setUp(self):
+ self.app_context = app.app_context()
+ self.app_context.push()
+ self.temp_dir = tempfile.mkdtemp()
+
+ self.user = User.query.filter_by(username='autotest_monitor_user').first()
+ if not self.user:
+ self.user = User(
+ username='autotest_monitor_user',
+ password='fakehash',
+ is_admin=True
+ )
+ db.session.add(self.user)
+ db.session.commit()
+
+ def tearDown(self):
+ Tag.query.filter_by(user_id=self.user.id).delete()
+ db.session.commit()
+ shutil.rmtree(self.temp_dir, ignore_errors=True)
+ self.app_context.pop()
+
+ def _create_auto_process_tag(self, name, folder_name=None):
+ """Helper to create an auto-process tag."""
+ from src.api.tags import _sanitize_folder_name
+ if not folder_name:
+ folder_name = _sanitize_folder_name(name)
+ tag = Tag(
+ name=name,
+ user_id=self.user.id,
+ is_auto_process=True,
+ auto_process_folder_name=folder_name
+ )
+ db.session.add(tag)
+ db.session.commit()
+ return tag
+
+ def test_scan_detects_file_in_tag_subdirectory(self):
+ """Files in tag subdirectories should be detected by scanner."""
+ from src.file_monitor import FileMonitor
+
+ tag = self._create_auto_process_tag('Meetings')
+ tag_dir = os.path.join(self.temp_dir, tag.auto_process_folder_name)
+ os.makedirs(tag_dir)
+
+ test_file = os.path.join(tag_dir, 'test.mp3')
+ with open(test_file, 'wb') as f:
+ f.write(b'\x00' * 1024)
+
+ monitor = FileMonitor(self.temp_dir, check_interval=30, mode='admin_only')
+ monitor._admin_user_id = self.user.id
+ monitor._valid_users = {self.user.id: self.user.username}
+
+ processed = []
+ def mock_process(path, uid, tag_id=None):
+ processed.append({'path': str(path), 'user_id': uid, 'tag_id': tag_id})
+ monitor._process_file = mock_process
+ monitor._is_file_stable = lambda *a, **kw: True
+
+ monitor._scan_tag_subdirectories(monitor.base_watch_directory, self.user.id)
+
+ self.assertEqual(len(processed), 1)
+ self.assertEqual(processed[0]['tag_id'], tag.id)
+
+ def test_scan_applies_tag_to_recording(self):
+ """Files processed from tag dirs should get the tag applied."""
+ tag = self._create_auto_process_tag('Interviews')
+
+ recording = Recording(
+ title='Test Recording',
+ audio_path='/tmp/test.mp3',
+ original_filename='test.mp3',
+ status='PENDING',
+ user_id=self.user.id
+ )
+ db.session.add(recording)
+ db.session.commit()
+
+ rt = RecordingTag(recording_id=recording.id, tag_id=tag.id, order=0)
+ db.session.add(rt)
+ db.session.commit()
+
+ associations = RecordingTag.query.filter_by(recording_id=recording.id).all()
+ self.assertEqual(len(associations), 1)
+ self.assertEqual(associations[0].tag_id, tag.id)
+
+ db.session.delete(rt)
+ db.session.delete(recording)
+ db.session.commit()
+
+ def test_scan_passes_tag_settings_to_job_params(self):
+ """Tag settings (hotwords, prompt, language) should be passed to job params."""
+ tag = self._create_auto_process_tag('Technical')
+ tag.default_hotwords = 'Kubernetes, Docker'
+ tag.default_initial_prompt = 'Technical discussion'
+ tag.default_language = 'en'
+ db.session.commit()
+
+ job_params = {}
+ if tag.default_hotwords:
+ job_params['hotwords'] = tag.default_hotwords
+ if tag.default_initial_prompt:
+ job_params['initial_prompt'] = tag.default_initial_prompt
+ if tag.default_language:
+ job_params['language'] = tag.default_language
+ job_params['tag_id'] = tag.id
+
+ self.assertEqual(job_params['hotwords'], 'Kubernetes, Docker')
+ self.assertEqual(job_params['initial_prompt'], 'Technical discussion')
+ self.assertEqual(job_params['language'], 'en')
+ self.assertEqual(job_params['tag_id'], tag.id)
+
+ def test_root_directory_files_have_no_tag(self):
+ """Files in the root watch directory should not get a tag."""
+ from src.file_monitor import FileMonitor
+
+ monitor = FileMonitor(self.temp_dir, check_interval=30, mode='admin_only')
+ monitor._admin_user_id = self.user.id
+
+ test_file = os.path.join(self.temp_dir, 'root_test.mp3')
+ with open(test_file, 'wb') as f:
+ f.write(b'\x00' * 1024)
+
+ processed = []
+ def mock_process(path, uid, tag_id=None):
+ processed.append({'tag_id': tag_id})
+ monitor._process_file = mock_process
+ monitor._is_file_stable = lambda *a, **kw: True
+
+ monitor._scan_directory_for_user(monitor.base_watch_directory, self.user.id)
+
+ self.assertEqual(len(processed), 1)
+ self.assertIsNone(processed[0]['tag_id'])
+
+ def test_startup_ensures_tag_folders_exist(self):
+ """start_file_monitor should create folders for existing auto-process tags."""
+ tag = self._create_auto_process_tag('Startup Test')
+ folder_path = os.path.join(self.temp_dir, tag.auto_process_folder_name)
+ self.assertFalse(os.path.exists(folder_path))
+
+ from src.file_monitor import _ensure_tag_folders_on_startup
+ _ensure_tag_folders_on_startup(app, self.temp_dir, 'admin_only')
+
+ self.assertTrue(os.path.isdir(folder_path))
+
+ def test_folder_recreated_if_externally_deleted(self):
+ """If a tag folder is deleted externally, startup should recreate it."""
+ tag = self._create_auto_process_tag('Recreate Test')
+ folder_path = os.path.join(self.temp_dir, tag.auto_process_folder_name)
+ os.makedirs(folder_path, exist_ok=True)
+ self.assertTrue(os.path.isdir(folder_path))
+
+ os.rmdir(folder_path)
+ self.assertFalse(os.path.exists(folder_path))
+
+ from src.file_monitor import _ensure_tag_folders_on_startup
+ _ensure_tag_folders_on_startup(app, self.temp_dir, 'admin_only')
+
+ self.assertTrue(os.path.isdir(folder_path))
+
+ def test_unmatched_subdirectory_ignored(self):
+ """Subdirectories without matching tags should be skipped."""
+ from src.file_monitor import FileMonitor
+
+ random_dir = os.path.join(self.temp_dir, 'random-folder')
+ os.makedirs(random_dir)
+ test_file = os.path.join(random_dir, 'test.mp3')
+ with open(test_file, 'wb') as f:
+ f.write(b'\x00' * 1024)
+
+ monitor = FileMonitor(self.temp_dir, check_interval=30, mode='admin_only')
+ monitor._admin_user_id = self.user.id
+ monitor._valid_users = {self.user.id: self.user.username}
+
+ processed = []
+ def mock_process(path, uid, tag_id=None):
+ processed.append({'tag_id': tag_id})
+ monitor._process_file = mock_process
+ monitor._is_file_stable = lambda *a, **kw: True
+
+ monitor._scan_tag_subdirectories(monitor.base_watch_directory, self.user.id)
+
+ self.assertEqual(len(processed), 0)
+
+
+# ---------------------------------------------------------------------------
+# Layer 4: Edge Cases
+# ---------------------------------------------------------------------------
+
+class TestAutoProcessEdgeCases(unittest.TestCase):
+ """Edge case tests for auto-process features."""
+
+ def setUp(self):
+ self.app_context = app.app_context()
+ self.app_context.push()
+ app.config['WTF_CSRF_ENABLED'] = False
+ self.temp_dir = tempfile.mkdtemp()
+
+ self.user = User.query.filter_by(username='autotest_edge_user').first()
+ if not self.user:
+ self.user = User(
+ username='autotest_edge_user',
+ password='fakehash',
+ is_admin=True
+ )
+ db.session.add(self.user)
+ db.session.commit()
+
+ self.client = app.test_client()
+ with self.client.session_transaction() as sess:
+ sess['_user_id'] = str(self.user.id)
+
+ def tearDown(self):
+ Tag.query.filter_by(user_id=self.user.id).delete()
+ db.session.commit()
+ shutil.rmtree(self.temp_dir, ignore_errors=True)
+ self.app_context.pop()
+
+ def test_sanitize_folder_name_special_characters(self):
+ """Folder name sanitization should handle special characters."""
+ from src.api.tags import _sanitize_folder_name
+
+ self.assertEqual(_sanitize_folder_name('Hello World'), 'hello-world')
+ self.assertEqual(_sanitize_folder_name('Test Tag'), 'test-tag')
+ self.assertEqual(_sanitize_folder_name('Special!@#$%'), 'special')
+ self.assertEqual(_sanitize_folder_name('under_score'), 'under-score')
+ self.assertEqual(_sanitize_folder_name(' Spaces '), 'spaces')
+ self.assertEqual(_sanitize_folder_name('MiXeD CaSe'), 'mixed-case')
+ self.assertEqual(_sanitize_folder_name('dots.and-dashes'), 'dots.and-dashes')
+ self.assertEqual(_sanitize_folder_name('!@#$%'), 'tag')
+
+ def test_multiple_tags_different_folders_route_correctly(self):
+ """Multiple auto-process tags should each have their own folder."""
+ with _enable_auto_processing(), \
+ patch.dict(os.environ, {'AUTO_PROCESS_WATCH_DIR': self.temp_dir, 'AUTO_PROCESS_MODE': 'admin_only'}):
+ resp1 = self.client.post('/api/tags', json={
+ 'name': 'Tag Alpha',
+ 'is_auto_process': True
+ }, content_type='application/json')
+ resp2 = self.client.post('/api/tags', json={
+ 'name': 'Tag Beta',
+ 'is_auto_process': True
+ }, content_type='application/json')
+
+ self.assertEqual(resp1.status_code, 201)
+ self.assertEqual(resp2.status_code, 201)
+
+ folder1 = resp1.get_json()['auto_process_folder_name']
+ folder2 = resp2.get_json()['auto_process_folder_name']
+
+ self.assertNotEqual(folder1, folder2)
+ self.assertTrue(os.path.isdir(os.path.join(self.temp_dir, folder1)))
+ self.assertTrue(os.path.isdir(os.path.join(self.temp_dir, folder2)))
+
+ def test_user_directories_mode_tag_folders(self):
+ """In user_directories mode, tag folders should be under user dir."""
+ with _enable_auto_processing(), \
+ patch.dict(os.environ, {'AUTO_PROCESS_WATCH_DIR': self.temp_dir, 'AUTO_PROCESS_MODE': 'user_directories'}):
+ resp = self.client.post('/api/tags', json={
+ 'name': 'User Dir Tag',
+ 'is_auto_process': True
+ }, content_type='application/json')
+
+ self.assertEqual(resp.status_code, 201)
+ data = resp.get_json()
+ expected_path = os.path.join(self.temp_dir, f'user{self.user.id}', data['auto_process_folder_name'])
+ self.assertTrue(os.path.isdir(expected_path))
+
+ def test_disable_auto_process_folder_not_empty_leaves_folder(self):
+ """Disabling auto-process should leave the folder if it has files."""
+ with _enable_auto_processing(), \
+ patch.dict(os.environ, {'AUTO_PROCESS_WATCH_DIR': self.temp_dir, 'AUTO_PROCESS_MODE': 'admin_only'}):
+ resp = self.client.post('/api/tags', json={
+ 'name': 'Nonempty Test',
+ 'is_auto_process': True
+ }, content_type='application/json')
+ self.assertIn(resp.status_code, [200, 201])
+ tag_id = resp.get_json()['id']
+ folder_name = resp.get_json()['auto_process_folder_name']
+ folder_path = os.path.join(self.temp_dir, folder_name)
+
+ # Put a file in the folder
+ with open(os.path.join(folder_path, 'important.mp3'), 'wb') as f:
+ f.write(b'\x00' * 100)
+
+ resp = self.client.put(f'/api/tags/{tag_id}', json={
+ 'is_auto_process': False
+ }, content_type='application/json')
+
+ self.assertEqual(resp.status_code, 200)
+ self.assertTrue(os.path.isdir(folder_path))
+
+
+if __name__ == '__main__':
+ unittest.main()