Skip to content

Commit

Permalink
split save icon and cover
Browse files Browse the repository at this point in the history
  • Loading branch information
linyows committed Jan 23, 2024
1 parent ee2e442 commit 0a6c55c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 29 deletions.
25 changes: 20 additions & 5 deletions src/exporter/breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { reqAPIWithBackoff, notion } from './api.js'
import type { GetBlockResponse, GetPageResponseEx, GetDatabaseResponseEx, MentionIcon, Parent } from './types.js'
import { notion, reqAPIWithBackoffAndCache } from './api.js'
import { savePageIcon } from './page.js'
import { saveDatabaseIcon } from './database.js'
import type {
GetBlockResponse,
GetPageResponseEx,
GetDatabaseResponseEx,
MentionIcon,
Parent,
} from './types.js'

export interface FetchBreadcrumbsProps {
type: 'page_id' | 'database_id' | 'block_id' | 'workspace'
Expand Down Expand Up @@ -39,7 +47,8 @@ export const FetchBreadcrumbs = async ({ type, id, limit }: FetchBreadcrumbsProp
while (count < max && isNext) {
switch (nextType) {
case 'block_id': {
const res = await reqAPIWithBackoff<GetBlockResponse>({
const res = await reqAPIWithBackoffAndCache<GetBlockResponse>({
name: 'notion.blocks.retrieve',
func: notion.blocks.retrieve,
args: { block_id: nextID },
count: 3,
Expand All @@ -53,11 +62,14 @@ export const FetchBreadcrumbs = async ({ type, id, limit }: FetchBreadcrumbsProp
break
}
case 'page_id': {
const page = await reqAPIWithBackoff<GetPageResponseEx>({
const page = await reqAPIWithBackoffAndCache<GetPageResponseEx>({
name: 'notion.page.retrieve',
func: notion.pages.retrieve,
args: { page_id: nextID },
count: 3,
})
await savePageIcon(page)

nextType = page.parent.type
nextID = getID(page.parent)
const name = page.properties.title.type === 'title' ? page.properties.title.title.map(v => v.plain_text).join('') : ''
Expand All @@ -84,11 +96,14 @@ export const FetchBreadcrumbs = async ({ type, id, limit }: FetchBreadcrumbsProp
break
}
case 'database_id': {
const db = await reqAPIWithBackoff<GetDatabaseResponseEx>({
const db = await reqAPIWithBackoffAndCache<GetDatabaseResponseEx>({
name: 'notion.database.retrieve',
func: notion.databases.retrieve,
args: { database_id: nextID },
count: 3,
})
await saveDatabaseIcon(db)

nextType = db.parent.type
nextID = getID(db.parent)
const name = db.title.map(v => v.plain_text).join('')
Expand Down
39 changes: 30 additions & 9 deletions src/exporter/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import type {
PersonUserObjectResponseEx,
GetPagePropertyResponse,
} from './types.js'
import {
savePageCover,
savePageIcon,
} from './page.js'

export interface FetchDatabaseArgs extends QueryDatabaseParameters {
}
Expand Down Expand Up @@ -80,11 +84,8 @@ export const FetchDatabase = async (params: FetchDatabaseArgs): Promise<FetchDat

for (const result of allres.results) {
const page: PageObjectResponseEx = result
// Save page cover files
if ('cover' in page && page.cover != null) {
const imageUrl = (page.cover.type === 'external') ? page.cover.external.url : page.cover.file.url
page.cover.src = await saveImage(imageUrl, `page-cover-${page.id}`)
}
await savePageCover(page)
await savePageIcon(page)
// Set page property items
page.property_items = []
for (const [, v] of Object.entries(page.properties)) {
Expand Down Expand Up @@ -114,13 +115,33 @@ export const FetchDatabase = async (params: FetchDatabaseArgs): Promise<FetchDat
args: { database_id },
count: 3,
})
if ('cover' in meta && meta.cover !== null) {
const imageUrl = (meta.cover.type === 'external') ? meta.cover.external.url : meta.cover.file.url
meta.cover.src = await saveImage(imageUrl, `database-cover-${database_id}`)
}
await saveDatabaseCover(meta)
await saveDatabaseIcon(meta)
allres.meta = meta

await writeCache(cacheFile, allres)

return allres
}

export async function saveDatabaseCover(db: GetDatabaseResponseEx) {
if (db.cover === undefined || db.cover === null) {
return
}
if (db.cover.type === 'external') {
db.cover.src = await saveImage(db.cover.external.url, `database-cover-${db.id}`)
} else if (db.cover.type === 'file') {
db.cover.src = await saveImage(db.cover.file.url, `database-cover-${db.id}`)
}
}

export async function saveDatabaseIcon(db: GetDatabaseResponseEx) {
if (db.icon === undefined || db.icon === null) {
return
}
if (db.icon.type === 'external') {
db.icon.src = await saveImage(db.icon.external.url, `database-icon-${db.id}`)
} else if (db.icon.type === 'file') {
db.icon.src = await saveImage(db.icon.file.url, `database-icon-${db.id}`)
}
}
1 change: 1 addition & 0 deletions src/exporter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './blocks.js'
export * from './database.js'
export * from './page.js'
export * from './types.js'
export * from './breadcrumbs.js'
40 changes: 25 additions & 15 deletions src/exporter/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
GetPageResponseEx,
PropertyItemPropertyItemListResponse,
GetPagePropertyResponse,
PageObjectResponseEx,
} from './types.js'

export interface FetchPageArgs {
Expand Down Expand Up @@ -81,22 +82,31 @@ export const FetchPage = async ({ page_id, last_edited_time }: FetchPageArgs): P
page.meta = list
}

if ('cover' in page && page.cover !== null) {
if (page.cover.type === 'external') {
page.cover.src = await saveImage(page.cover.external.url, `page-cover-${page.id}`)
} else if (page.cover.type === 'file') {
page.cover.src = await saveImage(page.cover.file.url, `page-cover-${page.id}`)
}
}
if ('icon' in page && page.icon !== null) {
if (page.icon.type === 'external') {
page.icon.src = await saveImage(page.icon.external.url, `page-icon-${page.id}`)
} else if (page.icon.type === 'file') {
page.icon.src = await saveImage(page.icon.file.url, `page-icon-${page.id}`)
}
}

await savePageCover(page)
await savePageIcon(page)
await writeCache(cacheFile, page)

return page
}

export async function savePageCover(page: GetPageResponseEx | PageObjectResponseEx) {
if (page.cover === undefined || page.cover === null) {
return
}
if (page.cover.type === 'external') {
page.cover.src = await saveImage(page.cover.external.url, `page-cover-${page.id}`)
} else if (page.cover.type === 'file') {
page.cover.src = await saveImage(page.cover.file.url, `page-cover-${page.id}`)
}
}

export async function savePageIcon(page: GetPageResponseEx | PageObjectResponseEx) {
if (page.icon === undefined || page.icon === null) {
return
}
if (page.icon.type === 'external') {
page.icon.src = await saveImage(page.icon.external.url, `page-icon-${page.id}`)
} else if (page.icon.type === 'file') {
page.icon.src = await saveImage(page.icon.file.url, `page-icon-${page.id}`)
}
}
5 changes: 5 additions & 0 deletions src/exporter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ export type PageObjectResponseEx = PageObjectResponse & {
| { src: string, type: 'external'; external: { url: TextRequest } }
| { src: string, type: 'file'; file: { url: string; expiry_time: string } }
| null
icon:
| { type: 'emoji', emoji: EmojiRequest }
| { src: string, type: 'external'; external: { url: TextRequest } }
| { src: string, type: 'file'; file: { url: string; expiry_time: string } }
| null
}

// https://github.com/makenotion/notion-sdk-js/blob/7c5b7645759bf90d71d496dc542a1a912379ee12/src/api-endpoints.ts#L4603-L4632
Expand Down

0 comments on commit 0a6c55c

Please sign in to comment.