Skip to content

Commit eabfddf

Browse files
committed
feat: enhance file processing with support for base64 and binary types, and add variable processing functionality
1 parent 6e72fde commit eabfddf

File tree

1 file changed

+95
-130
lines changed

1 file changed

+95
-130
lines changed

src/server.js

Lines changed: 95 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,16 @@ module.exports = async function file(
353353
}
354354

355355
async function getSource(path, mimeType, isSymlink) {
356-
let readType = "utf8";
357-
if (mimeType === "image/svg+xml") {
358-
readType = "utf8";
359-
} else if (/^(image|audio|video)\/[-+.\w]+/.test(mimeType)) {
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+
360+
let readType = "utf8"; // Default to utf8
361+
362+
if (base64Types.test(mimeType)) {
360363
readType = "base64";
364+
} else if (binaryTypes.test(mimeType)) {
365+
readType = "binary";
361366
}
362367

363368
if (isSymlink) path = await realpathAsync(path);
@@ -372,124 +377,100 @@ module.exports = async function file(
372377
* Store files by config sources
373378
**/
374379
async function runSources() {
375-
let updatedSources = [];
380+
let newConfig = require(configPath);
376381

377382
for (let i = 0; i < sources.length; i++) {
378-
const { array, object } = sources[i];
379-
380-
let source = { ...sources[i] };
381-
let keys = new Map();
382-
let response = {};
383-
let isMatch = false;
384-
385-
try {
386-
if (array) {
387-
if (!object) object = {};
388-
else
389-
for (const key of Object.keys(object)) {
390-
if (typeof object[key] != "string") continue;
391-
392-
let variables = object[key].match(
393-
/{{([A-Za-z0-9_.,\[\]\-\/ ]*)}}/g
394-
);
395-
if (variables) {
396-
let originalValue = object[key];
397-
keys.set(key, originalValue);
398-
let value = "";
399-
for (let variable of variables) {
400-
let entry = /{{\s*([\w\W]+)\s*}}/g.exec(
401-
variable
402-
);
403-
entry = entry[1].trim();
404-
if (entry) {
405-
if (!fs.existsSync(entry)) continue;
406-
407-
if (!isMatch) {
408-
const filePath = path.resolve(
409-
configDirectoryPath,
410-
entry
411-
);
412-
for (
413-
let i = 0;
414-
i < match.length;
415-
i++
416-
) {
417-
if (
418-
filePath.startsWith(
419-
match[i]
420-
)
421-
) {
422-
console.log(
423-
"Source saved",
424-
sources[i]
425-
);
426-
isMatch = true;
427-
break;
428-
}
429-
}
430-
}
431-
432-
let read_type = "utf8";
433-
const fileExtension =
434-
path.extname(entry);
435-
let mime_type =
436-
mimeTypes[fileExtension] ||
437-
"text/html";
438-
439-
if (
440-
/^(image|audio|video)\/[-+.\w]+/.test(
441-
mime_type
442-
)
443-
) {
444-
read_type = "base64";
445-
}
446-
447-
let binary = fs.readFileSync(entry);
448-
let content = new Buffer.from(
449-
binary
450-
).toString(read_type);
451-
if (content) value += content;
452-
// object[key] = object[key].replace(variable, content);
453-
}
454-
}
455-
object[key] = value;
456-
}
383+
let data = sources[i];
384+
385+
// Handle string values
386+
if (typeof data === "string") {
387+
let {value, filePath } = await processVariables(data);
388+
let response = await runStore(value);
389+
if (response && response.object && response.object[0]) {
390+
updateFilePath(filePath, response); // Call the new function to update the file path
391+
}
392+
} else if (data.array && data.object) {
393+
if (typeof data.object === "string") {
394+
let {value, filePath } = await processVariables(data.object);
395+
if (value) {
396+
let response = await runStore(value);
397+
if (response && response.object && response.object[0]) {
398+
updateFilePath(filePath, response.object);
457399
}
458-
459-
let data = { array, object };
460-
if (!object._id && object.pathname)
461-
data.$filter = {
462-
query: {
463-
$or: [{ pathname: object.pathname }]
464-
}
465-
};
466-
467-
if (match.length && isMatch)
468-
response = await runStore(data);
400+
}
401+
} else if (typeof data.object === "object" && data.object !== null) {
402+
for (const key in data) {
403+
let {value } = await processVariables(data[key]);
404+
if (data) {
405+
data.object[key] = value;
406+
}
407+
}
408+
let response = await runStore(data);
409+
if (response && response.object && response.object[0] && response.object[0]._id) {
410+
newConfig.sources[i].object._id = response.object[0]._id;
411+
}
469412
}
470-
} catch (err) {
471-
console.log(err);
472-
process.exit();
473413
}
474414

475-
if (
476-
response.object &&
477-
response.object[0] &&
478-
response.object[0]._id
479-
) {
480-
source.object._id = response.object[0]._id;
481-
}
415+
}
482416

483-
for (const [key, value] of keys) {
484-
source.object[key] = value;
485-
}
417+
return newConfig;
418+
}
419+
420+
async function processVariables(value) {
421+
let variableMatch = /{{\s*([\w\W]+)\s*}}/g.exec(value);
422+
if (!variableMatch) return { value, filePath: null };
423+
424+
let entry = variableMatch[1].trim();
425+
if (!fs.existsSync(entry)) return { value, filePath: null };
486426

487-
updatedSources.push(source);
427+
const filePath = path.resolve(configDirectoryPath, entry);
428+
429+
// Check if the file path matches any of the provided match patterns
430+
let isMatched = match.some((pattern) => filePath.startsWith(pattern));
431+
if (!isMatched) return { value, filePath: null };
432+
433+
// Read the file as is
434+
let content;
435+
try {
436+
const fileMimeType = mimeTypes[path.extname(entry)] || "text/plain";
437+
438+
if (fileMimeType === "application/json") {
439+
// Parse JSON files
440+
content = JSON.parse(fs.readFileSync(filePath, "utf8"));
441+
} else if (fileMimeType === "application/javascript" || fileMimeType === "text/javascript") {
442+
// For JavaScript files, require the file to execute exports
443+
content = require(filePath);
444+
} else {
445+
// For plain strings, read as UTF-8 without conversion
446+
content = fs.readFileSync(filePath, "utf8");
447+
}
448+
} catch (error) {
449+
console.error(`Failed to process file: ${filePath}`, error);
450+
return { value, filePath: null };
488451
}
489452

490-
return updatedSources;
453+
return { value: content, filePath };
491454
}
492455

456+
/**
457+
* Updates the file at the given file path with the provided data.
458+
* The data is saved as a JSON string.
459+
*
460+
* @param {string} filePath - The path of the file to update.
461+
* @param {object} data - The data to write to the file.
462+
*/
463+
function updateFilePath(filePath, data) {
464+
try {
465+
const jsonData = JSON.stringify(data, null, 4); // Format JSON with indentation
466+
fs.writeFileSync(filePath, jsonData, "utf8");
467+
console.log(`File updated successfully at: ${filePath}`);
468+
} catch (error) {
469+
console.error(`Failed to update file at: ${filePath}`, error);
470+
}
471+
}
472+
473+
493474
async function runStore(data) {
494475
try {
495476
let response;
@@ -517,28 +498,12 @@ module.exports = async function file(
517498
}
518499

519500
async function run() {
520-
if (directories) await runDirectories();
501+
if (directories) {
502+
await runDirectories();
503+
}
521504

522505
if (sources && sources.length) {
523-
let sources = await runSources();
524-
let newConfig = { ...CoCreateConfig };
525-
if (directories && directories.length)
526-
newConfig.directories = directories;
527-
528-
newConfig.sources = sources;
529-
530-
if (newConfig.repositories)
531-
newConfig.repositories.forEach((obj) => {
532-
for (const key in obj) {
533-
if (!["path", "repo", "exclude"].includes(key)) {
534-
delete obj[key];
535-
}
536-
}
537-
});
538-
539-
delete newConfig.url;
540-
delete newConfig.broadcast;
541-
506+
let newConfig = await runSources();
542507
fs.writeFileSync(
543508
configPath,
544509
`module.exports = ${JSON.stringify(newConfig, null, 4)};`

0 commit comments

Comments
 (0)