Fix double file extension on uploaded images

This commit is contained in:
valadaptive 2023-12-06 17:29:10 -05:00
parent 66da13b1bb
commit 9c850b0b0c
2 changed files with 15 additions and 3 deletions

View File

@ -48,7 +48,7 @@ const { jsonParser, urlencodedParser } = require('./src/express-common.js');
const contentManager = require('./src/endpoints/content-manager');
const statsHelpers = require('./statsHelpers.js');
const { readSecret, migrateSecrets, SECRET_KEYS } = require('./src/endpoints/secrets');
const { delay, getVersion, getConfigValue, color, uuidv4, humanizedISO8601DateTime, tryParse, clientRelativePath } = require('./src/util');
const { delay, getVersion, getConfigValue, color, uuidv4, humanizedISO8601DateTime, tryParse, clientRelativePath, removeFileExtension } = require('./src/util');
const { invalidateThumbnail, ensureThumbnailCache } = require('./src/endpoints/thumbnails');
const { getTokenizerModel, getTiktokenTokenizer, loadTokenizers, TEXT_COMPLETION_MODELS, getSentencepiceTokenizer, sentencepieceTokenizers } = require('./src/endpoints/tokenizers');
const { convertClaudePrompt } = require('./src/chat-completion');
@ -1573,9 +1573,11 @@ app.post('/uploadimage', jsonParser, async (request, response) => {
}
// Constructing filename and path
let filename = `${Date.now()}.${format}`;
let filename;
if (request.body.filename) {
filename = `${request.body.filename}.${format}`;
filename = `${removeFileExtension(request.body.filename)}.${format}`;
} else {
filename = `${Date.now()}.${format}`;
}
// if character is defined, save to a sub folder for that character

View File

@ -298,6 +298,15 @@ function clientRelativePath(inputPath) {
return path.normalize(inputPath).split(path.sep).slice(1).join('/');
}
/**
* Strip the last file extension from a given file name. If there are multiple extensions, only the last is removed.
* @param {string} filename The file name to remove the extension from.
* @returns The file name, sans extension
*/
function removeFileExtension(filename) {
return filename.replace(/\.[^.]+$/, '');
}
module.exports = {
getConfig,
getConfigValue,
@ -313,4 +322,5 @@ module.exports = {
humanizedISO8601DateTime,
tryParse,
clientRelativePath,
removeFileExtension,
};