Skip to content

Commit

Permalink
[fetch] Move download image function into package
Browse files Browse the repository at this point in the history
  • Loading branch information
Xennis committed Jun 2, 2024
1 parent a6333a8 commit ef861bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
28 changes: 3 additions & 25 deletions examples/nextjs/src/lib/fetchers.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 ?? "")
}
30 changes: 30 additions & 0 deletions packages/fetch/src/image.ts
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions packages/fetch/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./fetch"
export * from "./image"
export * from "./properties"

0 comments on commit ef861bb

Please sign in to comment.