mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-16 01:25:30 +00:00
Fixed code to run on tabs
This commit is contained in:
@@ -1,20 +1,21 @@
|
|||||||
const functions = require("firebase-functions");
|
const functions = require("firebase-functions");
|
||||||
const { PKPass } = require("passkit-generator");
|
const { PKPass } = require("passkit-generator");
|
||||||
const admin = require("firebase-admin");
|
const admin = require("firebase-admin");
|
||||||
var fs = require('file-system');
|
var fs = require("file-system");
|
||||||
var path = require('path');
|
var path = require("path");
|
||||||
var axios = require('axios');
|
var axios = require("axios");
|
||||||
var os = require('os');
|
var os = require("os");
|
||||||
|
|
||||||
// Firebase init
|
// Firebase init
|
||||||
admin.initializeApp({
|
admin.initializeApp({
|
||||||
credential: admin.credential.cert(require("CERTIFICATE_PATH")),
|
credential: admin.credential.cert(require("CERTIFICATE_PATH")),
|
||||||
storageBucket: "STORAGE_BUCKET_URL"
|
storageBucket: "STORAGE_BUCKET_URL",
|
||||||
});
|
});
|
||||||
var storageRef = admin.storage().bucket()
|
var storageRef = admin.storage().bucket();
|
||||||
|
|
||||||
exports.pass = functions.https.onRequest((request, response) => {
|
exports.pass = functions.https.onRequest((request, response) => {
|
||||||
PKPass.from({
|
PKPass.from(
|
||||||
|
{
|
||||||
// Get relevant pass model from model folder (see passkit-generator/examples/models/)
|
// Get relevant pass model from model folder (see passkit-generator/examples/models/)
|
||||||
model: `./model/${request.body.passType}.pass`,
|
model: `./model/${request.body.passType}.pass`,
|
||||||
certificates: {
|
certificates: {
|
||||||
@@ -22,7 +23,7 @@ exports.pass = functions.https.onRequest((request, response) => {
|
|||||||
wwdr: fs.fs.readFileSync("./certs/wwdr.pem"),
|
wwdr: fs.fs.readFileSync("./certs/wwdr.pem"),
|
||||||
signerCert: fs.fs.readFileSync("./certs/signerCert.pem"),
|
signerCert: fs.fs.readFileSync("./certs/signerCert.pem"),
|
||||||
signerKey: fs.fs.readFileSync("./certs/signerKey.pem"),
|
signerKey: fs.fs.readFileSync("./certs/signerKey.pem"),
|
||||||
signerKeyPassphrase: "PASSPHRASE"
|
signerKeyPassphrase: "PASSPHRASE",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -31,10 +32,9 @@ exports.pass = functions.https.onRequest((request, response) => {
|
|||||||
logoText: request.body.logoText,
|
logoText: request.body.logoText,
|
||||||
foregroundColor: request.body.textColor,
|
foregroundColor: request.body.textColor,
|
||||||
backgroundColor: request.body.backgroundColor,
|
backgroundColor: request.body.backgroundColor,
|
||||||
labelColor: request.body.labelColor
|
labelColor: request.body.labelColor,
|
||||||
})
|
},
|
||||||
.then(async (newPass) => {
|
).then(async (newPass) => {
|
||||||
|
|
||||||
let currentPassType = request.body.passType;
|
let currentPassType = request.body.passType;
|
||||||
|
|
||||||
if (currentPassType == "boardingPass") {
|
if (currentPassType == "boardingPass") {
|
||||||
@@ -49,12 +49,18 @@ exports.pass = functions.https.onRequest((request, response) => {
|
|||||||
newPass.setExpirationDate(new Date(request.body.expiryDate));
|
newPass.setExpirationDate(new Date(request.body.expiryDate));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.body.relevantLocationLat !== "Blank" && request.body.relevantLocationLong !== "Blank") {
|
if (
|
||||||
newPass.setLocations({latitude: request.body.relevantLocationLat, longitude: request.body.relevantLocationLong});
|
request.body.relevantLocationLat !== "Blank" &&
|
||||||
|
request.body.relevantLocationLong !== "Blank"
|
||||||
|
) {
|
||||||
|
newPass.setLocations({
|
||||||
|
latitude: request.body.relevantLocationLat,
|
||||||
|
longitude: request.body.relevantLocationLong,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
request.body.header.forEach((field, index) => {
|
request.body.header.forEach((field, index) => {
|
||||||
if (field.label !== '' || field.value !== '') {
|
if (field.label !== "" || field.value !== "") {
|
||||||
newPass.headerFields.push({
|
newPass.headerFields.push({
|
||||||
key: `header${index}`,
|
key: `header${index}`,
|
||||||
label: field.label,
|
label: field.label,
|
||||||
@@ -64,33 +70,44 @@ exports.pass = functions.https.onRequest((request, response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
request.body.primary.forEach((field, index) => {
|
request.body.primary.forEach((field, index) => {
|
||||||
if (field.label !== '' || field.value !== '') {
|
if (field.label !== "" || field.value !== "") {
|
||||||
newPass.primaryFields.push({
|
newPass.primaryFields.push({
|
||||||
key: `primary${index}`,
|
key: `primary${index}`,
|
||||||
label: field.label,
|
label: field.label,
|
||||||
value: (currentPassType == "boardingPass") ? field.value.toUpperCase() : field.value,
|
value:
|
||||||
|
currentPassType == "boardingPass"
|
||||||
|
? field.value.toUpperCase()
|
||||||
|
: field.value,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
request.body.secondary.forEach((field, index) => {
|
request.body.secondary.forEach((field, index) => {
|
||||||
if (field.label !== '' || field.value !== '') {
|
if (field.label !== "" || field.value !== "") {
|
||||||
newPass.secondaryFields.push({
|
newPass.secondaryFields.push({
|
||||||
key: `secondary${index}`,
|
key: `secondary${index}`,
|
||||||
label: field.label,
|
label: field.label,
|
||||||
value: field.value,
|
value: field.value,
|
||||||
textAlignment: (index === request.body.secondary.length - 2 || index === request.body.secondary.length - 1) ? "PKTextAlignmentRight" : "PKTextAlignmentLeft",
|
textAlignment:
|
||||||
|
index === request.body.secondary.length - 2 ||
|
||||||
|
index === request.body.secondary.length - 1
|
||||||
|
? "PKTextAlignmentRight"
|
||||||
|
: "PKTextAlignmentLeft",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
request.body.auxiliary.forEach((field, index) => {
|
request.body.auxiliary.forEach((field, index) => {
|
||||||
if (field.label !== '' || field.value !== '') {
|
if (field.label !== "" || field.value !== "") {
|
||||||
newPass.auxiliaryFields.push({
|
newPass.auxiliaryFields.push({
|
||||||
key: `auxiliary${index}`,
|
key: `auxiliary${index}`,
|
||||||
label: field.label,
|
label: field.label,
|
||||||
value: field.value,
|
value: field.value,
|
||||||
textAlignment: (index === request.body.secondary.length - 2 || index === request.body.secondary.length - 1) ? "PKTextAlignmentRight" : "PKTextAlignmentLeft",
|
textAlignment:
|
||||||
|
index === request.body.secondary.length - 2 ||
|
||||||
|
index === request.body.secondary.length - 1
|
||||||
|
? "PKTextAlignmentRight"
|
||||||
|
: "PKTextAlignmentLeft",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -112,66 +129,76 @@ exports.pass = functions.https.onRequest((request, response) => {
|
|||||||
|
|
||||||
// Downloading thumbnail and logo files from Firebase Storage and adding to pass
|
// Downloading thumbnail and logo files from Firebase Storage and adding to pass
|
||||||
if (currentPassType == "generic" || currentPassType == "eventTicket") {
|
if (currentPassType == "generic" || currentPassType == "eventTicket") {
|
||||||
const thumbnailFile = request.body.thumbnailFile
|
const thumbnailFile = request.body.thumbnailFile;
|
||||||
const tempPath1 = path.join(os.tmpdir(), thumbnailFile)
|
const tempPath1 = path.join(os.tmpdir(), thumbnailFile);
|
||||||
try {
|
try {
|
||||||
await storageRef.file(`thumbnails/${thumbnailFile}`).download({destination: tempPath1})
|
await storageRef
|
||||||
|
.file(`thumbnails/${thumbnailFile}`)
|
||||||
|
.download({ destination: tempPath1 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error);
|
||||||
}
|
}
|
||||||
let buffer = Buffer.alloc(0);
|
let buffer = Buffer.alloc(0);
|
||||||
try {
|
try {
|
||||||
buffer = fs.readFileSync(tempPath1)
|
buffer = fs.readFileSync(tempPath1);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error);
|
||||||
}
|
}
|
||||||
newPass.addBuffer("thumbnail.png", buffer)
|
newPass.addBuffer("thumbnail.png", buffer);
|
||||||
newPass.addBuffer("thumbnail@2x.png", buffer)
|
newPass.addBuffer("thumbnail@2x.png", buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
const logoFile = request.body.logoFile
|
const logoFile = request.body.logoFile;
|
||||||
const tempPath2 = path.join(os.tmpdir(), logoFile)
|
const tempPath2 = path.join(os.tmpdir(), logoFile);
|
||||||
try {
|
try {
|
||||||
await storageRef.file(`logos/${logoFile}`).download({destination: tempPath2})
|
await storageRef
|
||||||
|
.file(`logos/${logoFile}`)
|
||||||
|
.download({ destination: tempPath2 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error);
|
||||||
}
|
}
|
||||||
let buffer = Buffer.alloc(0);
|
let buffer = Buffer.alloc(0);
|
||||||
try {
|
try {
|
||||||
buffer = fs.readFileSync(tempPath2)
|
buffer = fs.readFileSync(tempPath2);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error);
|
||||||
}
|
}
|
||||||
newPass.addBuffer("logo.png", buffer)
|
newPass.addBuffer("logo.png", buffer);
|
||||||
newPass.addBuffer("logo@2x.png", buffer)
|
newPass.addBuffer("logo@2x.png", buffer);
|
||||||
|
|
||||||
|
|
||||||
const bufferData = newPass.getAsBuffer();
|
const bufferData = newPass.getAsBuffer();
|
||||||
try {
|
try {
|
||||||
console.log("Pass was uploaded successfully.");
|
console.log("Pass was uploaded successfully.");
|
||||||
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
|
// Delete thumbnail file in Firebase Storage
|
||||||
storageRef.file(`thumbnails/${thumbnailFile}`).delete().then(() => {
|
storageRef
|
||||||
console.log('Thumbnail file deleted successfully');
|
.file(`thumbnails/${thumbnailFile}`)
|
||||||
}).catch((error) => {
|
.delete()
|
||||||
console.error(error);
|
.then(() => {
|
||||||
|
console.log("Thumbnail file deleted successfully");
|
||||||
})
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(error);
|
||||||
|
});
|
||||||
|
|
||||||
// Delete logo file in Firebase Storage
|
// Delete logo file in Firebase Storage
|
||||||
storageRef.file(`logos/${logoFile}`).delete().then(() => {
|
storageRef
|
||||||
console.log('Logo file deleted successfully');
|
.file(`logos/${logoFile}`)
|
||||||
}).catch((error) => {
|
.delete()
|
||||||
console.error(error);
|
.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);
|
||||||
response.send({
|
response.send({
|
||||||
"explanation": error.message,
|
explanation: error.message,
|
||||||
"result": "FAILED",
|
result: "FAILED",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user