Skip to content

Commit

Permalink
add browser preview template for both image & avatar endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanrikulu committed Jul 28, 2024
1 parent 84178dd commit 4355bad
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"@adraffy/ens-normalize": "^1.10.0",
"@ensdomains/ens-avatar": "^1.0.0-alpha.2.ethers.6",
"@ensdomains/ens-avatar": "^1.0.0-alpha.3.ethers.6",
"@ensdomains/ensjs": "^3.7.0",
"@types/lodash": "^4.14.170",
"btoa": "^1.2.1",
Expand Down
10 changes: 10 additions & 0 deletions src/controller/avatarImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { RESPONSE_TIMEOUT } from '../config';
import { getAvatarImage } from '../service/avatar';
import getNetwork, { NetworkName } from '../service/network';
import createDocumentfromTemplate from '../template-document';

export async function avatarImage(req: Request, res: Response) {
// #swagger.description = 'ENS avatar image'
Expand All @@ -29,6 +30,15 @@ export async function avatarImage(req: Request, res: Response) {
description: 'Image file'
} */
if (!res.headersSent) {
if (req.header('sec-fetch-dest') === 'document') {
const documentTemplate = createDocumentfromTemplate({ buffer, metadata: { name, network: networkName }, mimeType });
res
.writeHead(200, {
'Content-Type': 'text/html',
})
.end(documentTemplate);
return;
}
res
.writeHead(200, {
'Content-Type': mimeType,
Expand Down
10 changes: 10 additions & 0 deletions src/controller/ensImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { RESPONSE_TIMEOUT } from '../config';
import { checkContract } from '../service/contract';
import { getDomain } from '../service/domain';
import getNetwork, { NetworkName } from '../service/network';
import createDocumentfromTemplate from '../template-document';

/* istanbul ignore next */
export async function ensImage(req: Request, res: Response) {
Expand Down Expand Up @@ -39,6 +40,15 @@ export async function ensImage(req: Request, res: Response) {
version
);
if (result.image_url) {
if (req.header('sec-fetch-dest') === 'document') {
const documentTemplate = createDocumentfromTemplate({ metadata: {...result, network: networkName }});
res
.writeHead(200, {
'Content-Type': 'text/html',
})
.end(documentTemplate);
return;
}
const base64 = result.image_url.replace('data:image/svg+xml;base64,', '');
const buffer = Buffer.from(base64, 'base64');
if (!res.headersSent) {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const setCacheHeader = function (
`public, max-age=${period}, s-maxage=${period}`
);
}
res.append('Vary', 'Sec-Fetch-Dest');

next();
};
Expand Down
6 changes: 3 additions & 3 deletions src/service/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class AvatarMetadata {
this.uri = uri;
}

async getImage() {
async getImage(): Promise<[Buffer, string]> {
let avatarURI;
try {
avatarURI = await this.avtResolver.getAvatar(this.uri, {
Expand Down Expand Up @@ -102,7 +102,7 @@ export class AvatarMetadata {

assert(!!response, 'Response is empty');

const mimeType = response?.headers.get('Content-Type');
const mimeType = response?.headers.get('Content-Type') || '';
const data = await response?.buffer();

if (mimeType?.includes('svg') || isSvg(data.toString())) {
Expand Down Expand Up @@ -189,7 +189,7 @@ export async function getAvatarMeta(
export async function getAvatarImage(
provider: JsonRpcProvider,
name: string
): Promise<any> {
): Promise<[Buffer, string]> {
const avatar = new AvatarMetadata(provider, name);
return await avatar.getImage();
}
Loading

0 comments on commit 4355bad

Please sign in to comment.