Liquid icons api
This commit is contained in:
parent
b455814e90
commit
a4569788f8
39
backend/src/api/liquid/icons.ts
Normal file
39
backend/src/api/liquid/icons.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
|
import config from '../../config';
|
||||||
|
import logger from '../../logger';
|
||||||
|
|
||||||
|
class Icons {
|
||||||
|
private static FILE_NAME = './icons.json';
|
||||||
|
private iconIds: string[] = [];
|
||||||
|
private icons: { [assetId: string]: string; } = {};
|
||||||
|
constructor() {
|
||||||
|
if (config.MEMPOOL.NETWORK !== 'liquid') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(Icons.FILE_NAME)) {
|
||||||
|
logger.warn(`${Icons.FILE_NAME} does not exist. No Liquid icons loaded.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const cacheData = fs.readFileSync(Icons.FILE_NAME, 'utf8');
|
||||||
|
this.icons = JSON.parse(cacheData);
|
||||||
|
|
||||||
|
for (const i in this.icons) {
|
||||||
|
this.iconIds.push(i);
|
||||||
|
}
|
||||||
|
logger.debug(`Liquid icons has been loaded.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getIconByAssetId(assetId: string): Buffer | undefined {
|
||||||
|
const icon = this.icons[assetId];
|
||||||
|
if (icon) {
|
||||||
|
return Buffer.from(icon, 'base64');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAllIconIds() {
|
||||||
|
return this.iconIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default new Icons();
|
@ -270,6 +270,13 @@ class Server {
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config.MEMPOOL.NETWORK === 'liquid') {
|
||||||
|
this.app
|
||||||
|
.get(config.MEMPOOL.API_URL_PREFIX + 'liquid/icons', routes.getAllLiquidIcon)
|
||||||
|
.get(config.MEMPOOL.API_URL_PREFIX + 'liquid/icon/:assetId', routes.getLiquidIcon)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
if (config.MEMPOOL.NETWORK === 'liquid' && config.DATABASE.ENABLED) {
|
if (config.MEMPOOL.NETWORK === 'liquid' && config.DATABASE.ENABLED) {
|
||||||
this.app
|
this.app
|
||||||
.get(config.MEMPOOL.API_URL_PREFIX + 'liquid/pegs/month', routes.$getElementsPegsByMonth)
|
.get(config.MEMPOOL.API_URL_PREFIX + 'liquid/pegs/month', routes.$getElementsPegsByMonth)
|
||||||
|
@ -19,6 +19,7 @@ import loadingIndicators from './api/loading-indicators';
|
|||||||
import { Common } from './api/common';
|
import { Common } from './api/common';
|
||||||
import bitcoinClient from './api/bitcoin/bitcoin-client';
|
import bitcoinClient from './api/bitcoin/bitcoin-client';
|
||||||
import elementsParser from './api/liquid/elements-parser';
|
import elementsParser from './api/liquid/elements-parser';
|
||||||
|
import icons from './api/liquid/icons';
|
||||||
|
|
||||||
class Routes {
|
class Routes {
|
||||||
constructor() {}
|
constructor() {}
|
||||||
@ -807,6 +808,26 @@ class Routes {
|
|||||||
: (e.message || 'Error'));
|
: (e.message || 'Error'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getLiquidIcon(req: Request, res: Response) {
|
||||||
|
const result = icons.getIconByAssetId(req.params.assetId);
|
||||||
|
if (result) {
|
||||||
|
res.setHeader('content-type', 'image/png');
|
||||||
|
res.setHeader('content-length', result.length);
|
||||||
|
res.send(result);
|
||||||
|
} else {
|
||||||
|
res.status(404).send('Asset icon not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAllLiquidIcon(req: Request, res: Response) {
|
||||||
|
const result = icons.getAllIconIds();
|
||||||
|
if (result) {
|
||||||
|
res.json(result);
|
||||||
|
} else {
|
||||||
|
res.status(404).send('Asset icons not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new Routes();
|
export default new Routes();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user