Fix presigned URL generation race conditions

This commit is contained in:
2022-02-15 23:16:38 +02:00
committed by Lauri Võsandi
parent a08f1d37b7
commit 52ecba53cc
3 changed files with 20 additions and 18 deletions

@@ -10,6 +10,7 @@ const mongoUri = process.env.MONGO_URI || 'mongodb://127.0.0.1:27017/default?rep
// Minio set-up variables
const minioURI = new URL(process.env.MINIO_URI || 'http://kspace-mugshot:2mSI6HdbJ8@127.0.0.1:9000/kspace-mugshot');
const minioBucket = minioURI.pathname.substring(1);
console.info("Using bucket:", minioBucket);
const historyNumber = parseInt(process.env.HISTORY_AMOUNT) || 1000;
// Stream set-up variables
@@ -44,14 +45,12 @@ async function run() {
secretKey: minioURI.password
});
function wrapEvent(doc) {
async function wrapEvent(doc) {
if (doc && doc.screenshot_count) {
doc.screenshots = [];
for (let i = 1; i <= doc.screenshot_count ; i++) {
minioClient.presignedUrl('GET', minioBucket, `${doc.camera}/${doc._id}/${i}.jpg`, 60 * 60, (err, presignedUrl) => {
if (err) { return console.log(err) };
doc.screenshots.push({ url: presignedUrl });
})
let presignedUrl = await minioClient.presignedUrl('GET', minioBucket, `${doc.camera}/${doc._id}/${i}.jpg`, 60 * 60);
doc.screenshots.push({ url: presignedUrl });
}
};
let blob = JSON.stringify(doc);
@@ -62,14 +61,14 @@ async function run() {
const header = { 'Content-Type': 'text/event-stream', 'Connection': 'keep-alive' };
response.writeHead(200, "OK", header);
const historyCursor = collection.find({}).sort({$natural : -1}).limit(historyNumber);
const historyCursor = collection.find().sort({$natural:-1}).limit(historyNumber);
historyCursor.forEach((document) => {
response.write(wrapEvent(document));
historyCursor.forEach(async (document) => {
response.write(await wrapEvent(document));
})
changeStream.on("change", data => {
response.write(wrapEvent(data.fullDocument));
changeStream.on("change", async(data) => {
response.write(await wrapEvent(data.fullDocument));
});
});