mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 16:25:21 +00:00
Changed structure of example to follow the firebase provided one
This commit is contained in:
5
examples/firebase/.firebaserc
Normal file
5
examples/firebase/.firebaserc
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"projects": {
|
||||||
|
"default": "passkit-generator-cloud-fn"
|
||||||
|
}
|
||||||
|
}
|
||||||
66
examples/firebase/.gitignore
vendored
Normal file
66
examples/firebase/.gitignore
vendored
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
firebase-debug.log*
|
||||||
|
firebase-debug.*.log*
|
||||||
|
|
||||||
|
# Firebase cache
|
||||||
|
.firebase/
|
||||||
|
|
||||||
|
# Firebase config
|
||||||
|
|
||||||
|
# Uncomment this if you'd like others to create their own Firebase project.
|
||||||
|
# For a team working on the same Firebase project(s), it is recommended to leave
|
||||||
|
# it commented so all members can deploy to the same project(s) in .firebaserc.
|
||||||
|
# .firebaserc
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variables file
|
||||||
|
.env
|
||||||
17
examples/firebase/firebase.json
Normal file
17
examples/firebase/firebase.json
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"functions": [
|
||||||
|
{
|
||||||
|
"source": "functions",
|
||||||
|
"codebase": "default",
|
||||||
|
"ignore": [
|
||||||
|
"node_modules",
|
||||||
|
".git",
|
||||||
|
"firebase-debug.log",
|
||||||
|
"firebase-debug.*.log"
|
||||||
|
],
|
||||||
|
"predeploy": [
|
||||||
|
"npm --prefix \"$RESOURCE_DIR\" run build"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
9
examples/firebase/functions/.gitignore
vendored
Normal file
9
examples/firebase/functions/.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Compiled JavaScript files
|
||||||
|
lib/**/*.js
|
||||||
|
lib/**/*.js.map
|
||||||
|
|
||||||
|
# TypeScript v1 declaration files
|
||||||
|
typings/
|
||||||
|
|
||||||
|
# Node.js dependency directory
|
||||||
|
node_modules/
|
||||||
14496
examples/firebase/functions/package-lock.json
generated
Normal file
14496
examples/firebase/functions/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -15,7 +15,7 @@
|
|||||||
"engines": {
|
"engines": {
|
||||||
"node": "16"
|
"node": "16"
|
||||||
},
|
},
|
||||||
"main": "index.js",
|
"main": "lib/index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.2.6",
|
"axios": "^1.2.6",
|
||||||
"firebase-admin": "^10.3.0",
|
"firebase-admin": "^10.3.0",
|
||||||
@@ -1,225 +0,0 @@
|
|||||||
const functions = require("firebase-functions");
|
|
||||||
const admin = require("firebase-admin");
|
|
||||||
const { PKPass } = require("passkit-generator");
|
|
||||||
const fs = require("node:fs");
|
|
||||||
const path = require("node:path");
|
|
||||||
const axios = require("axios");
|
|
||||||
const os = require("node:os");
|
|
||||||
|
|
||||||
// Firebase init
|
|
||||||
admin.initializeApp({
|
|
||||||
credential: admin.credential.cert(require("CERTIFICATE_PATH")),
|
|
||||||
storageBucket: "STORAGE_BUCKET_URL",
|
|
||||||
});
|
|
||||||
|
|
||||||
const storageRef = admin.storage().bucket();
|
|
||||||
|
|
||||||
exports.pass = functions.https.onRequest(async (request, response) => {
|
|
||||||
const newPass = await PKPass.from(
|
|
||||||
{
|
|
||||||
// Get relevant pass model from model folder (see passkit-generator/examples/models/)
|
|
||||||
model: `./model/${request.body.passType}.pass`,
|
|
||||||
certificates: {
|
|
||||||
// Assigning certificates from certs folder (you will need to provide these yourself)
|
|
||||||
wwdr: fs.readFileSync("./certs/wwdr.pem"),
|
|
||||||
signerCert: fs.readFileSync("./certs/signerCert.pem"),
|
|
||||||
signerKey: fs.readFileSync("./certs/signerKey.pem"),
|
|
||||||
signerKeyPassphrase: "PASSPHRASE",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
serialNumber: request.body.serialNumber,
|
|
||||||
description: "DESCRIPTION",
|
|
||||||
logoText: request.body.logoText,
|
|
||||||
foregroundColor: request.body.textColor,
|
|
||||||
backgroundColor: request.body.backgroundColor,
|
|
||||||
labelColor: request.body.labelColor,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
let currentPassType = request.body.passType;
|
|
||||||
|
|
||||||
if (currentPassType == "boardingPass") {
|
|
||||||
newPass.transitType = `PKTransitType${request.body.transitType}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.body.relevantDate !== "Blank") {
|
|
||||||
newPass.setRelevantDate(new Date(request.body.relevantDate));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.body.expiryDate !== "Blank") {
|
|
||||||
newPass.setExpirationDate(new Date(request.body.expiryDate));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
request.body.relevantLocationLat !== "Blank" &&
|
|
||||||
request.body.relevantLocationLong !== "Blank"
|
|
||||||
) {
|
|
||||||
newPass.setLocations({
|
|
||||||
latitude: request.body.relevantLocationLat,
|
|
||||||
longitude: request.body.relevantLocationLong,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < request.body.header.length; i++) {
|
|
||||||
const field = request.body.header[i];
|
|
||||||
|
|
||||||
if (!(field.label && field.value)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
newPass.headerFields.push({
|
|
||||||
key: `header${i}`,
|
|
||||||
label: field.label,
|
|
||||||
value: field.value,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < request.body.primary.length; i++) {
|
|
||||||
const field = request.body.primary[i];
|
|
||||||
|
|
||||||
if (!(field.label && field.value)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
newPass.primaryFields.push({
|
|
||||||
key: `primary${i}`,
|
|
||||||
label: field.label,
|
|
||||||
value:
|
|
||||||
currentPassType == "boardingPass"
|
|
||||||
? field.value.toUpperCase()
|
|
||||||
: field.value,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < request.body.secondary.length; i++) {
|
|
||||||
const field = request.body.secondary[i];
|
|
||||||
|
|
||||||
if (!(field.label && field.value)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const isElementInLastTwoPositions =
|
|
||||||
index === request.body.secondary.length - 2 ||
|
|
||||||
index === request.body.secondary.length - 1;
|
|
||||||
|
|
||||||
newPass.secondaryFields.push({
|
|
||||||
key: `secondary${i}`,
|
|
||||||
label: field.label,
|
|
||||||
value: field.value,
|
|
||||||
textAlignment: isElementInLastTwoPositions
|
|
||||||
? "PKTextAlignmentRight"
|
|
||||||
: "PKTextAlignmentLeft",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < request.body.auxiliary.length; i++) {
|
|
||||||
const field = request.body.auxiliary[i];
|
|
||||||
|
|
||||||
if (!(field.label && field.value)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const isElementInLastTwoPositions =
|
|
||||||
index === request.body.auxiliary.length - 2 ||
|
|
||||||
index === request.body.auxiliary.length - 1;
|
|
||||||
|
|
||||||
newPass.auxiliaryFields.push({
|
|
||||||
key: `auxiliary${i}`,
|
|
||||||
label: field.label,
|
|
||||||
value: field.value,
|
|
||||||
textAlignment: isElementInLastTwoPositions
|
|
||||||
? "PKTextAlignmentRight"
|
|
||||||
: "PKTextAlignmentLeft",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!request.body.codeAlt || request.body.codeAlt.trim() === "") {
|
|
||||||
newPass.setBarcodes({
|
|
||||||
message: request.body.qrText,
|
|
||||||
format: `PKBarcodeFormat${request.body.codeType}`,
|
|
||||||
messageEncoding: "iso-8859-1",
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
newPass.setBarcodes({
|
|
||||||
message: request.body.qrText,
|
|
||||||
format: `PKBarcodeFormat${request.body.codeType}`,
|
|
||||||
messageEncoding: "iso-8859-1",
|
|
||||||
altText: request.body.codeAlt,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Downloading thumbnail and logo files from Firebase Storage and adding to pass
|
|
||||||
if (currentPassType == "generic" || currentPassType == "eventTicket") {
|
|
||||||
const thumbnailFile = request.body.thumbnailFile;
|
|
||||||
const tempPath1 = path.join(os.tmpdir(), thumbnailFile);
|
|
||||||
try {
|
|
||||||
await storageRef
|
|
||||||
.file(`thumbnails/${thumbnailFile}`)
|
|
||||||
.download({ destination: tempPath1 });
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
let buffer = Buffer.alloc(0);
|
|
||||||
try {
|
|
||||||
buffer = fs.readFileSync(tempPath1);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
newPass.addBuffer("thumbnail.png", buffer);
|
|
||||||
newPass.addBuffer("thumbnail@2x.png", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
const logoFile = request.body.logoFile;
|
|
||||||
const tempPath2 = path.join(os.tmpdir(), logoFile);
|
|
||||||
try {
|
|
||||||
await storageRef
|
|
||||||
.file(`logos/${logoFile}`)
|
|
||||||
.download({ destination: tempPath2 });
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
let buffer = Buffer.alloc(0);
|
|
||||||
try {
|
|
||||||
buffer = fs.readFileSync(tempPath2);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
newPass.addBuffer("logo.png", buffer);
|
|
||||||
newPass.addBuffer("logo@2x.png", buffer);
|
|
||||||
|
|
||||||
const bufferData = newPass.getAsBuffer();
|
|
||||||
try {
|
|
||||||
console.log("Pass was uploaded successfully.");
|
|
||||||
response.set("Content-Type", newPass.mimeType);
|
|
||||||
response.status(200).send(bufferData);
|
|
||||||
|
|
||||||
// Delete thumbnail file in Firebase Storage
|
|
||||||
storageRef
|
|
||||||
.file(`thumbnails/${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) {
|
|
||||||
console.log("Error Uploading pass " + error);
|
|
||||||
response.send({
|
|
||||||
explanation: error.message,
|
|
||||||
result: "FAILED",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
4440
examples/firebase/package-lock.json
generated
4440
examples/firebase/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user