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

Fail to access attached files: Access Denied #493

Open
Qubit-Fernand opened this issue Apr 28, 2023 · 2 comments
Open

Fail to access attached files: Access Denied #493

Qubit-Fernand opened this issue Apr 28, 2023 · 2 comments

Comments

@Qubit-Fernand
Copy link

Description

When I try to attach files such as PDF into the Blog in my Homepage, on the website I have no access as in Notion.
Besides, there are some photo displaying bugs in Safari on Mac in my Homepage.

image

923E8209-338D-49BE-98C4-167D4A14B69F

71607649-E266-4B5B-8337-7A3E87E53365

Notion Test Page ID

Homepage: 5f6bf54917834c538f8b4c0264582174
Subpage containing files: ba95d53958e447eebb959c064a9be787
-->

@fabianbeier
Copy link

Hi I had the same Issue and saw that for the first X hours if you uploaded a file the "signed_urls" object in the the RecordMap was empty (still don't know why). At some point it would be populated, but I needed the files to work immediately.

If anybody else is stumbling across this problem, I added this to the notion.ts file:

/**
 * This function checks if a given block is of type 'file' or 'pdf'.
 * @param {any} block - The block to check.
 * @return {boolean} - Returns true if the block is of type 'file' or 'pdf', false otherwise.
 */
function isBlockOfTypeFile(block: any): boolean {
  return block.value.type === 'file' || block.value.type === 'pdf'
}

/**
 * This function retrieves signed URLs for all 'file' or 'pdf' type blocks in a given record map.
 * @param {ExtendedRecordMap} recordMap - The record map to retrieve signed URLs for.
 * @return {Promise<any>} - Returns a promise that resolves to an object. The keys of the object are block IDs, and the values are the corresponding signed URLs.
 */
async function getSignedUrls(recordMap: ExtendedRecordMap): Promise<any> {
  // Extract all blocks of type 'file' or 'pdf' from the record map
  const fileBlocks = Object.values(recordMap.block).filter(isBlockOfTypeFile)

  // Initialize an empty object to store the signed URLs
  // eslint-disable-next-line prefer-const
  let signedUrlsObject = {}
  // Iterate over each file block
  for (const block of fileBlocks) {
    // Retrieve the signed URL for the current block
    const signedUrls = await notion.getSignedFileUrls([
      {
        url: block.value.properties.source[0][0],
        permissionRecord: {
          table: 'block',
          id: block.value.id
        }
      }
    ])

    // Store the signed URL in the object, using the block ID as the key
    signedUrlsObject[block.value.id] = signedUrls.signedUrls[0]
  }

  // Log the object containing the signed URLs
  console.log(signedUrlsObject)
  // Return the object containing the signed URLs
  return signedUrlsObject
}

It loops over all blocks and identifies 'file' and 'pdf' blocks and then makes a request to get the signed Url and add it to the RecordMap.

Then in the getPage function I add this:

let recordMap = await notion.getPage(pageId)

const signedUrls = await getSignedUrls(recordMap)
;(recordMap as any).signed_urls = signedUrls

@matkoch
Copy link

matkoch commented Nov 13, 2023

@fabianbeier nice work! Just adding a note here that I had to adapt isBlockOfTypeFile to the following:

function isBlockOfTypeFile(block: any): boolean {
  return block.value && (block.value.type === 'file' || block.value.type === 'pdf')
}

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

3 participants