-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
87 changed files
with
1,317 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
var functions = require("firebase-functions"); | ||
var admin = require("firebase-admin"); | ||
var cors = require("cors")({ origin: true }); | ||
var webpush = require("web-push"); | ||
var formidable = require("formidable"); | ||
var fs = require("fs"); | ||
var UUID = require("uuid-v4"); | ||
var os = require("os"); | ||
var Busboy = require("busboy"); | ||
var path = require('path'); | ||
|
||
// // Create and Deploy Your First Cloud Functions | ||
// // https://firebase.google.com/docs/functions/write-firebase-functions | ||
// | ||
|
||
var serviceAccount = require("./pwagram-fb-key.json"); | ||
|
||
var gcconfig = { | ||
projectId: "YOUR_PROJECT_ID", | ||
keyFilename: "pwagram-fb-key.json" | ||
}; | ||
|
||
var gcs = require("@google-cloud/storage")(gcconfig); | ||
|
||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount), | ||
databaseURL: "https://YOUR_PROJECT_ID.firebaseio.com/" | ||
}); | ||
|
||
exports.storePostData = functions.https.onRequest(function(request, response) { | ||
cors(request, response, function() { | ||
var uuid = UUID(); | ||
|
||
const busboy = new Busboy({ headers: request.headers }); | ||
// These objects will store the values (file + fields) extracted from busboy | ||
let upload; | ||
const fields = {}; | ||
|
||
// This callback will be invoked for each file uploaded | ||
busboy.on("file", (fieldname, file, filename, encoding, mimetype) => { | ||
console.log( | ||
`File [${fieldname}] filename: ${filename}, encoding: ${encoding}, mimetype: ${mimetype}` | ||
); | ||
const filepath = path.join(os.tmpdir(), filename); | ||
upload = { file: filepath, type: mimetype }; | ||
file.pipe(fs.createWriteStream(filepath)); | ||
}); | ||
|
||
// This will invoked on every field detected | ||
busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) { | ||
fields[fieldname] = val; | ||
}); | ||
|
||
// This callback will be invoked after all uploaded files are saved. | ||
busboy.on("finish", () => { | ||
var bucket = gcs.bucket("YOUR_PROJECT_ID.appspot.com"); | ||
bucket.upload( | ||
upload.file, | ||
{ | ||
uploadType: "media", | ||
metadata: { | ||
metadata: { | ||
contentType: upload.type, | ||
firebaseStorageDownloadTokens: uuid | ||
} | ||
} | ||
}, | ||
function(err, uploadedFile) { | ||
if (!err) { | ||
admin | ||
.database() | ||
.ref("posts") | ||
.push({ | ||
id: fields.id, | ||
title: fields.title, | ||
location: fields.location, | ||
image: | ||
"https://firebasestorage.googleapis.com/v0/b/" + | ||
bucket.name + | ||
"/o/" + | ||
encodeURIComponent(uploadedFile.name) + | ||
"?alt=media&token=" + | ||
uuid | ||
}) | ||
.then(function() { | ||
webpush.setVapidDetails( | ||
"mailto:[email protected]", | ||
"BKapuZ3XLgt9UZhuEkodCrtnfBo9Smo-w1YXCIH8YidjHOFAU6XHpEnXefbuYslZY9vtlEnOAmU7Mc-kWh4gfmE", | ||
"AyVHwGh16Kfxrh5AU69E81nVWIKcUwR6a9f1X4zXT_s" | ||
); | ||
return admin | ||
.database() | ||
.ref("subscriptions") | ||
.once("value"); | ||
}) | ||
.then(function(subscriptions) { | ||
subscriptions.forEach(function(sub) { | ||
var pushConfig = { | ||
endpoint: sub.val().endpoint, | ||
keys: { | ||
auth: sub.val().keys.auth, | ||
p256dh: sub.val().keys.p256dh | ||
} | ||
}; | ||
|
||
webpush | ||
.sendNotification( | ||
pushConfig, | ||
JSON.stringify({ | ||
title: "New Post", | ||
content: "New Post added!", | ||
openUrl: "/help" | ||
}) | ||
) | ||
.catch(function(err) { | ||
console.log(err); | ||
}); | ||
}); | ||
response | ||
.status(201) | ||
.json({ message: "Data stored", id: fields.id }); | ||
}) | ||
.catch(function(err) { | ||
response.status(500).json({ error: err }); | ||
}); | ||
} else { | ||
console.log(err); | ||
} | ||
} | ||
); | ||
}); | ||
|
||
// The raw bytes of the upload will be in request.rawBody. Send it to busboy, and get | ||
// a callback when it's finished. | ||
busboy.end(request.rawBody); | ||
// formData.parse(request, function(err, fields, files) { | ||
// fs.rename(files.file.path, "/tmp/" + files.file.name); | ||
// var bucket = gcs.bucket("YOUR_PROJECT_ID.appspot.com"); | ||
// }); | ||
}); | ||
}); |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function dataURItoBlob(dataURI) { | ||
var byteString = atob(dataURI.split(',')[1]); | ||
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0] | ||
var ab = new ArrayBuffer(byteString.length); | ||
var ia = new Uint8Array(ab); | ||
for (var i = 0; i < byteString.length; i++) { | ||
ia[i] = byteString.charCodeAt(i); | ||
} | ||
var blob = new Blob([ab], {type: mimeString}); | ||
return blob; | ||
} |
Binary file not shown.
Binary file added
BIN
+919 KB
12. Service Worker Management with Workbox/workbox-02-adjust-precaching.zip
Binary file not shown.
Binary file added
BIN
+919 KB
12. Service Worker Management with Workbox/workbox-03-inject-manifest.zip
Binary file not shown.
Binary file added
BIN
+919 KB
12. Service Worker Management with Workbox/workbox-04-added-routing.zip
Binary file not shown.
Binary file added
BIN
+919 KB
12. Service Worker Management with Workbox/workbox-05-strategy-options.zip
Binary file not shown.
Binary file added
BIN
+919 KB
12. Service Worker Management with Workbox/workbox-06-custom-handler.zip
Binary file not shown.
Binary file added
BIN
+920 KB
12. Service Worker Management with Workbox/workbox-07-offline-fallback.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+691 KB
2. Understanding the App Manifest/app-manifest-02-added-properties.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.