Skip to content

Commit

Permalink
feat(notion): add signed file url script
Browse files Browse the repository at this point in the history
for new way how notion handles file signatures
  • Loading branch information
mfts committed Jan 12, 2025
1 parent 4c1dfda commit d4e867f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
75 changes: 75 additions & 0 deletions lib/notion/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { NotionAPI } from "notion-client";
import { getPageContentBlockIds } from "notion-utils";

import notion from "./index";

export const addSignedUrls: NotionAPI["addSignedUrls"] = async ({
recordMap,
contentBlockIds,
kyOptions,
}) => {
recordMap.signed_urls = {};

if (!contentBlockIds) {
contentBlockIds = getPageContentBlockIds(recordMap);
}

const allFileInstances = contentBlockIds.flatMap((blockId) => {
const block = recordMap.block[blockId]?.value;

if (
block &&
(block.type === "pdf" ||
block.type === "audio" ||
(block.type === "image" && block.file_ids?.length) ||
block.type === "video" ||
block.type === "file" ||
block.type === "page")
) {
const source =
block.type === "page"
? block.format?.page_cover
: block.properties?.source?.[0]?.[0];

if (source) {
if (
source.includes("secure.notion-static.com") ||
source.includes("prod-files-secure")
) {
return {
permissionRecord: {
table: "block",
id: block.id,
},
url: source,
};
}
}
}

return [];
});

if (allFileInstances.length > 0) {
try {
const { signedUrls } = await notion.getSignedFileUrls(
allFileInstances,
kyOptions,
);

if (signedUrls.length === allFileInstances.length) {
for (const [i, file] of allFileInstances.entries()) {
const signedUrl = signedUrls[i];
if (!signedUrl) continue;

const blockId = file.permissionRecord.id;
if (!blockId) continue;

recordMap.signed_urls[blockId] = signedUrl;
}
}
} catch (err) {
console.warn("NotionAPI getSignedfileUrls error", err);
}
}
};
5 changes: 4 additions & 1 deletion pages/api/file/notion/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";

import notion from "@/lib/notion";
import { addSignedUrls } from "@/lib/notion/utils";
import { log } from "@/lib/utils";

export default async function handle(
Expand All @@ -23,7 +24,9 @@ export default async function handle(
const { pageId } = req.body as { pageId: string };

try {
const recordMap = await notion.getPage(pageId);
const recordMap = await notion.getPage(pageId, { signFileUrls: false });
// TODO: separately sign the file urls until PR merged and published; ref: https://github.com/NotionX/react-notion-x/issues/580#issuecomment-2542823817
await addSignedUrls({ recordMap });

if (!recordMap) {
res.status(500).json({ message: "Internal Server Error" });
Expand Down
5 changes: 4 additions & 1 deletion pages/api/views-dataroom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { sendOtpVerificationEmail } from "@/lib/emails/send-email-otp-verificati
import { getFile } from "@/lib/files/get-file";
import { newId } from "@/lib/id-helper";
import notion from "@/lib/notion";
import { addSignedUrls } from "@/lib/notion/utils";
import prisma from "@/lib/prisma";
import { ratelimit } from "@/lib/redis";
import { parseSheet } from "@/lib/sheet";
Expand Down Expand Up @@ -650,7 +651,9 @@ export default async function handle(
}

const pageId = notionPageId;
recordMap = await notion.getPage(pageId);
recordMap = await notion.getPage(pageId, { signFileUrls: false });
// TODO: separately sign the file urls until PR merged and published; ref: https://github.com/NotionX/react-notion-x/issues/580#issuecomment-2542823817
await addSignedUrls({ recordMap });
}

if (documentVersion.type === "sheet") {
Expand Down
5 changes: 4 additions & 1 deletion pages/view/[linkId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import DataroomView from "@/components/view/dataroom/dataroom-view";
import DocumentView from "@/components/view/document-view";

import notion from "@/lib/notion";
import { addSignedUrls } from "@/lib/notion/utils";
import {
CustomUser,
LinkWithDataroom,
Expand Down Expand Up @@ -93,7 +94,9 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
}

pageId = notionPageId;
recordMap = await notion.getPage(pageId);
recordMap = await notion.getPage(pageId, { signFileUrls: false });
// TODO: separately sign the file urls until PR merged and published; ref: https://github.com/NotionX/react-notion-x/issues/580#issuecomment-2542823817
await addSignedUrls({ recordMap });
}

const { team, teamId, advancedExcelEnabled, ...linkDocument } =
Expand Down

0 comments on commit d4e867f

Please sign in to comment.