|
import path from 'node:path'; |
|
import fs from 'node:fs'; |
|
import { finished } from 'node:stream/promises'; |
|
|
|
import mime from 'mime-types'; |
|
import express from 'express'; |
|
import sanitize from 'sanitize-filename'; |
|
import fetch from 'node-fetch'; |
|
|
|
import { UNSAFE_EXTENSIONS } from '../constants.js'; |
|
import { clientRelativePath } from '../util.js'; |
|
|
|
const VALID_CATEGORIES = ['bgm', 'ambient', 'blip', 'live2d', 'vrm', 'character', 'temp']; |
|
|
|
|
|
|
|
|
|
|
|
|
|
export function validateAssetFileName(inputFilename) { |
|
if (!/^[a-zA-Z0-9_\-.]+$/.test(inputFilename)) { |
|
return { |
|
error: true, |
|
message: 'Illegal character in filename; only alphanumeric, \'_\', \'-\' are accepted.', |
|
}; |
|
} |
|
|
|
const inputExtension = path.extname(inputFilename).toLowerCase(); |
|
if (UNSAFE_EXTENSIONS.some(ext => ext === inputExtension)) { |
|
return { |
|
error: true, |
|
message: 'Forbidden file extension.', |
|
}; |
|
} |
|
|
|
if (inputFilename.startsWith('.')) { |
|
return { |
|
error: true, |
|
message: 'Filename cannot start with \'.\'', |
|
}; |
|
} |
|
|
|
if (sanitize(inputFilename) !== inputFilename) { |
|
return { |
|
error: true, |
|
message: 'Reserved or long filename.', |
|
}; |
|
} |
|
|
|
return { error: false }; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getFiles(dir, files = []) { |
|
if (!fs.existsSync(dir)) return files; |
|
|
|
|
|
const fileList = fs.readdirSync(dir, { withFileTypes: true }); |
|
|
|
for (const file of fileList) { |
|
const name = path.join(dir, file.name); |
|
|
|
if (file.isDirectory()) { |
|
|
|
getFiles(name, files); |
|
} else { |
|
|
|
files.push(name); |
|
} |
|
} |
|
return files; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function ensureFoldersExist(directories) { |
|
const folderPath = path.join(directories.assets); |
|
|
|
for (const category of VALID_CATEGORIES) { |
|
const assetCategoryPath = path.join(folderPath, category); |
|
if (fs.existsSync(assetCategoryPath) && !fs.statSync(assetCategoryPath).isDirectory()) { |
|
fs.unlinkSync(assetCategoryPath); |
|
} |
|
if (!fs.existsSync(assetCategoryPath)) { |
|
fs.mkdirSync(assetCategoryPath, { recursive: true }); |
|
} |
|
} |
|
} |
|
|
|
export const router = express.Router(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/get', async (request, response) => { |
|
const folderPath = path.join(request.user.directories.assets); |
|
let output = {}; |
|
|
|
try { |
|
if (fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory()) { |
|
|
|
ensureFoldersExist(request.user.directories); |
|
|
|
const folders = fs.readdirSync(folderPath, { withFileTypes: true }) |
|
.filter(file => file.isDirectory()); |
|
|
|
for (const { name: folder } of folders) { |
|
if (folder == 'temp') |
|
continue; |
|
|
|
|
|
if (folder == 'live2d') { |
|
output[folder] = []; |
|
const live2d_folder = path.normalize(path.join(folderPath, folder)); |
|
const files = getFiles(live2d_folder); |
|
|
|
for (let file of files) { |
|
if (file.includes('model') && file.endsWith('.json')) { |
|
|
|
output[folder].push(clientRelativePath(request.user.directories.root, file)); |
|
} |
|
} |
|
continue; |
|
} |
|
|
|
|
|
if (folder == 'vrm') { |
|
output[folder] = { 'model': [], 'animation': [] }; |
|
|
|
const vrm_model_folder = path.normalize(path.join(folderPath, 'vrm', 'model')); |
|
let files = getFiles(vrm_model_folder); |
|
|
|
for (let file of files) { |
|
if (!file.endsWith('.placeholder')) { |
|
|
|
output['vrm']['model'].push(clientRelativePath(request.user.directories.root, file)); |
|
} |
|
} |
|
|
|
|
|
const vrm_animation_folder = path.normalize(path.join(folderPath, 'vrm', 'animation')); |
|
files = getFiles(vrm_animation_folder); |
|
|
|
for (let file of files) { |
|
if (!file.endsWith('.placeholder')) { |
|
|
|
output['vrm']['animation'].push(clientRelativePath(request.user.directories.root, file)); |
|
} |
|
} |
|
continue; |
|
} |
|
|
|
|
|
const files = fs.readdirSync(path.join(folderPath, folder)) |
|
.filter(filename => { |
|
return filename != '.placeholder'; |
|
}); |
|
output[folder] = []; |
|
for (const file of files) { |
|
output[folder].push(`assets/${folder}/${file}`); |
|
} |
|
} |
|
} |
|
} |
|
catch (err) { |
|
console.error(err); |
|
} |
|
return response.send(output); |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/download', async (request, response) => { |
|
const url = request.body.url; |
|
const inputCategory = request.body.category; |
|
|
|
|
|
let category = null; |
|
for (let i of VALID_CATEGORIES) |
|
if (i == inputCategory) |
|
category = i; |
|
|
|
if (category === null) { |
|
console.error('Bad request: unsupported asset category.'); |
|
return response.sendStatus(400); |
|
} |
|
|
|
|
|
ensureFoldersExist(request.user.directories); |
|
const validation = validateAssetFileName(request.body.filename); |
|
if (validation.error) |
|
return response.status(400).send(validation.message); |
|
|
|
const temp_path = path.join(request.user.directories.assets, 'temp', request.body.filename); |
|
const file_path = path.join(request.user.directories.assets, category, request.body.filename); |
|
console.info('Request received to download', url, 'to', file_path); |
|
|
|
try { |
|
|
|
const res = await fetch(url); |
|
if (!res.ok || res.body === null) { |
|
throw new Error(`Unexpected response ${res.statusText}`); |
|
} |
|
const destination = path.resolve(temp_path); |
|
|
|
if (fs.existsSync(temp_path)) { |
|
fs.unlink(temp_path, (err) => { |
|
if (err) throw err; |
|
}); |
|
} |
|
const fileStream = fs.createWriteStream(destination, { flags: 'wx' }); |
|
|
|
await finished(res.body.pipe(fileStream)); |
|
|
|
if (category === 'character') { |
|
const fileContent = fs.readFileSync(temp_path); |
|
const contentType = mime.lookup(temp_path) || 'application/octet-stream'; |
|
response.setHeader('Content-Type', contentType); |
|
response.send(fileContent); |
|
fs.unlinkSync(temp_path); |
|
return; |
|
} |
|
|
|
|
|
console.info('Download finished, moving file from', temp_path, 'to', file_path); |
|
fs.copyFileSync(temp_path, file_path); |
|
fs.unlinkSync(temp_path); |
|
response.sendStatus(200); |
|
} |
|
catch (error) { |
|
console.error(error); |
|
response.sendStatus(500); |
|
} |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/delete', async (request, response) => { |
|
const inputCategory = request.body.category; |
|
|
|
|
|
let category = null; |
|
for (let i of VALID_CATEGORIES) |
|
if (i == inputCategory) |
|
category = i; |
|
|
|
if (category === null) { |
|
console.error('Bad request: unsupported asset category.'); |
|
return response.sendStatus(400); |
|
} |
|
|
|
|
|
const validation = validateAssetFileName(request.body.filename); |
|
if (validation.error) |
|
return response.status(400).send(validation.message); |
|
|
|
const file_path = path.join(request.user.directories.assets, category, request.body.filename); |
|
console.info('Request received to delete', category, file_path); |
|
|
|
try { |
|
|
|
if (fs.existsSync(file_path)) { |
|
fs.unlink(file_path, (err) => { |
|
if (err) throw err; |
|
}); |
|
console.info('Asset deleted.'); |
|
} |
|
else { |
|
console.error('Asset not found.'); |
|
response.sendStatus(400); |
|
} |
|
|
|
response.sendStatus(200); |
|
} |
|
catch (error) { |
|
console.error(error); |
|
response.sendStatus(500); |
|
} |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/character', async (request, response) => { |
|
if (request.query.name === undefined) return response.sendStatus(400); |
|
|
|
|
|
const name = sanitize(request.query.name.toString()); |
|
const inputCategory = request.query.category; |
|
|
|
|
|
let category = null; |
|
for (let i of VALID_CATEGORIES) |
|
if (i == inputCategory) |
|
category = i; |
|
|
|
if (category === null) { |
|
console.error('Bad request: unsupported asset category.'); |
|
return response.sendStatus(400); |
|
} |
|
|
|
const folderPath = path.join(request.user.directories.characters, name, category); |
|
|
|
let output = []; |
|
try { |
|
if (fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory()) { |
|
|
|
|
|
if (category == 'live2d') { |
|
const folders = fs.readdirSync(folderPath, { withFileTypes: true }); |
|
for (const folderInfo of folders) { |
|
if (!folderInfo.isDirectory()) continue; |
|
|
|
const modelFolder = folderInfo.name; |
|
const live2dModelPath = path.join(folderPath, modelFolder); |
|
for (let file of fs.readdirSync(live2dModelPath)) { |
|
|
|
if (file.includes('model') && file.endsWith('.json')) |
|
output.push(path.join('characters', name, category, modelFolder, file)); |
|
} |
|
} |
|
return response.send(output); |
|
} |
|
|
|
|
|
const files = fs.readdirSync(folderPath) |
|
.filter(filename => { |
|
return filename != '.placeholder'; |
|
}); |
|
|
|
for (let i of files) |
|
output.push(`/characters/${name}/${category}/${i}`); |
|
} |
|
return response.send(output); |
|
} |
|
catch (err) { |
|
console.error(err); |
|
return response.sendStatus(500); |
|
} |
|
}); |
|
|