Problem/Motivation
The AI Audio Field module currently ignores the "File Directory" settings configured for audio file fields, instead hardcoding file paths to public:// when generating audio files using AI services. This prevents proper file organization and leads to all generated audio files being saved in the public root directory.
Steps to reproduce
Configure a Media Audio type with a custom directory setting for "field_media_audio_file" (e.g., "audio/[date:custom:Y]-[date:custom:m]")
Use the AI Audio Field module to generate audio content
Observe that generated audio files are stored directly in public:// instead of in the configured subdirectory
Current behavior
AI-generated audio files are always stored directly in the public files directory with a hardcoded filename "test.mp3" regardless of any field configuration.
Expected behavior
AI-generated audio files should respect the "File Directory" configuration of the field_media_audio_file field, including any tokens, just like standard file uploads do.
Proposed resolution
The issue exists in two files:
- src/Plugin/Field/FieldType/AiAudioField.php (line ~173)
- src/Plugin/AiAutomatorType/AiAudioFieldStory.php (line ~299)
Both files contain the same code pattern that hardcodes the file path to public://:
$file = $output->getNormalized()[0]->getAsFileEntity('public://', 'test.mp3');
This code should be updated to retrieve and respect the field's directory configuration.
The attached patch implements a fix for both affected files by:
- Retrieving the field configuration and its directory settings
- Processing any tokens in the directory path
- Sanitizing the directory name and ensuring it has no leading/trailing slashes
- Creating the directory structure if it doesn't exist
- Generating a more meaningful filename using timestamps for uniqueness
- Using the configured path for saving the AI-generated audio files
- This ensures consistency between regular file uploads and AI-generated content, keeping all files organized according to the site's configuration.
This fix improves file organization and prevents the public files root directory from becoming cluttered with AI-generated content. It also ensures that directory-based backup and organizational strategies work properly for AI-generated content.