71 lines
2.4 KiB
TypeScript
71 lines
2.4 KiB
TypeScript
import * as fs from 'fs/promises';
|
|
import * as path from 'path';
|
|
import * as crypto from 'crypto';
|
|
|
|
import * as FileType from 'file-type';
|
|
|
|
import { ExitCodes, ExitCode } from '../../exit-codes/exit-codes';
|
|
import Logger from '../../logger/logger';
|
|
|
|
import DB from '../db';
|
|
|
|
const LOG = Logger.create(__filename);
|
|
|
|
let serverName = process.argv[2] || 'no chicoms';
|
|
let expiresAfter = process.argv[3] || '7 days';
|
|
let outputPath = process.argv[4] || path.join(__dirname, '../invites/no-chicoms.cordis');
|
|
|
|
process.on('unhandledRejection', async (reason, promise) => {
|
|
LOG.error('unhandled promise rejection:', reason);
|
|
ExitCodes.exit(ExitCode.GENERAL_ERROR);
|
|
});
|
|
|
|
// From ElementsUtil
|
|
async function getImageBufferSrc(buffer) {
|
|
let result = await FileType.fromBuffer(buffer);
|
|
switch (result && result.mime) {
|
|
case 'image/png':
|
|
case 'image/jpeg':
|
|
case 'image/gif':
|
|
return 'data:' + result?.mime + ';base64,' + buffer.toString('base64');
|
|
default:
|
|
// TODO: some images are not getting setup here... (webp, etc)
|
|
LOG.warn('bad result for buffer:', { buffer, result });
|
|
throw new Error('invalid image buffer');
|
|
}
|
|
}
|
|
|
|
(async () => {
|
|
await DB.connect();
|
|
|
|
let certFile = path.join(__dirname, '../ssl/cert.pem');
|
|
let cert = (await fs.readFile(certFile)).toString();
|
|
|
|
let servers = await DB.getAllServers();
|
|
let found = false;
|
|
for (let server of servers) {
|
|
if (server.name == serverName) {
|
|
found = true;
|
|
let iconBuff = (await DB.getResource(server.id, server.icon_resource_id)).data;
|
|
let iconSrc = await getImageBufferSrc(iconBuff);
|
|
let tokenInfo = await DB.createToken(server.id, expiresAfter);
|
|
let fileData = {
|
|
name: server.name,
|
|
url: 'https://localhost:3030',
|
|
cert: cert,
|
|
token: tokenInfo.token,
|
|
expires: new Date(tokenInfo.expires).getTime(),
|
|
iconSrc: iconSrc
|
|
};
|
|
LOG.info('writing file data:', fileData);
|
|
await fs.writeFile(outputPath, JSON.stringify(fileData));
|
|
LOG.info('wrote to ' + outputPath);
|
|
break;
|
|
}
|
|
}
|
|
if (!found) {
|
|
LOG.warn('unable to find server named "' + serverName + '"');
|
|
}
|
|
await DB.end();
|
|
})();
|