Skip to content

Commit

Permalink
Code Added
Browse files Browse the repository at this point in the history
Code Added
  • Loading branch information
packtbipin2377 committed May 10, 2019
1 parent 9e2b3bf commit 8b8b192
Show file tree
Hide file tree
Showing 87 changed files with 1,317 additions and 0 deletions.
Binary file not shown.
Binary file added 1. Getting Started/first-pwa-start.zip
Binary file not shown.
Binary file added 1. Getting Started/first-pwa.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 not shown.
Binary file not shown.
141 changes: 141 additions & 0 deletions 11. Native Device Features/index.js
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 added 11. Native Device Features/update-files.zip
Binary file not shown.
11 changes: 11 additions & 0 deletions 11. Native Device Features/utility.js
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 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 13. SPAs and PWAs/spa-angular-finished.zip
Binary file not shown.
Binary file added 13. SPAs and PWAs/spa-angular-start.zip
Binary file not shown.
Binary file added 13. SPAs and PWAs/spa-react-finished.zip
Binary file not shown.
Binary file added 13. SPAs and PWAs/spa-vue-finished.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 added 3. The Service Workers/sw-basics-04-finished.zip
Binary file not shown.
Binary file added 4. Promise and Fetch/Thumbs.db
Binary file not shown.
Loading

0 comments on commit 8b8b192

Please sign in to comment.