Move client-relative path logic to helper function
This commit is contained in:
parent
39d771cc4a
commit
5096e70c11
|
@ -48,7 +48,7 @@ const { jsonParser, urlencodedParser } = require('./src/express-common.js');
|
||||||
const contentManager = require('./src/endpoints/content-manager');
|
const contentManager = require('./src/endpoints/content-manager');
|
||||||
const statsHelpers = require('./statsHelpers.js');
|
const statsHelpers = require('./statsHelpers.js');
|
||||||
const { readSecret, migrateSecrets, SECRET_KEYS } = require('./src/endpoints/secrets');
|
const { readSecret, migrateSecrets, SECRET_KEYS } = require('./src/endpoints/secrets');
|
||||||
const { delay, getVersion, getConfigValue, color, uuidv4, humanizedISO8601DateTime, tryParse } = require('./src/util');
|
const { delay, getVersion, getConfigValue, color, uuidv4, humanizedISO8601DateTime, tryParse, clientRelativePath } = require('./src/util');
|
||||||
const { invalidateThumbnail, ensureThumbnailCache } = require('./src/endpoints/thumbnails');
|
const { invalidateThumbnail, ensureThumbnailCache } = require('./src/endpoints/thumbnails');
|
||||||
const { getTokenizerModel, getTiktokenTokenizer, loadTokenizers, TEXT_COMPLETION_MODELS, getSentencepiceTokenizer, sentencepieceTokenizers } = require('./src/endpoints/tokenizers');
|
const { getTokenizerModel, getTiktokenTokenizer, loadTokenizers, TEXT_COMPLETION_MODELS, getSentencepiceTokenizer, sentencepieceTokenizers } = require('./src/endpoints/tokenizers');
|
||||||
const { convertClaudePrompt } = require('./src/chat-completion');
|
const { convertClaudePrompt } = require('./src/chat-completion');
|
||||||
|
@ -1587,10 +1587,7 @@ app.post('/uploadimage', jsonParser, async (request, response) => {
|
||||||
ensureDirectoryExistence(pathToNewFile);
|
ensureDirectoryExistence(pathToNewFile);
|
||||||
const imageBuffer = Buffer.from(base64Data, 'base64');
|
const imageBuffer = Buffer.from(base64Data, 'base64');
|
||||||
await fs.promises.writeFile(pathToNewFile, imageBuffer);
|
await fs.promises.writeFile(pathToNewFile, imageBuffer);
|
||||||
// send the path to the image, relative to the client folder, which means removing the first folder from the path which is 'public'
|
response.send({ path: clientRelativePath(pathToNewFile) });
|
||||||
// Use forward slashes, even on Windows
|
|
||||||
pathToNewFile = pathToNewFile.split(path.sep).slice(1).join('/');
|
|
||||||
response.send({ path: pathToNewFile });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
response.status(500).send({ error: 'Failed to save the image' });
|
response.status(500).send({ error: 'Failed to save the image' });
|
||||||
|
|
|
@ -6,6 +6,7 @@ const fetch = require('node-fetch').default;
|
||||||
const { finished } = require('stream/promises');
|
const { finished } = require('stream/promises');
|
||||||
const { DIRECTORIES, UNSAFE_EXTENSIONS } = require('../constants');
|
const { DIRECTORIES, UNSAFE_EXTENSIONS } = require('../constants');
|
||||||
const { jsonParser } = require('../express-common');
|
const { jsonParser } = require('../express-common');
|
||||||
|
const { clientRelativePath } = require('../util');
|
||||||
|
|
||||||
const VALID_CATEGORIES = ['bgm', 'ambient', 'blip', 'live2d'];
|
const VALID_CATEGORIES = ['bgm', 'ambient', 'blip', 'live2d'];
|
||||||
|
|
||||||
|
@ -99,10 +100,9 @@ router.post('/get', jsonParser, async (_, response) => {
|
||||||
const files = getFiles(live2d_folder);
|
const files = getFiles(live2d_folder);
|
||||||
//console.debug("FILE FOUND:",files)
|
//console.debug("FILE FOUND:",files)
|
||||||
for (let file of files) {
|
for (let file of files) {
|
||||||
file = path.normalize(file.replace('public' + path.sep, ''));
|
|
||||||
if (file.includes('model') && file.endsWith('.json')) {
|
if (file.includes('model') && file.endsWith('.json')) {
|
||||||
//console.debug("Asset live2d model found:",file)
|
//console.debug("Asset live2d model found:",file)
|
||||||
output[folder].push(path.normalize(file).replace(/\\/g, '/'));
|
output[folder].push(clientRelativePath(file));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
@ -115,7 +115,7 @@ router.post('/get', jsonParser, async (_, response) => {
|
||||||
});
|
});
|
||||||
output[folder] = [];
|
output[folder] = [];
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
output[folder].push(path.join('assets', folder, file).replace(/\\/g, '/'));
|
output[folder].push(`assets/${folder}/${file}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ const router = express.Router();
|
||||||
const { validateAssetFileName } = require('./assets');
|
const { validateAssetFileName } = require('./assets');
|
||||||
const { jsonParser } = require('../express-common');
|
const { jsonParser } = require('../express-common');
|
||||||
const { DIRECTORIES } = require('../constants');
|
const { DIRECTORIES } = require('../constants');
|
||||||
|
const { clientRelativePath } = require('../util');
|
||||||
|
|
||||||
router.post('/upload', jsonParser, async (request, response) => {
|
router.post('/upload', jsonParser, async (request, response) => {
|
||||||
try {
|
try {
|
||||||
|
@ -23,7 +24,7 @@ router.post('/upload', jsonParser, async (request, response) => {
|
||||||
|
|
||||||
const pathToUpload = path.join(DIRECTORIES.files, request.body.name);
|
const pathToUpload = path.join(DIRECTORIES.files, request.body.name);
|
||||||
writeFileSyncAtomic(pathToUpload, request.body.data, 'base64');
|
writeFileSyncAtomic(pathToUpload, request.body.data, 'base64');
|
||||||
const url = path.normalize(pathToUpload.replace('public' + path.sep, '').replace(/\\/g, '/'));
|
const url = clientRelativePath(pathToUpload);
|
||||||
return response.send({ path: url });
|
return response.send({ path: url });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
|
11
src/util.js
11
src/util.js
|
@ -288,6 +288,16 @@ function tryParse(str) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes a path to a client-accessible file in the `public` folder and converts it to a relative URL segment that the
|
||||||
|
* client can fetch it from. This involves stripping the `public/` prefix and always using `/` as the separator.
|
||||||
|
* @param {string} inputPath The path to be converted.
|
||||||
|
* @returns The relative URL path from which the client can access the file.
|
||||||
|
*/
|
||||||
|
function clientRelativePath(inputPath) {
|
||||||
|
return path.normalize(inputPath).split(path.sep).slice(1).join('/');
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getConfig,
|
getConfig,
|
||||||
getConfigValue,
|
getConfigValue,
|
||||||
|
@ -302,4 +312,5 @@ module.exports = {
|
||||||
uuidv4,
|
uuidv4,
|
||||||
humanizedISO8601DateTime,
|
humanizedISO8601DateTime,
|
||||||
tryParse,
|
tryParse,
|
||||||
|
clientRelativePath,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue