Skip to content

Commit

Permalink
💩 Use class metadata image as nft image fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Feb 10, 2023
1 parent ffcc490 commit e8a0bfe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/routes/likernft/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,15 @@ router.get(
}
const size = Math.min(Math.max(inputSizeNum, 1), 1920);
const iscnPrefix = await getISCNPrefixByClassId(classId);
const { data } = await getNFTISCNData(iscnPrefix);
if (!data) throw new ValidationError('ISCN_NOT_FOUND', 404);
const iscnId = data && data['@id'] as string;
const [{ data: ISCNData }, chainData] = await Promise.all([
getNFTISCNData(iscnPrefix),
getNFTClassDataById(classId),
]);
if (!chainData) throw new ValidationError('CLASS_ID_NOT_FOUND', 404);
if (!ISCNData) throw new ValidationError('ISCN_NOT_FOUND', 404);
const iscnId = ISCNData && ISCNData['@id'] as string;
if (!iscnId) throw new ValidationError('ISCN_ID_NOT_FOUND', 404);
const { image: chainImage } = chainData.data.metadata;
let iscnData = await iscnInfoCollection.doc(encodeURIComponent(iscnId)).get();
if (!iscnData.exists) {
await axios.post(
Expand All @@ -105,16 +110,16 @@ router.get(
await sleep(1000);
iscnData = await iscnInfoCollection.doc(encodeURIComponent(iscnId)).get();
}
let image = '';
let iscnImage = '';
let title = 'Writing NFT';
if (iscnData.exists) {
({ image, title = 'Writing NFT' } = iscnData.data());
({ image: iscnImage, title = 'Writing NFT' } = iscnData.data());
}
const {
image: basicImage,
contentType,
isDefault: isImageMissing,
} = await getBasicImage(image, title);
} = await getBasicImage(iscnImage, chainImage, title);
const resizedImage = getResizedImage(size);
// Disable image mask for now
// const combinedImage = await getCombinedImage();
Expand Down
14 changes: 11 additions & 3 deletions src/util/api/likernft/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@ export function parseImageURLFromMetadata(image: string): string {
return image.replace('ar://', 'https://arweave.net/').replace('ipfs://', 'https://ipfs.io/ipfs/')
}

export async function getBasicImage(image, title) {
export async function getBasicImage(iscnImage, chainImage, title) {
let imageBuffer;
let contentType;
let isDefault = true;
if (image) {
const imageData = (await axios.get(encodedURL(image), { responseType: 'stream' }).catch(() => ({} as any)));
if (iscnImage) {
const imageData = (await axios.get(encodedURL(iscnImage), { responseType: 'stream' }).catch(() => ({} as any)));
if (imageData && imageData.data) {
imageBuffer = imageData.data;
contentType = imageData.headers['content-type'] || 'image/png';
isDefault = false;
}
}
if (chainImage && !imageBuffer) {
const imageData = (await axios.get(encodedURL(chainImage), { responseType: 'stream' }).catch(() => ({} as any)));
if (imageData && imageData.data) {
imageBuffer = imageData.data;
contentType = imageData.headers['content-type'] || 'image/png';
Expand Down

0 comments on commit e8a0bfe

Please sign in to comment.