Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List trigger #52

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
generate preview images of Merklisten
d0ntrash committed Oct 3, 2020
commit 9f4d81cb9ad5ad397a2b9f476020e3bed61db92e
108 changes: 64 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -17,8 +17,8 @@
"@sendgrid/mail": "^7.0.0",
"@slack/webhook": "^5.0.3",
"@types/mkdirp": "^1.0.1",
"@types/sharp": "^0.25.1",
"@types/node": "^14.0.27",
"@types/sharp": "^0.25.1",
"@types/tmp": "^0.2.0",
"algoliasearch": "^4.3.0",
"axios": "^0.19.2",
@@ -28,12 +28,12 @@
"firebase-admin": "^8.6.0",
"firebase-functions": "^3.7.0",
"fs": "0.0.1-security",
"pure-uuid": "^1.6.1",
"geofirestore": "^3.4.1",
"javascript-stringify": "^2.0.1",
"mkdirp": "^1.0.4",
"moment": "^2.25.2",
"sharp": "^0.25.2",
"pure-uuid": "^1.6.1",
"sharp": "^0.25.4",
"tmp": "^0.2.1"
},
"devDependencies": {
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as fs from "fs";
import UUID from "pure-uuid";
import * as admin from 'firebase-admin';
import * as os from "os";
import { OverlayOptions } from "sharp";
const mkdirp = require("mkdirp");
const path = require("path");
const sharp = require("sharp");

const width = 224;
const height = 224;
const logoPath = "./sources/lokalkauf-logo.jpg";
const logoPathBig = "./sources/lokalkauf-logo-big.jpg";
const logoPath = "Merklisten/Logos/lokalkauf-logo.jpg";
const logoPathBig = "Merklisten/Logos/lokalkauf-logo-big.jpg";

interface Dimensions {
width: number;
@@ -104,42 +104,78 @@ async function calculateBaseImageSize(imagePaths: string[]): Promise<Dimensions>
return { width: width * 3, height: height * Math.ceil(imagePaths.length / 3) + (imagePaths.length % 3 === 0 ? height : 0) };
}

export async function imageBuilder(imagePaths: string[]): Promise<boolean> {
export async function imageBuilder(imagePaths: string[], merklisteid: string) {
if (imagePaths.length === 0) {
return false;
return new Promise((_, reject) =>
reject("1. No image path provided " + merklisteid));
}

const tempLocalDir = path.join(".", imagePaths.length + "-" + new UUID(4).format());
await mkdirp(tempLocalDir);
if (await fs.existsSync(tempLocalDir)) {
const lst: OverlayOptions[] = await extractImages(imagePaths);
const bucket = admin.storage().bucket();
const tempDir = os.tmpdir();

lst.push(placeLokalkaufLogo(imagePaths));
const tempPaths: string[] = [];
await Promise.all(imagePaths.map(
async imagePath => {
if (imagePath && imagePath.length > 1) {
const remoteFile = bucket.file(imagePath);
const originalFile = path.join(tempDir, imagePath);
const tempLocalDir = path.dirname(originalFile);
await mkdirp(tempLocalDir);
await remoteFile.download({ destination: originalFile });
tempPaths.push(originalFile);
}
}
));

if (tempPaths.length === 0) {
return new Promise((_, reject) =>
reject("2. No image path provided" + merklisteid));
}

const dimensions = await calculateBaseImageSize(imagePaths);
const lst: OverlayOptions[] = await extractImages(tempPaths);

sharp({
create: {
width: dimensions.width,
height: dimensions.height,
channels: 3,
background: { r: 255, g: 255, b: 255 },
},
const logoOptions = placeLokalkaufLogo(imagePaths);
if (logoOptions && logoOptions.input) {
console.log("logoOptions ", logoOptions);
const remoteFile = bucket.file(logoOptions.input.toString());
const originalFile = path.join(tempDir, logoOptions.input.toString());
const tempLocalDir = path.dirname(originalFile);
await mkdirp(tempLocalDir);
await remoteFile.download({ destination: originalFile });
const newOptions = { ...logoOptions, input: originalFile };
console.log("newOptions", newOptions);
lst.push(newOptions);
};

const dimensions = await calculateBaseImageSize(tempPaths);
const metadata = {
contentType: 'image/jpg',
};
sharp({
create: {
width: dimensions.width,
height: dimensions.height,
channels: 3,
background: { r: 255, g: 255, b: 255 },
},
})
.jpeg()
.composite(lst)
.sharpen()
.withMetadata()
.jpeg({ quality: 90 })
.toBuffer()
.then((data: any, info: any) => {
console.log(info);
bucket.file(`Merklisten/${merklisteid}/preview.jpg`).save(data, metadata, (err) => { console.log(err) });
return new Promise((succ, _) =>
succ());
})
.jpeg()
.composite(lst)
.sharpen()
.withMetadata()
.jpeg({ quality: 90 })
.toFile(tempLocalDir + "/desti-" + imagePaths.length + ".jpg")
.then((info: any) => {
console.log(info);
})
.catch((err: any) => {
console.log(err);
});
}
return true;
.catch((err: any) => {
console.log(err);
return new Promise((_, reject) =>
reject(err));
});
}
/*
imageBuilder(["./sources/a.jpg", "./sources/b.jpg"]);
18 changes: 17 additions & 1 deletion src/triggers/publicBookmark.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import { imageBuilder } from '../common/services/generate-some-merkliste';



export const updatePublicBookmark = functions.firestore.document("/Merkliste/{merklisteId}").onUpdate(async (snap, _) => {
const privateBookmark = snap.after.data();
@@ -14,6 +17,19 @@ export const updatePublicBookmark = functions.firestore.document("/Merkliste/{me
isActive: privateBookmark.publicactive ? privateBookmark.publicactive : false,
})
.catch((err) => console.log(`Public document does not exist: ${err}`));
const imagePaths: string[] = [];
await Promise.all(privateBookmark.bookmarks.map(
async (bookmark: any) => {
const trader = (await admin.firestore().doc(`Traders/${bookmark.traderid}`).get()).data();
if (trader && trader.defaultImagePath && trader.defaultImagePath.length > 0) {
const defaultImage = trader.defaultImagePath.substring(trader.defaultImagePath.lastIndexOf("/") + 1);
imagePaths.push(`Traders/${bookmark.traderid}/BusinessImages/thumb_224x224_${defaultImage}.jpg`);
}
}
));
imageBuilder(imagePaths, privateBookmark.publicid)
.then((result) => console.log("Result: " + result))
.catch((err) => console.log("Error: " + err));
}
});

@@ -22,7 +38,7 @@ export const deletePublicBookmark = functions.firestore.document("/Merkliste/{me
if (privateBookmark && privateBookmark.publicid) {
await admin
.firestore()
.doc(`Merkliste_PUBLIC/${privateBookmark.publicid}`)
.doc(`Merkliste_PUBLIC / ${privateBookmark.publicid}`)
.delete()
.catch((err) => console.log(`Public document does not exist: ${err}`));
}