From a4561b75ce51bd57d4834c9b661bddca50253e20 Mon Sep 17 00:00:00 2001 From: Marcel <65048232+dromzeh@users.noreply.github.com> Date: Wed, 29 May 2024 15:49:12 +0100 Subject: [PATCH] word --- src/index.ts | 14 -------------- src/lib/d1/checkRow.ts | 31 ------------------------------- src/lib/d1/checkTable.ts | 13 ------------- src/lib/d1/getAssetRequests.ts | 26 -------------------------- src/lib/responseHeaders.ts | 2 +- src/routes/games/getAsset.ts | 14 ++++---------- src/routes/games/getGameId.ts | 25 +++---------------------- 7 files changed, 8 insertions(+), 117 deletions(-) delete mode 100644 src/lib/d1/checkRow.ts delete mode 100644 src/lib/d1/checkTable.ts delete mode 100644 src/lib/d1/getAssetRequests.ts diff --git a/src/index.ts b/src/index.ts index 71273ae6..9af06d24 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,21 +1,7 @@ import { router } from "@/handler"; -// import { instrument, ResolveConfigFn } from "@microlabs/otel-cf-workers"; const handler = { fetch: router.handle, }; -// // eslint-disable-next-line @typescript-eslint/no-unused-vars -// const config: ResolveConfigFn = (env: Env, _trigger) => { -// return { -// exporter: { -// url: "https://otel.baselime.io/v1", -// headers: { "x-api-key": env.BASELIME_API_KEY }, -// }, -// service: { name: env.SERVICE_NAME }, -// }; -// }; - -// export default instrument(handler, config); - export default handler; diff --git a/src/lib/d1/checkRow.ts b/src/lib/d1/checkRow.ts deleted file mode 100644 index 629eebe9..00000000 --- a/src/lib/d1/checkRow.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { rename } from "@/lib/helpers/rename"; - -export const checkRow = async ( - db: D1Database, - gameId: string, - asset: string -): Promise => { - const tableName: string = rename(gameId); - const location: string = rename(asset); - - const row: D1Result<{ requests: number }> = await db - .prepare(`SELECT requests FROM ${tableName} WHERE location = ?`) - .bind(location) - .all(); - - if (Array.isArray(row) && row.length === 0) { - await db - .prepare( - `INSERT INTO ${tableName} (location, requests) VALUES (?, 0)` - ) - .bind(location) - .run(); - } - - await db - .prepare( - `UPDATE ${tableName} SET requests = requests + 1 WHERE location = ?` - ) - .bind(location) - .run(); -}; diff --git a/src/lib/d1/checkTable.ts b/src/lib/d1/checkTable.ts deleted file mode 100644 index 22534ee5..00000000 --- a/src/lib/d1/checkTable.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { rename } from "@/lib/helpers/rename"; - -export const checkTable = async ( - db: D1Database, - gameId: string -): Promise => { - const tableName: string = rename(gameId); - await db - .prepare( - `CREATE TABLE IF NOT EXISTS ${tableName} (location TEXT, requests INTEGER)` - ) - .run(); -}; diff --git a/src/lib/d1/getAssetRequests.ts b/src/lib/d1/getAssetRequests.ts deleted file mode 100644 index bbcfa266..00000000 --- a/src/lib/d1/getAssetRequests.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { rename } from "@/lib/helpers/rename"; - -export const getAssetRequests = async ( - db: D1Database, - gameId: string, - asset: string -): Promise => { - const tableName: string = rename(gameId); - const location: string = rename(asset); - - const requests: { results?: { requests: number }[] } = await db - .prepare(`SELECT requests FROM ${tableName} WHERE location = ?`) - .bind(location) - .all(); - - if (requests.results?.length === 0) { - await db - .prepare( - `INSERT INTO ${tableName} (location, requests) VALUES (?, 0)` - ) - .bind(location) - .run(); - } - - return requests?.results[0]?.requests ?? 0; -}; diff --git a/src/lib/responseHeaders.ts b/src/lib/responseHeaders.ts index b59b2ac4..613a2414 100644 --- a/src/lib/responseHeaders.ts +++ b/src/lib/responseHeaders.ts @@ -3,5 +3,5 @@ export const responseHeaders: Record = { "Referrer-Policy": "strict-origin-when-cross-origin", "content-type": "application/json;charset=UTF-8", "access-control-allow-origin": "*", - "Cache-Control": `max-age=${60 * 60 * 1}`, + "Cache-Control": `max-age=${60 * 60 * 2}`, }; diff --git a/src/routes/games/getAsset.ts b/src/routes/games/getAsset.ts index 1a1a8171..e7c13113 100644 --- a/src/routes/games/getAsset.ts +++ b/src/routes/games/getAsset.ts @@ -1,7 +1,5 @@ import { responseHeaders } from "@/lib/responseHeaders"; import { listBucket } from "@/lib/listBucket"; -import { checkTable } from "@/lib/d1/checkTable"; -import { checkRow } from "@/lib/d1/checkRow"; import type { Image } from "@/lib/types/asset"; export const getAsset = async ( @@ -62,14 +60,10 @@ export const getAsset = async ( size: file.size, })); - const lastUploaded = images.sort((a, b) => b.uploaded - a.uploaded)[0]; - - try { - await checkTable(env.database, gameId); - await checkRow(env.database, gameId, asset); - } catch (e) { - console.error(e); - } + const sortedImages = [...images].sort( + (a, b) => b.uploaded - a.uploaded + ); + const lastUploaded = sortedImages[0]; response = new Response( JSON.stringify({ diff --git a/src/routes/games/getGameId.ts b/src/routes/games/getGameId.ts index c9434a5c..08e96cb2 100644 --- a/src/routes/games/getGameId.ts +++ b/src/routes/games/getGameId.ts @@ -1,7 +1,5 @@ import { responseHeaders } from "@/lib/responseHeaders"; import { listBucket } from "@/lib/listBucket"; -import { checkTable } from "@/lib/d1/checkTable"; -import { getAssetRequests } from "@/lib/d1/getAssetRequests"; import type { Location } from "@/lib/types/game"; export const getGameId = async ( @@ -48,6 +46,7 @@ export const getGameId = async ( }); const fileCount = subfolderFiles.objects.length; + const lastUploaded = subfolderFiles.objects.reduce( (prev, current) => { const prevDate = new Date(prev.uploaded); @@ -59,38 +58,20 @@ export const getGameId = async ( const name = file.replace(`${gameId}/`, "").replace("/", ""); - try { - await checkTable(env.database, gameId); - } catch (e) { - console.error(e); - } - - let popularity = 0; - try { - const requestsCount = await getAssetRequests( - env.database, - gameId, - name - ); - popularity = requestsCount; - } catch (e) { - console.error(e); - } - return { name, path: `https://api.wanderer.moe/game/${gameId}/${file .replace(`${gameId}/`, "") .replace("/", "")}`, fileCount, - popularity, + popularity: 0, lastUploaded: lastUploaded.uploaded, } as Location; }); const locationsWithFileCount = await Promise.all(locations); - locationsWithFileCount.sort((a, b) => b.popularity - a.popularity); + locationsWithFileCount.sort((a, b) => b.lastUploaded - a.lastUploaded); locationsWithFileCount.forEach((location, index) => { location.popularity = index + 1; });