added marketplace

This commit is contained in:
2025-12-04 18:32:41 +01:00
parent 5cdf53b7ce
commit c0ffc6c108
7 changed files with 1009 additions and 27 deletions

View File

@@ -20,30 +20,7 @@ async function loadExtensions() {
for (const file of files) {
if (file.endsWith('.js')) {
const filePath = path.join(extensionsDir, file);
try {
delete require.cache[require.resolve(filePath)];
const ExtensionClass = require(filePath);
const instance = typeof ExtensionClass === 'function'
? new ExtensionClass()
: (ExtensionClass.default ? new ExtensionClass.default() : null);
if (instance &&
(instance.type === "anime-board" ||
instance.type === "book-board" ||
instance.type === "image-board")) {
const name = instance.constructor.name;
extensions.set(name, instance);
instance.scrape = scrape;
console.log(`📦 Loaded Extension: ${name}`);
}
} catch (e) {
console.error(`❌ Failed to load extension ${file}:`, e.message);
}
await loadExtension(file);
}
}
@@ -51,7 +28,6 @@ async function loadExtensions() {
try {
const loaded = Array.from(extensions.keys());
const rows = await queryAll("SELECT DISTINCT ext_name FROM extension");
for (const row of rows) {
@@ -69,6 +45,114 @@ async function loadExtensions() {
}
}
async function loadExtension(fileName) {
const homeDir = os.homedir();
const extensionsDir = path.join(homeDir, 'WaifuBoards', 'extensions');
const filePath = path.join(extensionsDir, fileName);
if (!fs.existsSync(filePath)) {
throw new Error("Extension file does not exist");
}
try {
delete require.cache[require.resolve(filePath)];
const ExtensionClass = require(filePath);
const instance = typeof ExtensionClass === 'function'
? new ExtensionClass()
: (ExtensionClass.default ? new ExtensionClass.default() : null);
if (!instance) {
throw new Error("Invalid extension export");
}
if (
instance.type !== "anime-board" &&
instance.type !== "book-board" &&
instance.type !== "image-board"
) {
throw new Error(`Invalid extension type: ${instance.type}`);
}
const name = instance.constructor.name;
instance.scrape = scrape;
extensions.set(name, instance);
console.log(`📦 Installed & Loaded Extension: ${name}`);
return name;
} catch (err) {
throw new Error(`LoadExtension failed: ${err.message}`);
}
}
const https = require('https');
async function saveExtensionFile(fileName, downloadUrl) {
const homeDir = os.homedir();
const extensionsDir = path.join(homeDir, 'WaifuBoards', 'extensions');
const filePath = path.join(extensionsDir, fileName);
const fullUrl = downloadUrl;
if (!fs.existsSync(extensionsDir)) {
fs.mkdirSync(extensionsDir, { recursive: true });
}
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(filePath);
https.get(fullUrl, async (response) => {
if (response.statusCode !== 200) {
return reject(new Error(`Download failed: ${response.statusCode}`));
}
response.pipe(file);
file.on('finish', async () => {
file.close(async () => {
try {
await loadExtension(fileName);
resolve();
} catch (err) {
if (fs.existsSync(filePath)) {
await fs.promises.unlink(filePath);
}
reject(new Error(`Load failed, file rolled back: ${err.message}`));
}
});
});
}).on('error', async (err) => {
if (fs.existsSync(filePath)) {
await fs.promises.unlink(filePath);
}
reject(err);
});
});
}
async function deleteExtensionFile(fileName) {
const homeDir = os.homedir();
const extensionsDir = path.join(homeDir, 'WaifuBoards', 'extensions');
const filePath = path.join(extensionsDir, fileName);
const extName = fileName.replace(".js", "");
for (const key of extensions.keys()) {
if (key.toLowerCase() === extName) {
extensions.delete(key);
console.log(`🗑️ Removed from memory: ${key}`);
break;
}
}
if (fs.existsSync(filePath)) {
await fs.promises.unlink(filePath);
console.log(`🗑️ Deleted file: ${fileName}`);
}
}
function getExtension(name) {
return extensions.get(name);
}
@@ -118,5 +202,7 @@ module.exports = {
getExtensionsList,
getAnimeExtensionsMap,
getBookExtensionsMap,
getGalleryExtensionsMap
getGalleryExtensionsMap,
saveExtensionFile,
deleteExtensionFile
};