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

PDF file URLs won't be signed #580

Open
sagan opened this issue Nov 8, 2024 · 2 comments
Open

PDF file URLs won't be signed #580

sagan opened this issue Nov 8, 2024 · 2 comments

Comments

@sagan
Copy link

sagan commented Nov 8, 2024

Description

At this time, NotionAPI.addSignedUrls only signs URLs which contains secure.notion-static.com string (#332).

Notion is currently serving PDF (may be also other type files, I don't test) files from "https://prod-files-secure.s3.us-west-2.amazonaws.com/<uuid1>/<uuid2>/<name>.pdf" style urls, which won't be signed by NotionAPI.addSignedUrls.

Notion Test Page ID

1387d259b2558024b095eb5c50425400

@worgho2
Copy link

worgho2 commented Dec 14, 2024

See #596

@worgho2
Copy link

worgho2 commented Dec 14, 2024

Meanwhile you can use this function to solve the problem

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];
        // console.log(block, source)

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

      return [];
    });

    if (allFileInstances.length > 0) {
      try {
        const { signedUrls } = await this.client.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);
      }
    }
  };

const pageId = 'your page id';

const recordMap = await this.client.getPage(pageId, {
        signFileUrls: false 
    }
});

await addSignedUrls({ recordMap })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants