mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 21:25:26 +00:00
Improved storage loading and removed files removing from storage
This commit is contained in:
@@ -1,24 +1,19 @@
|
|||||||
import * as functions from "firebase-functions";
|
import * as functions from "firebase-functions";
|
||||||
import admin from "firebase-admin";
|
import { initializeApp } from "firebase-admin/app";
|
||||||
|
import { getStorage } from "firebase-admin/storage";
|
||||||
import passkit from "passkit-generator";
|
import passkit from "passkit-generator";
|
||||||
import type { Barcode, TransitType } from "passkit-generator";
|
import type { Barcode, TransitType } from "passkit-generator";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
|
|
||||||
|
// Please note this is experimental in NodeJS as
|
||||||
|
// it is marked as Stage 3 in TC39
|
||||||
|
// Should probably not be used in production
|
||||||
|
import startData from "./startData.json" assert { "type": "json" };
|
||||||
|
|
||||||
const PKPass = passkit.PKPass;
|
const PKPass = passkit.PKPass;
|
||||||
|
|
||||||
// Firebase init
|
|
||||||
admin
|
|
||||||
.initializeApp
|
|
||||||
// {
|
|
||||||
// credential: admin.credential.cert(require("CERTIFICATE_PATH")),
|
|
||||||
// storageBucket: "STORAGE_BUCKET_URL",
|
|
||||||
// }
|
|
||||||
();
|
|
||||||
|
|
||||||
const storageRef = admin.storage().bucket();
|
|
||||||
|
|
||||||
interface RequestWithBody extends functions.Request {
|
interface RequestWithBody extends functions.Request {
|
||||||
body: {
|
body: {
|
||||||
passModel: string;
|
passModel: string;
|
||||||
@@ -56,6 +51,13 @@ declare global {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Firebase init
|
||||||
|
initializeApp({
|
||||||
|
storageBucket: startData.FIREBASE_BUCKET_ADDR,
|
||||||
|
});
|
||||||
|
|
||||||
|
const storageRef = getStorage().bucket();
|
||||||
|
|
||||||
export const pass = functions.https.onRequest(
|
export const pass = functions.https.onRequest(
|
||||||
async (request: RequestWithBody, response) => {
|
async (request: RequestWithBody, response) => {
|
||||||
try {
|
try {
|
||||||
@@ -283,28 +285,6 @@ export const pass = functions.https.onRequest(
|
|||||||
|
|
||||||
response.set("Content-Type", newPass.mimeType);
|
response.set("Content-Type", newPass.mimeType);
|
||||||
response.status(200).send(bufferData);
|
response.status(200).send(bufferData);
|
||||||
|
|
||||||
// Delete thumbnail file in Firebase Storage
|
|
||||||
storageRef
|
|
||||||
.file(`thumbnails/${request.body.thumbnailFile}`)
|
|
||||||
.delete()
|
|
||||||
.then(() => {
|
|
||||||
console.log("Thumbnail file deleted successfully");
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error(error);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Delete logo file in Firebase Storage
|
|
||||||
storageRef
|
|
||||||
.file(`logos/${logoFile}`)
|
|
||||||
.delete()
|
|
||||||
.then(() => {
|
|
||||||
console.log("Logo file deleted successfully");
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error(error);
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Error Uploading pass " + error);
|
console.log("Error Uploading pass " + error);
|
||||||
|
|
||||||
|
|||||||
3
examples/firebase/functions/src/startData.json
Normal file
3
examples/firebase/functions/src/startData.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"FIREBASE_BUCKET_ADDR": "<insert your firebase storage bucket address (the part right after 'gs://...')"
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@
|
|||||||
"noImplicitOverride": true,
|
"noImplicitOverride": true,
|
||||||
"noPropertyAccessFromIndexSignature": true,
|
"noPropertyAccessFromIndexSignature": true,
|
||||||
"allowUnusedLabels": false,
|
"allowUnusedLabels": false,
|
||||||
"allowUnreachableCode": false
|
"allowUnreachableCode": false,
|
||||||
|
"resolveJsonModule": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user