diff --git a/package.json b/package.json index b46c5a721..1bb185e05 100644 --- a/package.json +++ b/package.json @@ -51,11 +51,14 @@ "no-path-concat": "off", "no-var": "off" }, + "main": "server.js", "pkg": { - "assets": [ - "node_modules/open/xdg-open/", - "public", - "uploads" - ] - } + "assets": [ + "node_modules/**/*", + "poe_graphql/**/*" + ], + "scripts": [ + "server.js" + ] + } } diff --git a/server.js b/server.js index 7aee9bb00..69585629a 100644 --- a/server.js +++ b/server.js @@ -20,7 +20,10 @@ const cliArguments = yargs(hideBin(process.argv)) }).argv; // change all relative paths -process.chdir(__dirname) +// process.chdir(__dirname) +//console.log('cwd is: ',process.cwd()) //correct external project path +const path = require('path'); +process.chdir(path.dirname(process.execPath)); const express = require('express'); const compression = require('compression'); @@ -42,7 +45,7 @@ const encode = require('png-chunks-encode'); const PNGtext = require('png-chunk-text'); const jimp = require('jimp'); -const path = require('path'); +//const path = require('path'); const sanitize = require('sanitize-filename'); const mime = require('mime-types'); @@ -60,10 +63,10 @@ const utf8Encode = new TextEncoder(); const utf8Decode = new TextDecoder('utf-8', { ignoreBOM: true }); const commandExistsSync = require('command-exists').sync; -const config = require(path.join(__dirname, './config.conf')); +const config = require(path.join(process.cwd(), './config.conf')); const server_port = process.env.SILLY_TAVERN_PORT || config.port; -const whitelistPath = path.join(__dirname, "./whitelist.txt"); +const whitelistPath = path.join(process.cwd(), "./whitelist.txt"); let whitelist = config.whitelist; if (fs.existsSync(whitelistPath)) { @@ -254,7 +257,7 @@ app.use((req, res, next) => { console.log(filePath); fs.access(filePath, fs.constants.R_OK, (err) => { if (!err) { - res.sendFile(filePath, { root: __dirname }); + res.sendFile(filePath, { root: process.cwd() }); } else { res.send('Character not found: ' + filePath); //next(); @@ -265,10 +268,10 @@ app.use((req, res, next) => { } }); -app.use(express.static(__dirname + "/public", { refresh: true })); +app.use(express.static(process.cwd() + "/public", { refresh: true })); app.use('/backgrounds', (req, res) => { - const filePath = decodeURIComponent(path.join(__dirname, 'public/backgrounds', req.url.replace(/%20/g, ' '))); + const filePath = decodeURIComponent(path.join(process.cwd(), 'public/backgrounds', req.url.replace(/%20/g, ' '))); fs.readFile(filePath, (err, data) => { if (err) { res.status(404).send('File not found'); @@ -280,7 +283,7 @@ app.use('/backgrounds', (req, res) => { }); app.use('/characters', (req, res) => { - const filePath = decodeURIComponent(path.join(__dirname, charactersPath, req.url.replace(/%20/g, ' '))); + const filePath = decodeURIComponent(path.join(process.cwd(), charactersPath, req.url.replace(/%20/g, ' '))); fs.readFile(filePath, (err, data) => { if (err) { res.status(404).send('File not found'); @@ -291,16 +294,16 @@ app.use('/characters', (req, res) => { }); app.use(multer({ dest: "uploads" }).single("avatar")); app.get("/", function (request, response) { - response.sendFile(__dirname + "/public/index.html"); + response.sendFile(process.cwd() + "/public/index.html"); }); app.get("/notes/*", function (request, response) { - response.sendFile(__dirname + "/public" + request.url + ".html"); + response.sendFile(process.cwd() + "/public" + request.url + ".html"); }); app.get('/get_faq', function (_, response) { - response.sendFile(__dirname + "/faq.md"); + response.sendFile(process.cwd() + "/faq.md"); }); app.get('/get_readme', function (_, response) { - response.sendFile(__dirname + "/readme.md"); + response.sendFile(process.cwd() + "/readme.md"); }); app.get('/deviceinfo', function (request, response) { const userAgent = request.header('user-agent'); @@ -315,7 +318,7 @@ app.get('/version', function (_, response) { pkgVersion = pkgJson.version; if (commandExistsSync('git')) { gitRevision = require('child_process') - .execSync('git rev-parse --short HEAD', { cwd: __dirname }) + .execSync('git rev-parse --short HEAD', { cwd: process.cwd() }) .toString().trim(); gitBranch = require('child_process') @@ -1685,7 +1688,7 @@ app.post("/exportcharacter", jsonParser, async function (request, response) { switch (request.body.format) { case 'png': - return response.sendFile(filename, { root: __dirname }); + return response.sendFile(filename, { root: process.cwd() }); case 'json': { try { let json = await charaRead(filename); @@ -1715,7 +1718,7 @@ app.post("/exportcharacter", jsonParser, async function (request, response) { await webp.cwebp(filename, inputWebpPath, '-q 95'); await webp.webpmux_add(inputWebpPath, outputWebpPath, metadataPath, 'exif'); - response.sendFile(outputWebpPath, { root: __dirname }, () => { + response.sendFile(outputWebpPath, { root: process.cwd() }, () => { fs.rmSync(inputWebpPath); fs.rmSync(metadataPath); fs.rmSync(outputWebpPath); @@ -2360,7 +2363,7 @@ app.get('/thumbnail', jsonParser, async function (request, response) { if (config.disableThumbnails == true) { const pathToOriginalFile = path.join(getOriginalFolder(type), file); - return response.sendFile(pathToOriginalFile, { root: __dirname }); + return response.sendFile(pathToOriginalFile, { root: process.cwd() }); } const pathToCachedFile = await generateThumbnail(type, file); @@ -2369,7 +2372,7 @@ app.get('/thumbnail', jsonParser, async function (request, response) { return response.sendStatus(404); } - return response.sendFile(pathToCachedFile, { root: __dirname }); + return response.sendFile(pathToCachedFile, { root: process.cwd() }); }); /* OpenAI */ diff --git a/src/middleware/basicAuthMiddleware.js b/src/middleware/basicAuthMiddleware.js index 2f368214c..a5b1b3459 100644 --- a/src/middleware/basicAuthMiddleware.js +++ b/src/middleware/basicAuthMiddleware.js @@ -3,9 +3,11 @@ * allow access to the endpoint after successful authentication. */ -const {dirname} = require('path'); -const appDir = dirname(require.main.filename); -const config = require(appDir + '/config.conf'); +//const {dirname} = require('path'); +//const appDir = dirname(require.main.filename); +//const config = require(appDir + '/config.conf'); +const path = require('path'); +const config = require(path.join(process.cwd(), './config.conf')); const unauthorizedResponse = (res) => { res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"'); @@ -36,4 +38,4 @@ const basicAuthMiddleware = function (request, response, callback) { } } -module.exports = basicAuthMiddleware; \ No newline at end of file +module.exports = basicAuthMiddleware;