Added approach to allow versioning of PDFs (Requires env var!)

This commit is contained in:
Joao Ramos
2024-02-05 20:01:50 +00:00
parent 8baa25c445
commit 934394b17f
2 changed files with 20 additions and 12 deletions

View File

@@ -125,7 +125,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
const stats = docsSnap.docs.map((d) => d.data());
// verify if the stats already have a pdf generated
const hasPDF = stats.find((s) => s.pdf);
const hasPDF = stats.find((s) => s.pdf?.path && s.pdf?.version === process.env.PDF_VERSION);
// find the user that generated the stats
const statIndex = stats.findIndex((s) => s.user);
@@ -138,7 +138,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
if (hasPDF) {
// if it does, return the pdf url
const fileRef = ref(storage, hasPDF.pdf);
const fileRef = ref(storage, hasPDF.pdf.path);
const url = await getDownloadURL(fileRef);
res.status(200).end(url);
@@ -305,7 +305,10 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
// update the stats entries with the pdf url to prevent duplication
docsSnap.docs.forEach(async (doc) => {
await updateDoc(doc.ref, {
pdf: refName,
pdf: {
path: refName,
version: process.env.PDF_VERSION,
},
});
});
const url = await getDownloadURL(fileRef);
@@ -336,10 +339,10 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
const stats = docsSnap.docs.map((d) => d.data());
const hasPDF = stats.find((s) => s.pdf);
const hasPDF = stats.find((s) => s.pdf?.path);
if (hasPDF) {
const fileRef = ref(storage, hasPDF.pdf);
const fileRef = ref(storage, hasPDF.pdf.path);
const url = await getDownloadURL(fileRef);
return res.redirect(url);
}