@@ -258,7 +258,7 @@ module.exports = async function file(
258258
259259 let values = {
260260 "{{name}}" : name || "" ,
261- "{{source}}" : source || "" ,
261+ "{{source}}" : Buffer . isBuffer ( source ) ? `data: ${ mimeType } ;base64, ${ source . toString ( 'base64' ) } ` : source || "" ,
262262 "{{directory}}" : directoryName || "" ,
263263 "{{path}}" : Path || "" ,
264264 "{{pathname}}" : pathname ,
@@ -294,7 +294,7 @@ module.exports = async function file(
294294 try {
295295 // Call your AI translation service
296296 const translations = await options . translate (
297- source ,
297+ Buffer . isBuffer ( source ) ? source . toString ( 'utf-8' ) : source ,
298298 directory . languages
299299 ) ;
300300 newObject . object . translations = translations ;
@@ -315,9 +315,13 @@ module.exports = async function file(
315315 ) ;
316316 if ( variables ) {
317317 for ( let variable of variables ) {
318+ let replacement = values [ variable ] ;
319+ if ( key === 'src' && variable === '{{source}}' && Buffer . isBuffer ( source ) ) {
320+ replacement = `data:${ mimeType } ;base64,${ source . toString ( 'base64' ) } ` ;
321+ }
318322 newObject . object [ key ] = newObject . object [
319323 key
320- ] . replace ( variable , values [ variable ] ) ;
324+ ] . replace ( variable , replacement ) ;
321325 }
322326 }
323327 }
@@ -352,25 +356,37 @@ module.exports = async function file(
352356 // console.log(...errorLog)
353357 }
354358
355- async function getSource ( path , mimeType , isSymlink ) {
356- // Define categories for encoding types
357- const base64Types = / ^ ( i m a g e | a u d i o | v i d e o | f o n t | a p p l i c a t i o n \/ o c t e t - s t r e a m | a p p l i c a t i o n \/ x - f o n t - t t f | a p p l i c a t i o n \/ x - f o n t - w o f f | a p p l i c a t i o n \/ x - f o n t - w o f f 2 | a p p l i c a t i o n \/ x - f o n t - o p e n t y p e | a p p l i c a t i o n \/ x - f o n t - t r u e t y p e | a p p l i c a t i o n \/ x - f o n t - e o t ) / ;
358- const binaryTypes = / ^ ( a p p l i c a t i o n \/ z i p | a p p l i c a t i o n \/ x - 7 z - c o m p r e s s e d | a p p l i c a t i o n \/ x - r a r - c o m p r e s s e d | a p p l i c a t i o n \/ p d f ) / ;
359+ async function getSource ( filePath , mimeType , isSymlink ) {
360+ // 1. UPDATED: Includes standard font types and uses simpler matching
361+ const base64MimeTypes = / ^ ( i m a g e | a u d i o | v i d e o | f o n t \/ ( w o f f 2 ? | t t f | o t f | e o t ) | a p p l i c a t i o n \/ v n d \. m s - f o n t o b j e c t | a p p l i c a t i o n \/ x - f o n t - .* | a p p l i c a t i o n \/ o c t e t - s t r e a m ) / ;
359362
360- let readType = "utf8" ; // Default to utf8
363+ // We only care if it needs to be Base64-encoded for a Data URI.
364+ const needsBase64 = base64MimeTypes . test ( mimeType ) ;
361365
362- if ( base64Types . test ( mimeType ) ) {
363- readType = "base64" ;
364- } else if ( binaryTypes . test ( mimeType ) ) {
365- readType = "binary" ;
366+ let resolvedPath = filePath ;
367+ if ( isSymlink ) {
368+ // Use promises for realpath
369+ resolvedPath = await realpathAsync ( filePath ) ;
366370 }
367371
368- if ( isSymlink ) path = await realpathAsync ( path ) ;
369-
370- let binary = fs . readFileSync ( path ) ;
371- let content = new Buffer . from ( binary ) . toString ( readType ) ;
372+ // 2. READ: Always read the file as a raw Buffer (omitting encoding)
373+ // This gives us the raw bytes, which is the safest start for any file.
374+ let fileBuffer ;
375+ try {
376+ fileBuffer = await fs . promises . readFile ( resolvedPath ) ;
377+ } catch ( error ) {
378+ console . error ( `Error reading file: ${ resolvedPath } ` , error ) ;
379+ return "" ; // Return empty string or handle error as appropriate
380+ }
372381
373- return content ;
382+ if ( needsBase64 ) {
383+ // 3. RETURN BUFFER: Return the raw buffer for binary files.
384+ return fileBuffer ;
385+ } else {
386+ // 4. HANDLE TEXT/OTHER:
387+ // For files not intended for Base64, convert the Buffer to a string using 'utf8'.
388+ return fileBuffer . toString ( 'utf8' ) ;
389+ }
374390 }
375391
376392 /**
0 commit comments