Skip to content

Instantly share code, notes, and snippets.

@rootasjey
Last active October 15, 2022 05:06
Show Gist options
  • Select an option

  • Save rootasjey/f88e4df08b0646a41cec10cfb4f63bcc to your computer and use it in GitHub Desktop.

Select an option

Save rootasjey/f88e4df08b0646a41cec10cfb4f63bcc to your computer and use it in GitHub Desktop.

Revisions

  1. rootasjey revised this gist Oct 15, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions firebase_functions_fetch_post.ts
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,7 @@ export const fetch = functions
    }

    // 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)
  2. rootasjey revised this gist Oct 15, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions firebase_functions_fetch_post.ts
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,7 @@ export const fetch = functions
    );
    }

    // Check this helper function in its dedicated gist.
    await checkAccessControl({projectId, jwt});

    // Retrieve content (access control OK)
  3. rootasjey created this gist Oct 15, 2022.
    41 changes: 41 additions & 0 deletions firebase_functions_fetch_post.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    /**
    * 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.`,
    );
    }

    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};
    });