88 */
99
1010namespace Joomla \AI \Response ;
11+
12+ use Joomla \Filesystem \File ;
13+ use Joomla \Filesystem \Folder ;
14+ use Joomla \Filesystem \Path ;
1115
1216/**
1317 * AI response data object class.
@@ -81,9 +85,20 @@ public function getContent(): string
8185 * Save the response content to file(s) based on response format.
8286 *
8387 * @param string $filename The base filename.
88+ *
89+ * @throws \RuntimeException
90+ * @since __DEPLOY_VERSION__
8491 */
8592 public function saveContentToFile (string $ filename )
8693 {
94+ // Create directory if it doesn't exist
95+ $ dir = dirname ($ filename );
96+ if (!is_dir ($ dir )) {
97+ if (!Folder::create ($ dir )) {
98+ throw new \RuntimeException ('Failed to create directory: ' . $ dir );
99+ }
100+ }
101+
87102 $ metadata = $ this ->getMetadata ();
88103 $ format = $ metadata ['response_format ' ] ?? null ;
89104 $ content = $ this ->getContent ();
@@ -92,19 +107,23 @@ public function saveContentToFile(string $filename)
92107 if ($ format === 'b64_json ' ) {
93108 $ savedFiles = [];
94109 $ imageCount = $ metadata ['image_count ' ] ?? 1 ;
95- $ dir = pathinfo ( $ filename, PATHINFO_DIRNAME );
96- $ baseName = pathinfo ($ filename , PATHINFO_FILENAME );
97- $ ext = pathinfo ($ filename , PATHINFO_EXTENSION ) ?: 'png ' ;
110+ $ dir = Path:: clean ( dirname ( $ filename) );
111+ $ baseName = pathinfo ($ filename , PATHINFO_FILENAME );
112+ $ ext = pathinfo ($ filename , PATHINFO_EXTENSION ) ?: 'png ' ;
98113
99114 $ data = json_decode ($ content , true );
100115 if ($ imageCount === 1 && is_string ($ content )) {
101- file_put_contents ($ filename , base64_decode ($ content ));
102- $ savedFiles [] = $ filename ;
116+ $ decodedContent = base64_decode ($ content );
117+ if (File::write ($ filename , $ decodedContent )) {
118+ $ savedFiles [] = $ filename ;
119+ }
103120 } elseif (is_array ($ data )) {
104121 foreach ($ data as $ index => $ b64 ) {
105- $ file = ($ dir !== '. ' ? $ dir . DIRECTORY_SEPARATOR : '' ) . $ baseName . '_ ' . ($ index + 1 ) . '. ' . $ ext ;
106- file_put_contents ($ file , base64_decode ($ b64 ));
107- $ savedFiles [] = $ file ;
122+ $ file = Path::clean ($ dir . '/ ' . $ baseName . '_ ' . ($ index + 1 ) . '. ' . $ ext );
123+ $ decodedContent = base64_decode ($ b64 );
124+ if (File::write ($ file , $ decodedContent )) {
125+ $ savedFiles [] = $ file ;
126+ }
108127 }
109128 }
110129 return $ savedFiles ;
@@ -128,15 +147,24 @@ public function saveContentToFile(string $filename)
128147 }
129148
130149 if (!empty ($ lines )) {
131- file_put_contents ($ filename , implode (PHP_EOL , $ lines ));
150+ if (File::write ($ filename , implode (PHP_EOL , $ lines ))) {
151+ return [$ filename ];
152+ }
153+ }
154+ }
155+
156+ // For binary content (like audio files)
157+ if (isset ($ metadata ['data_type ' ]) && $ metadata ['data_type ' ] === 'binary_audio ' ) {
158+ if (File::write ($ filename , $ content )) {
132159 return [$ filename ];
133160 }
134161 }
135162
136163 // For all other content
137164 if ($ content !== null ) {
138- file_put_contents ($ filename , $ content );
139- return [$ filename ];
165+ if (File::write ($ filename , $ content )) {
166+ return [$ filename ];
167+ }
140168 }
141169
142170 return false ;
0 commit comments