Skip to content

Commit 192337c

Browse files
committed
feat: enhance file handling by supporting base64 encoding for binary sources and improving file reading logic
1 parent eabfddf commit 192337c

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/server.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = /^(image|audio|video|font|application\/octet-stream|application\/x-font-ttf|application\/x-font-woff|application\/x-font-woff2|application\/x-font-opentype|application\/x-font-truetype|application\/x-font-eot)/;
358-
const binaryTypes = /^(application\/zip|application\/x-7z-compressed|application\/x-rar-compressed|application\/pdf)/;
359+
async function getSource(filePath, mimeType, isSymlink) {
360+
// 1. UPDATED: Includes standard font types and uses simpler matching
361+
const base64MimeTypes = /^(image|audio|video|font\/(woff2?|ttf|otf|eot)|application\/vnd\.ms-fontobject|application\/x-font-.*|application\/octet-stream)/;
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

Comments
 (0)