From ef861bb3c4ed0b8da3306a4441fc4ba18477ec35 Mon Sep 17 00:00:00 2001 From: Xennis Date: Sun, 2 Jun 2024 22:41:19 +0200 Subject: [PATCH] [fetch] Move download image function into package --- examples/nextjs/src/lib/fetchers.ts | 28 +++------------------------ packages/fetch/src/image.ts | 30 +++++++++++++++++++++++++++++ packages/fetch/src/index.ts | 1 + 3 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 packages/fetch/src/image.ts diff --git a/examples/nextjs/src/lib/fetchers.ts b/examples/nextjs/src/lib/fetchers.ts index 4b2f596..ef59986 100644 --- a/examples/nextjs/src/lib/fetchers.ts +++ b/examples/nextjs/src/lib/fetchers.ts @@ -1,6 +1,6 @@ import { unstable_cache } from "next/cache" import { fetchBlocksChildren, IconResponse } from "@react-notion-cms/render" -import { fetchDatabasePages, propsPlainTexts } from "@react-notion-cms/fetch" +import { downloadImage, fetchDatabasePages, propsPlainTexts } from "@react-notion-cms/fetch" import { Client } from "@notionhq/client" import { statSync, writeFileSync } from "node:fs" import type { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints" @@ -61,28 +61,6 @@ export const getCachedPageContent = unstable_cache( ) const downloadImageToPublicDir = async (url: string, meta: { blockId: string; lastEditedTime: Date }) => { - const fileUrl = new URL(url) - const originalFileName = fileUrl.pathname.substring(fileUrl.pathname.lastIndexOf("/") + 1) - const newFileName = `public/cms/${meta.blockId}-${originalFileName}` - - let savedLastEditedTime: Date | null = null - try { - const stat = statSync(newFileName) - savedLastEditedTime = stat.mtime - } catch (error) { - if (error instanceof Error && "code" in error && error.code === "ENOENT") { - console.debug(`${newFileName} not found`) - } else { - console.warn(`${newFileName}: ${error}`) - } - } - // Avoid download the file again and again - if (savedLastEditedTime === null || meta.lastEditedTime > savedLastEditedTime) { - const resp = await fetch(fileUrl) - const blob = await resp.blob() - writeFileSync(newFileName, new DataView(await blob.arrayBuffer())) - console.info(`downloaded image ${newFileName} of block ${meta.blockId}`) - } - - return newFileName.replace("public", nextConfig.basePath ?? "") + const localImage = await downloadImage("public/cms", url, meta) + return localImage.replace("public", nextConfig.basePath ?? "") } diff --git a/packages/fetch/src/image.ts b/packages/fetch/src/image.ts new file mode 100644 index 0000000..4a058ad --- /dev/null +++ b/packages/fetch/src/image.ts @@ -0,0 +1,30 @@ +import { statSync, writeFileSync } from "node:fs" +import * as path from "node:path" + +export const downloadImage = async (dir: string, url: string, meta: { blockId: string; lastEditedTime: Date }) => { + const fileUrl = new URL(url) + const originalFileName = fileUrl.pathname.substring(fileUrl.pathname.lastIndexOf("/") + 1) + const originalFileExtension = originalFileName.split(".").pop() + const newFileName = path.join(dir, `${meta.blockId}.${originalFileExtension}`) + + let savedLastEditedTime: Date | null = null + try { + const stat = statSync(newFileName) + savedLastEditedTime = stat.mtime + } catch (error) { + if (error instanceof Error && "code" in error && error.code === "ENOENT") { + console.debug(`${newFileName} not found`) + } else { + console.warn(`${newFileName}: ${error}`) + } + } + // Avoid download the file again and again + if (savedLastEditedTime === null || meta.lastEditedTime > savedLastEditedTime) { + const resp = await fetch(fileUrl) + const blob = await resp.blob() + writeFileSync(newFileName, new DataView(await blob.arrayBuffer())) + console.info(`downloaded image ${newFileName} of block ${meta.blockId}`) + } + + return newFileName +} diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index 310038c..de613f1 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -1,2 +1,3 @@ export * from "./fetch" +export * from "./image" export * from "./properties"