From 168baad9864669ebb8f026ef26ee256f0db9c4db Mon Sep 17 00:00:00 2001 From: Kentai Radiquum Date: Wed, 29 Jan 2025 23:25:02 +0500 Subject: [PATCH] add listing and caching of S3 objects --- src/build.tsx | 62 ++++++++++++++++++++++++++++++++++++++++- src/static/tailwind.css | 4 +++ src/utils.ts | 8 +++--- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/build.tsx b/src/build.tsx index 737d79e..9619d5a 100644 --- a/src/build.tsx +++ b/src/build.tsx @@ -10,6 +10,62 @@ if (environment == "dev") { if (!fs.existsSync("out")) fs.mkdirSync("out"); +// List Objects from S3 bucket + +import { S3Client, ListObjectsV2Command } from "@aws-sdk/client-s3"; + +const S3 = new S3Client({ + region: "auto", + endpoint: `${process.env.ENDPOINT}`, + credentials: { + accessKeyId: `${process.env.ACCESS_KEY_ID}`, + secretAccessKey: `${process.env.SECRET_ACCESS_KEY}`, + }, +}); + +let items: string[] = []; + +async function listAllObjects(bucketName: string, prefix: string) { + let isTruncated: boolean = true; + let continuationToken: string = ""; + + while (isTruncated) { + const params = { + Bucket: bucketName, + Prefix: prefix, + ContinuationToken: continuationToken, + }; + + const response = await S3.send(new ListObjectsV2Command(params)); + if (response && response.Contents) { + response.Contents.forEach((item) => { + if (item.Key != undefined) { + const ext = item.Key.split(".")[item.Key.split(".").length - 1]; + if (["png", "jpg", "mp4", "jpeg"].includes(ext.toLowerCase())) { + items.push(item.Key); + } + } + }); + isTruncated = response.IsTruncated as boolean; + continuationToken = response.NextContinuationToken as string; + } + } +} + +if (!fs.existsSync(".cache")) fs.mkdirSync(".cache"); +if (!fs.existsSync(".cache/objects.json")) { + log.info("Listing all objects in S3 bucket . . ."); + await listAllObjects(process.env.BUCKET as string, "red_panda"); + fs.writeFileSync(".cache/objects.json", JSON.stringify({ items })); + log.info(`Total: ${items.length}`); +} else { + log.info("Using S3 Bucket cache . . ."); + items = JSON.parse( + fs.readFileSync(".cache/objects.json", { encoding: "utf-8" }) + )["items"]; + log.info(`Total: ${items.length}`); +} + import { renderToString } from "react-dom/server"; import fs from "fs"; function App(props: {}) { @@ -17,7 +73,11 @@ function App(props: {}) { - {environment == "dev" ? : ""} + {environment == "dev" ? ( + + ) : ( + "" + )} Bun Render Test diff --git a/src/static/tailwind.css b/src/static/tailwind.css index f40ba17..4b5823c 100644 --- a/src/static/tailwind.css +++ b/src/static/tailwind.css @@ -568,6 +568,10 @@ font-size: var(--text-6xl); line-height: var(--tw-leading, var(--text-6xl--line-height)); } + .text-8xl { + font-size: var(--text-8xl); + line-height: var(--tw-leading, var(--text-8xl--line-height)); + } .text-green-400 { color: var(--color-green-400); } diff --git a/src/utils.ts b/src/utils.ts index 71c9113..18c8f81 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -33,7 +33,7 @@ export class Log { return `${time}:${level} - ${message}`; } - debug(message: string, isConnected = false) { + debug(message: string | any, isConnected = false) { if (this._level == 0) { if (isConnected) { message = `↳ ${message}`; @@ -43,7 +43,7 @@ export class Log { } } - info(message: string, isConnected = false) { + info(message: string | any, isConnected = false) { if (this._level <= 1) { if (isConnected) { message = `↳ ${message}`; @@ -53,7 +53,7 @@ export class Log { } } - warn(message: string, isConnected = false) { + warn(message: string | any, isConnected = false) { if (this._level <= 2) { if (isConnected) { message = `↳ ${message}`; @@ -63,7 +63,7 @@ export class Log { } } - error(message: string, isConnected = false) { + error(message: string | any, isConnected = false) { if (this._level <= 3) { if (isConnected) { message = `↳ ${message}`;