mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
61 lines
2.1 KiB
JavaScript
61 lines
2.1 KiB
JavaScript
import path from 'node:path';
|
|
import fs from 'node:fs';
|
|
|
|
import express from 'express';
|
|
import sanitize from 'sanitize-filename';
|
|
import jimp from 'jimp';
|
|
import { sync as writeFileAtomicSync } from 'write-file-atomic';
|
|
|
|
import { jsonParser, urlencodedParser } from '../express-common.js';
|
|
import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js';
|
|
import { getImages, tryParse } from '../util.js';
|
|
|
|
export const router = express.Router();
|
|
|
|
router.post('/get', jsonParser, function (request, response) {
|
|
var images = getImages(request.user.directories.avatars);
|
|
response.send(JSON.stringify(images));
|
|
});
|
|
|
|
router.post('/delete', jsonParser, function (request, response) {
|
|
if (!request.body) return response.sendStatus(400);
|
|
|
|
if (request.body.avatar !== sanitize(request.body.avatar)) {
|
|
console.error('Malicious avatar name prevented');
|
|
return response.sendStatus(403);
|
|
}
|
|
|
|
const fileName = path.join(request.user.directories.avatars, sanitize(request.body.avatar));
|
|
|
|
if (fs.existsSync(fileName)) {
|
|
fs.rmSync(fileName);
|
|
return response.send({ result: 'ok' });
|
|
}
|
|
|
|
return response.sendStatus(404);
|
|
});
|
|
|
|
router.post('/upload', urlencodedParser, async (request, response) => {
|
|
if (!request.file) return response.sendStatus(400);
|
|
|
|
try {
|
|
const pathToUpload = path.join(request.file.destination, request.file.filename);
|
|
const crop = tryParse(request.query.crop);
|
|
let rawImg = await jimp.read(pathToUpload);
|
|
|
|
if (typeof crop == 'object' && [crop.x, crop.y, crop.width, crop.height].every(x => typeof x === 'number')) {
|
|
rawImg = rawImg.crop(crop.x, crop.y, crop.width, crop.height);
|
|
}
|
|
|
|
const image = await rawImg.cover(AVATAR_WIDTH, AVATAR_HEIGHT).getBufferAsync(jimp.MIME_PNG);
|
|
|
|
const filename = request.body.overwrite_name || `${Date.now()}.png`;
|
|
const pathToNewFile = path.join(request.user.directories.avatars, filename);
|
|
writeFileAtomicSync(pathToNewFile, image);
|
|
fs.rmSync(pathToUpload);
|
|
return response.send({ path: filename });
|
|
} catch (err) {
|
|
return response.status(400).send('Is not a valid image');
|
|
}
|
|
});
|