Skip to content

Commit

Permalink
feat: convert data: image uris into image files and upload to cdn (#245)
Browse files Browse the repository at this point in the history
* feat: convert data: image uris into image files and upload to cdn

* test: remove obsolete
  • Loading branch information
rafaelcr authored Aug 26, 2024
1 parent f0824bb commit 903b0aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
36 changes: 22 additions & 14 deletions src/token-processor/images/image-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ import {
import { pipeline } from 'node:stream/promises';
import { Storage } from '@google-cloud/storage';

/** Saves an image provided via a `data:` uri string to disk for processing. */
function convertDataImage(uri: string, tmpPath: string): string {
const dataUrl = parseDataUrl(uri);
if (!dataUrl) {
throw new ImageParseError(`Data URL could not be parsed: ${uri}`);
}
if (!dataUrl.mediaType?.startsWith('image/')) {
throw new ImageParseError(`Token image is a Data URL with a non-image media type: ${uri}`);
}
const filePath = `${tmpPath}/image`;
const imageBuffer = Buffer.from(dataUrl.data, 'base64');
fs.writeFileSync(filePath, imageBuffer);
return filePath;
}

async function downloadImage(imgUrl: string, tmpPath: string): Promise<string> {
return new Promise((resolve, reject) => {
const filePath = `${tmpPath}/image`;
Expand Down Expand Up @@ -98,15 +113,18 @@ export async function processImageCache(
tokenNumber: bigint
): Promise<string[]> {
logger.info(`ImageCache processing token ${contractPrincipal} (${tokenNumber}) at ${imgUrl}`);
if (imgUrl.startsWith('data:')) return [imgUrl];

try {
const gcs = new Storage();
const gcsBucket = ENV.IMAGE_CACHE_GCS_BUCKET_NAME as string;

const tmpPath = `tmp/${contractPrincipal}_${tokenNumber}`;
fs.mkdirSync(tmpPath, { recursive: true });
const original = await downloadImage(imgUrl, tmpPath);
let original: string;
if (imgUrl.startsWith('data:')) {
original = convertDataImage(imgUrl, tmpPath);
} else {
original = await downloadImage(imgUrl, tmpPath);
}

const image1 = await transformImage(original);
const remoteName1 = `${contractPrincipal}/${tokenNumber}.png`;
Expand Down Expand Up @@ -149,17 +167,7 @@ export async function processImageCache(
* @returns Normalized URL string
*/
export function normalizeImageUri(uri: string): string {
// Support images embedded in a Data URL
if (uri.startsWith('data:')) {
const dataUrl = parseDataUrl(uri);
if (!dataUrl) {
throw new ImageParseError(`Data URL could not be parsed: ${uri}`);
}
if (!dataUrl.mediaType?.startsWith('image/')) {
throw new ImageParseError(`Token image is a Data URL with a non-image media type: ${uri}`);
}
return uri;
}
if (uri.startsWith('data:')) return uri;
const fetchableUrl = getFetchableDecentralizedStorageUrl(uri);
return fetchableUrl.toString();
}
Expand Down
7 changes: 0 additions & 7 deletions tests/token-queue/image-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,4 @@ describe('Image cache', () => {
).rejects.toThrow(MetadataHttpError);
await closeTestServer(server);
}, 10000);

test('ignores data: URL', async () => {
const url = 'data:123456';
await expect(processImageCache(url, contract, tokenNumber)).resolves.toStrictEqual([
'data:123456',
]);
});
});

0 comments on commit 903b0aa

Please sign in to comment.