Skip to content

Commit

Permalink
Fix content api
Browse files Browse the repository at this point in the history
  • Loading branch information
opcatdev committed Nov 7, 2024
1 parent 8c4256b commit ba665d0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/tracker/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function okResponse(data: any) {
return {
code: 0,
msg: 'OK',
data: data,
data,
};
}

Expand Down
34 changes: 25 additions & 9 deletions packages/tracker/src/routes/collection/collection.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Param, Query, Res } from '@nestjs/common';
import { Controller, Get, Header, Param, Query, Res } from '@nestjs/common';
import { CollectionService } from './collection.service';
import { okResponse, errorResponse } from '../../common/utils';
import { ApiOperation, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
Expand Down Expand Up @@ -53,6 +53,7 @@ export class CollectionController {
}

@Get(':collectionIdOrAddr/content')
@Header('Cache-Control', 'public, max-age=31536000')
@ApiTags('collection')
@ApiOperation({ summary: 'Get collection content' })
@ApiParam({
Expand All @@ -68,15 +69,22 @@ export class CollectionController {
try {
const content =
await this.collectionService.getCollectionContent(collectionIdOrAddr);
if (content.type) {
if (content?.type) {
res.setHeader('Content-Type', content.type);
}
if (content.encoding) {
if (content?.encoding) {
res.setHeader('Content-Encoding', content.encoding);
}
res.send(content.raw);
if (content?.lastModified) {
res.setHeader('Last-Modified', content.lastModified.toUTCString());
}
if (content?.raw) {
res.send(content.raw);
} else {
res.sendStatus(404);
}
} catch (e) {
return errorResponse(e);
return res.send(errorResponse(e));
}
}

Expand Down Expand Up @@ -111,6 +119,7 @@ export class CollectionController {
}

@Get(':collectionIdOrAddr/localId/:localId/content')
@Header('Cache-Control', 'public, max-age=31536000')
@ApiTags('collection')
@ApiOperation({ summary: 'Get nft content' })
@ApiParam({
Expand All @@ -135,15 +144,22 @@ export class CollectionController {
collectionIdOrAddr,
localId,
);
if (content.type) {
if (content?.type) {
res.setHeader('Content-Type', content.type);
}
if (content.encoding) {
if (content?.encoding) {
res.setHeader('Content-Encoding', content.encoding);
}
res.send(content.raw);
if (content?.lastModified) {
res.setHeader('Last-Modified', content.lastModified.toUTCString());
}
if (content?.raw) {
res.send(content.raw);
} else {
res.sendStatus(404);
}
} catch (e) {
return errorResponse(e);
return res.send(errorResponse(e));
}
}

Expand Down
18 changes: 13 additions & 5 deletions packages/tracker/src/routes/collection/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import { Constants } from '../../common/constants';
import { Content, TokenTypeScope } from '../../common/types';
import { TokenInfoEntity } from '../../entities/tokenInfo.entity';

type CachedContent = Content & { lastModified?: Date };

@Injectable()
export class CollectionService {
private static readonly nftInfoCache = new LRUCache<string, NftInfoEntity>({
max: Constants.CACHE_MAX_SIZE,
});

private static readonly nftContentCache = new LRUCache<string, Content>({
max: Constants.CACHE_MAX_SIZE,
});
private static readonly nftContentCache = new LRUCache<string, CachedContent>(
{
max: Constants.CACHE_MAX_SIZE,
},
);

constructor(
private readonly commonService: CommonService,
Expand All @@ -33,7 +37,7 @@ export class CollectionService {

async getCollectionContent(
collectionIdOrAddr: string,
): Promise<Content | null> {
): Promise<CachedContent | null> {
const key = `${collectionIdOrAddr}`;
let cached = CollectionService.nftContentCache.get(key);
if (!cached) {
Expand All @@ -49,6 +53,7 @@ export class CollectionService {
'contentType',
'contentEncoding',
'contentRaw',
'createdAt',
],
where: { tokenId: collectionInfo.tokenId },
});
Expand All @@ -57,6 +62,7 @@ export class CollectionService {
type: collectionContent.contentType,
encoding: collectionContent.contentEncoding,
raw: collectionContent.contentRaw,
lastModified: collectionContent.createdAt,
};
const lastProcessedHeight =
await this.commonService.getLastProcessedBlockHeight();
Expand Down Expand Up @@ -114,7 +120,7 @@ export class CollectionService {
async getNftContent(
collectionIdOrAddr: string,
localId: bigint,
): Promise<Content | null> {
): Promise<CachedContent | null> {
const key = `${collectionIdOrAddr}_${localId}`;
let cached = CollectionService.nftContentCache.get(key);
if (!cached) {
Expand All @@ -130,6 +136,7 @@ export class CollectionService {
'contentType',
'contentEncoding',
'contentRaw',
'createdAt',
],
where: { collectionId: collectionInfo.tokenId, localId },
});
Expand All @@ -138,6 +145,7 @@ export class CollectionService {
type: nftContent.contentType,
encoding: nftContent.contentEncoding,
raw: nftContent.contentRaw,
lastModified: nftContent.createdAt,
};
const lastProcessedHeight =
await this.commonService.getLastProcessedBlockHeight();
Expand Down

0 comments on commit ba665d0

Please sign in to comment.