#2422 Move uploads under the data root

This commit is contained in:
Cohee 2024-06-26 23:22:42 +03:00
parent 54fb7a9030
commit 5b002c6e46
10 changed files with 27 additions and 24 deletions

View File

@ -8,3 +8,5 @@ Start.bat
cloudflared.exe cloudflared.exe
access.log access.log
/data /data
/cache
.DS_Store

View File

@ -5,4 +5,6 @@ node_modules/
secrets.json secrets.json
/dist /dist
/backups/ /backups/
/data
/cache
access.log access.log

View File

@ -136,7 +136,7 @@ const disableCsrf = cliArguments.disableCsrf ?? getConfigValue('disableCsrfProte
const basicAuthMode = cliArguments.basicAuthMode ?? getConfigValue('basicAuthMode', DEFAULT_BASIC_AUTH); const basicAuthMode = cliArguments.basicAuthMode ?? getConfigValue('basicAuthMode', DEFAULT_BASIC_AUTH);
const enableAccounts = getConfigValue('enableUserAccounts', DEFAULT_ACCOUNTS); const enableAccounts = getConfigValue('enableUserAccounts', DEFAULT_ACCOUNTS);
const { UPLOADS_PATH } = require('./src/constants'); const uploadsPath = path.join(dataRoot, require('./src/constants').UPLOADS_DIRECTORY);
// CORS Settings // // CORS Settings //
const CORS = cors({ const CORS = cors({
@ -286,7 +286,7 @@ app.use(userModule.requireLoginMiddleware);
app.get('/api/ping', (_, response) => response.sendStatus(204)); app.get('/api/ping', (_, response) => response.sendStatus(204));
// File uploads // File uploads
app.use(multer({ dest: UPLOADS_PATH, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar')); app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar'));
app.use(require('./src/middleware/multerMonkeyPatch')); app.use(require('./src/middleware/multerMonkeyPatch'));
// User data mount // User data mount
@ -303,8 +303,8 @@ app.get('/version', async function (_, response) {
function cleanUploads() { function cleanUploads() {
try { try {
if (fs.existsSync(UPLOADS_PATH)) { if (fs.existsSync(uploadsPath)) {
const uploads = fs.readdirSync(UPLOADS_PATH); const uploads = fs.readdirSync(uploadsPath);
if (!uploads.length) { if (!uploads.length) {
return; return;
@ -312,7 +312,7 @@ function cleanUploads() {
console.debug(`Cleaning uploads folder (${uploads.length} files)`); console.debug(`Cleaning uploads folder (${uploads.length} files)`);
uploads.forEach(file => { uploads.forEach(file => {
const pathToFile = path.join(UPLOADS_PATH, file); const pathToFile = path.join(uploadsPath, file);
fs.unlinkSync(pathToFile); fs.unlinkSync(pathToFile);
}); });
} }

View File

@ -196,7 +196,10 @@ const CHAT_COMPLETION_SOURCES = {
GROQ: 'groq', GROQ: 'groq',
}; };
const UPLOADS_PATH = './uploads'; /**
* Path to multer file uploads under the data root.
*/
const UPLOADS_DIRECTORY = '_uploads';
// TODO: this is copied from the client code; there should be a way to de-duplicate it eventually // TODO: this is copied from the client code; there should be a way to de-duplicate it eventually
const TEXTGEN_TYPES = { const TEXTGEN_TYPES = {
@ -364,7 +367,7 @@ module.exports = {
PUBLIC_DIRECTORIES, PUBLIC_DIRECTORIES,
USER_DIRECTORY_TEMPLATE, USER_DIRECTORY_TEMPLATE,
UNSAFE_EXTENSIONS, UNSAFE_EXTENSIONS,
UPLOADS_PATH, UPLOADS_DIRECTORY,
GEMINI_SAFETY, GEMINI_SAFETY,
BISON_SAFETY, BISON_SAFETY,
TEXTGEN_TYPES, TEXTGEN_TYPES,

View File

@ -4,7 +4,7 @@ const fs = require('fs');
const sanitize = require('sanitize-filename'); const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync; const writeFileAtomicSync = require('write-file-atomic').sync;
const { jsonParser, urlencodedParser } = require('../express-common'); const { jsonParser, urlencodedParser } = require('../express-common');
const { AVATAR_WIDTH, AVATAR_HEIGHT, UPLOADS_PATH } = require('../constants'); const { AVATAR_WIDTH, AVATAR_HEIGHT } = require('../constants');
const { getImages, tryParse } = require('../util'); const { getImages, tryParse } = require('../util');
// image processing related library imports // image processing related library imports
@ -39,7 +39,7 @@ router.post('/upload', urlencodedParser, async (request, response) => {
if (!request.file) return response.sendStatus(400); if (!request.file) return response.sendStatus(400);
try { try {
const pathToUpload = path.join(UPLOADS_PATH, request.file.filename); const pathToUpload = path.join(request.file.destination, request.file.filename);
const crop = tryParse(request.query.crop); const crop = tryParse(request.query.crop);
let rawImg = await jimp.read(pathToUpload); let rawImg = await jimp.read(pathToUpload);

View File

@ -4,7 +4,6 @@ const express = require('express');
const sanitize = require('sanitize-filename'); const sanitize = require('sanitize-filename');
const { jsonParser, urlencodedParser } = require('../express-common'); const { jsonParser, urlencodedParser } = require('../express-common');
const { UPLOADS_PATH } = require('../constants');
const { invalidateThumbnail } = require('./thumbnails'); const { invalidateThumbnail } = require('./thumbnails');
const { getImages } = require('../util'); const { getImages } = require('../util');
@ -60,7 +59,7 @@ router.post('/rename', jsonParser, function (request, response) {
router.post('/upload', urlencodedParser, function (request, response) { router.post('/upload', urlencodedParser, function (request, response) {
if (!request.body || !request.file) return response.sendStatus(400); if (!request.body || !request.file) return response.sendStatus(400);
const img_path = path.join(UPLOADS_PATH, request.file.filename); const img_path = path.join(request.file.destination, request.file.filename);
const filename = request.file.originalname; const filename = request.file.originalname;
try { try {

View File

@ -11,7 +11,7 @@ const mime = require('mime-types');
const jimp = require('jimp'); const jimp = require('jimp');
const { UPLOADS_PATH, AVATAR_WIDTH, AVATAR_HEIGHT } = require('../constants'); const { AVATAR_WIDTH, AVATAR_HEIGHT } = require('../constants');
const { jsonParser, urlencodedParser } = require('../express-common'); const { jsonParser, urlencodedParser } = require('../express-common');
const { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer } = require('../util'); const { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer } = require('../util');
const { TavernCardValidator } = require('../validator/TavernCardValidator'); const { TavernCardValidator } = require('../validator/TavernCardValidator');
@ -729,7 +729,7 @@ router.post('/create', urlencodedParser, async function (request, response) {
return response.send(avatarName); return response.send(avatarName);
} else { } else {
const crop = tryParse(request.query.crop); const crop = tryParse(request.query.crop);
const uploadPath = path.join(UPLOADS_PATH, request.file.filename); const uploadPath = path.join(request.file.destination, request.file.filename);
await writeCharacterData(uploadPath, char, internalName, request, crop); await writeCharacterData(uploadPath, char, internalName, request, crop);
fs.unlinkSync(uploadPath); fs.unlinkSync(uploadPath);
return response.send(avatarName); return response.send(avatarName);
@ -812,7 +812,7 @@ router.post('/edit', urlencodedParser, async function (request, response) {
await writeCharacterData(avatarPath, char, targetFile, request); await writeCharacterData(avatarPath, char, targetFile, request);
} else { } else {
const crop = tryParse(request.query.crop); const crop = tryParse(request.query.crop);
const newAvatarPath = path.join(UPLOADS_PATH, request.file.filename); const newAvatarPath = path.join(request.file.destination, request.file.filename);
invalidateThumbnail(request.user.directories, 'avatar', request.body.avatar_url); invalidateThumbnail(request.user.directories, 'avatar', request.body.avatar_url);
await writeCharacterData(newAvatarPath, char, targetFile, request, crop); await writeCharacterData(newAvatarPath, char, targetFile, request, crop);
fs.unlinkSync(newAvatarPath); fs.unlinkSync(newAvatarPath);
@ -1096,7 +1096,7 @@ function getPreservedName(request) {
router.post('/import', urlencodedParser, async function (request, response) { router.post('/import', urlencodedParser, async function (request, response) {
if (!request.body || !request.file) return response.sendStatus(400); if (!request.body || !request.file) return response.sendStatus(400);
const uploadPath = path.join(UPLOADS_PATH, request.file.filename); const uploadPath = path.join(request.file.destination, request.file.filename);
const format = request.body.file_type; const format = request.body.file_type;
const preservedFileName = getPreservedName(request); const preservedFileName = getPreservedName(request);

View File

@ -6,7 +6,6 @@ const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync; const writeFileAtomicSync = require('write-file-atomic').sync;
const { jsonParser, urlencodedParser } = require('../express-common'); const { jsonParser, urlencodedParser } = require('../express-common');
const { UPLOADS_PATH } = require('../constants');
const { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, removeOldBackups } = require('../util'); const { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, removeOldBackups } = require('../util');
/** /**
@ -323,7 +322,7 @@ router.post('/group/import', urlencodedParser, function (request, response) {
} }
const chatname = humanizedISO8601DateTime(); const chatname = humanizedISO8601DateTime();
const pathToUpload = path.join(UPLOADS_PATH, filedata.filename); const pathToUpload = path.join(filedata.destination, filedata.filename);
const pathToNewFile = path.join(request.user.directories.groupChats, `${chatname}.jsonl`); const pathToNewFile = path.join(request.user.directories.groupChats, `${chatname}.jsonl`);
fs.copyFileSync(pathToUpload, pathToNewFile); fs.copyFileSync(pathToUpload, pathToNewFile);
fs.unlinkSync(pathToUpload); fs.unlinkSync(pathToUpload);
@ -347,7 +346,7 @@ router.post('/import', urlencodedParser, function (request, response) {
} }
try { try {
const data = fs.readFileSync(path.join(UPLOADS_PATH, request.file.filename), 'utf8'); const data = fs.readFileSync(path.join(request.file.destination, request.file.filename), 'utf8');
if (format === 'json') { if (format === 'json') {
const jsonData = JSON.parse(data); const jsonData = JSON.parse(data);
@ -388,7 +387,7 @@ router.post('/import', urlencodedParser, function (request, response) {
if (jsonData.user_name !== undefined || jsonData.name !== undefined) { if (jsonData.user_name !== undefined || jsonData.name !== undefined) {
const fileName = `${characterName} - ${humanizedISO8601DateTime()} imported.jsonl`; const fileName = `${characterName} - ${humanizedISO8601DateTime()} imported.jsonl`;
const filePath = path.join(request.user.directories.chats, avatarUrl, fileName); const filePath = path.join(request.user.directories.chats, avatarUrl, fileName);
fs.copyFileSync(path.join(UPLOADS_PATH, request.file.filename), filePath); fs.copyFileSync(path.join(request.file.destination, request.file.filename), filePath);
response.send({ res: true }); response.send({ res: true });
} else { } else {
console.log('Incorrect chat format .jsonl'); console.log('Incorrect chat format .jsonl');

View File

@ -5,7 +5,6 @@ const express = require('express');
const mime = require('mime-types'); const mime = require('mime-types');
const sanitize = require('sanitize-filename'); const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync; const writeFileAtomicSync = require('write-file-atomic').sync;
const { UPLOADS_PATH } = require('../constants');
const { getImageBuffers } = require('../util'); const { getImageBuffers } = require('../util');
const { jsonParser, urlencodedParser } = require('../express-common'); const { jsonParser, urlencodedParser } = require('../express-common');
@ -190,7 +189,7 @@ router.post('/upload-zip', urlencodedParser, async (request, response) => {
return response.sendStatus(404); return response.sendStatus(404);
} }
const spritePackPath = path.join(UPLOADS_PATH, file.filename); const spritePackPath = path.join(file.destination, file.filename);
const sprites = await getImageBuffers(spritePackPath); const sprites = await getImageBuffers(spritePackPath);
const files = fs.readdirSync(spritesPath); const files = fs.readdirSync(spritesPath);
@ -248,7 +247,7 @@ router.post('/upload', urlencodedParser, async (request, response) => {
} }
const filename = label + path.parse(file.originalname).ext; const filename = label + path.parse(file.originalname).ext;
const spritePath = path.join(UPLOADS_PATH, file.filename); const spritePath = path.join(file.destination, file.filename);
const pathToFile = path.join(spritesPath, filename); const pathToFile = path.join(spritesPath, filename);
// Copy uploaded file to sprites folder // Copy uploaded file to sprites folder
fs.cpSync(spritePath, pathToFile); fs.cpSync(spritePath, pathToFile);

View File

@ -5,7 +5,6 @@ const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync; const writeFileAtomicSync = require('write-file-atomic').sync;
const { jsonParser, urlencodedParser } = require('../express-common'); const { jsonParser, urlencodedParser } = require('../express-common');
const { UPLOADS_PATH } = require('../constants');
/** /**
* Reads a World Info file and returns its contents * Reads a World Info file and returns its contents
@ -74,7 +73,7 @@ router.post('/import', urlencodedParser, (request, response) => {
if (request.body.convertedData) { if (request.body.convertedData) {
fileContents = request.body.convertedData; fileContents = request.body.convertedData;
} else { } else {
const pathToUpload = path.join(UPLOADS_PATH, request.file.filename); const pathToUpload = path.join(request.file.destination, request.file.filename);
fileContents = fs.readFileSync(pathToUpload, 'utf8'); fileContents = fs.readFileSync(pathToUpload, 'utf8');
fs.unlinkSync(pathToUpload); fs.unlinkSync(pathToUpload);
} }