Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notion): add signed file url script #1440

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading