Last active
October 15, 2022 05:06
-
-
Save rootasjey/f88e4df08b0646a41cec10cfb4f63bcc to your computer and use it in GitHub Desktop.
Function to fetch a markdown post in Firebase Storage from Cloud Functions (old way)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Retrieve a project's content in markdown format | |
* (with accessibility check). | |
*/ | |
export const fetch = functions | |
.region(cloudRegions.eu) | |
.https | |
.onCall(async (data) => { | |
const projectId: string = data.projectId; | |
/** Used if the project is a draft or restricted to some members. */ | |
const jwt: string = data.jwt; | |
if (!projectId) { | |
throw new functions.https.HttpsError( | |
"invalid-argument", | |
`The function must be called with one (string) argument | |
"projectId" which is the project to fetch.`, | |
); | |
} | |
// Check this helper function in its dedicated gist. | |
// // [firebase_functions_helper_post_acl.ts](https://gist.github.com/rootasjey/6b8594cd4da10822c5029a6997f1b8c6) | |
await checkAccessControl({projectId, jwt}); | |
// Retrieve content (access control OK) | |
const projectFile = storage | |
.bucket() | |
.file(`blog/projects/${projectId}/post.md`); | |
let projectContent = ""; | |
const stream = projectFile.createReadStream(); | |
stream.on("data", (chunck) => { | |
projectContent = projectContent.concat(chunck.toString()); | |
}); | |
await new Promise((fulfill) => stream.on("end", fulfill)); | |
stream.destroy(); | |
return {project: projectContent}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment