Created
March 18, 2019 09:14
-
-
Save SHoogland/ff164117667af9a1814757f708cd0ec7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const firebaseAdmin = require('firebase-admin'); | |
firebaseAdmin.initializeApp(); | |
exports.firestore = firebaseAdmin.firestore(); | |
exports.auth = firebaseAdmin.auth(); | |
exports.storage = firebaseAdmin.storage(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
const firebaseAdmin = require('./firebase-admin'); | |
exports.handler = async (req, res) => { | |
res.locals.browser = await puppeteer.launch({ | |
args: ['--no-sandbox'] | |
}); | |
const url = req.query.url; | |
if (!url) { | |
res.status(400).send('Please provide a URL. Example: ?url=https://example.com'); | |
return; | |
} | |
const browser = res.locals.browser; | |
try { | |
const page = await browser.newPage(); | |
await page.setViewport({ width: 1280, height: 1280 }); | |
page.on('console', msg => { | |
console.info('console', msg._text); | |
}); | |
page.on('error', msg => { | |
console.info('error', msg); | |
}); | |
page.on('pageerror', msg => { | |
console.info('pageerror', msg); | |
}); | |
page.on('requestfailed', msg => { | |
console.info('requestfailed', msg._url); | |
}); | |
await page.goto(url, { waitUntil: 'networkidle2' }); | |
let date = new Date(); | |
let timestamp = date.getTime(); | |
const screenshot = await page.screenshot({ fullPage: true }); | |
// Instantiate the GCP Storage instance | |
var bucket = firebaseAdmin.storage.bucket(); | |
// Upload the image to the bucket | |
var file = bucket.file(`screenshot-${timestamp}.png`); | |
await file.save(screenshot, { | |
metadata: { contentType: 'image/png' }, | |
public: true, | |
validation: 'md5' | |
}); | |
res.send('captured and saved a screenshot'); | |
} catch (e) { | |
console.log(e); | |
res.status(500).send(e.toString()); | |
} | |
await browser.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
service firebase.storage { | |
match /b/{bucket}/o { | |
match /{allPaths=**} { | |
allow read, write: if false; | |
} | |
match /images/{imageId} { | |
allow read, write: if request.auth != null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment