Skip to content

Commit 8e96ef3

Browse files
committed
add firebase files for easier review
1 parent 832da6c commit 8e96ef3

File tree

7 files changed

+253
-0
lines changed

7 files changed

+253
-0
lines changed

β€Žscripts/firebase/.eslintrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "standard",
3+
"rules": {
4+
"semi": ["error", "always"],
5+
"space-before-function-paren": ["error", "never"]
6+
}
7+
}

β€Žscripts/firebase/.firebaserc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "ng-i-1121e"
4+
}
5+
}

β€Žscripts/firebase/firebase.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"hosting": {
3+
"public": "public",
4+
"redirects": [ {
5+
"source" : "/:version/docs",
6+
"destination" : "/:version/docs/index.html",
7+
"type" : 302
8+
}],
9+
"rewrites": [
10+
{
11+
"source": "/**",
12+
"function": "sendStoredFile"
13+
}
14+
]
15+
}
16+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const functions = require('firebase-functions');
2+
const gcs = require('@google-cloud/storage')();
3+
const path = require('path');
4+
5+
const LOCAL_TMP_FOLDER = '/tmp/';
6+
7+
function sendStoredFile(request, response) {
8+
let filePathSegments = request.path.split('/').filter((segment) => {
9+
return segment !== '';
10+
});
11+
const version = filePathSegments[0];
12+
const isDocsPath = filePathSegments[1] === 'docs';
13+
const lastSegment = filePathSegments[filePathSegments.length - 1];
14+
let downloadPath;
15+
let fileName;
16+
17+
if (isDocsPath && filePathSegments.length === 2) {
18+
fileName = 'index.html';
19+
filePathSegments = [version, 'docs', fileName];
20+
} else {
21+
fileName = lastSegment;
22+
}
23+
24+
downloadPath = path.join.apply(null, filePathSegments);
25+
26+
const bucket = gcs.bucket('ng-i-1121e.appspot.com');
27+
28+
downloadAndSend().catch(error => {
29+
30+
if (isDocsPath && error.code === 404) {
31+
fileName = 'index.html';
32+
filePathSegments = [version, 'docs', fileName];
33+
downloadPath = path.join.apply(null, filePathSegments);
34+
return downloadAndSend();
35+
}
36+
37+
return Promise.reject(error);
38+
}).catch(error => {
39+
let message = 'General error';
40+
if (error.code === 404) {
41+
if (fileName.split('.').length === 1) {
42+
message = 'Directory listing is not supported';
43+
} else {
44+
message = 'File not found';
45+
}
46+
}
47+
48+
return response.status(error.code).send(message);
49+
});
50+
51+
function downloadAndSend() {
52+
return bucket.file(downloadPath).download({
53+
destination: `/tmp/${fileName}`
54+
}).then(() => {
55+
return response.status(200).set({
56+
'Content-Encoding': 'gzip',
57+
'Cache-Control': 'public, max-age=300, s-maxage=600'
58+
}).sendFile(`${LOCAL_TMP_FOLDER}${fileName}`);
59+
});
60+
}
61+
}
62+
63+
exports.sendStoredFile = functions.https.onRequest(sendStoredFile);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "functions",
3+
"description": "Cloud Functions for Firebase",
4+
"dependencies": {
5+
"@google-cloud/storage": "^1.1.1",
6+
"firebase-admin": "~4.2.1",
7+
"firebase-functions": "^0.5.7",
8+
"mkdirp-promise": "^5.0.1",
9+
"yauzl": "^2.8.0"
10+
},
11+
"private": true
12+
}

β€Žscripts/firebase/public/404.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Page Not Found</title>
7+
8+
<style media="screen">
9+
body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
10+
#message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px 16px; border-radius: 3px; }
11+
#message h3 { color: #888; font-weight: normal; font-size: 16px; margin: 16px 0 12px; }
12+
#message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; }
13+
#message h1 { font-size: 22px; font-weight: 300; color: rgba(0,0,0,0.6); margin: 0 0 16px;}
14+
#message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
15+
#message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
16+
#message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
17+
#load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
18+
@media (max-width: 600px) {
19+
body, #message { margin-top: 0; background: white; box-shadow: none; }
20+
body { border-top: 16px solid #ffa100; }
21+
}
22+
</style>
23+
</head>
24+
<body>
25+
<div id="message">
26+
<h2>404</h2>
27+
<h1>Page Not Found</h1>
28+
<p>The specified file was not found on this website. Please check the URL for mistakes and try again.</p>
29+
<h3>Why am I seeing this?</h3>
30+
<p>This page was generated by the Firebase Command-Line Interface. To modify it, edit the <code>404.html</code> file in your project's configured <code>public</code> directory.</p>
31+
</div>
32+
</body>
33+
</html>

β€Žscripts/firebase/public/index.html

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Welcome to Firebase Hosting</title>
7+
8+
<!-- update the version number as needed -->
9+
<script defer src="/__/firebase/4.1.2/firebase-app.js"></script>
10+
<!-- include only the Firebase features as you need -->
11+
<script defer src="/__/firebase/4.1.2/firebase-auth.js"></script>
12+
<script defer src="/__/firebase/4.1.2/firebase-database.js"></script>
13+
<script defer src="/__/firebase/4.1.2/firebase-messaging.js"></script>
14+
<script defer src="/__/firebase/4.1.2/firebase-storage.js"></script>
15+
<!-- initialize the SDK after all desired features are loaded -->
16+
<script defer src="/__/firebase/init.js"></script>
17+
<script src="https://ng-i-1121e.firebaseapp.com/files/angular-gz.js" type="application/javascript"></script>
18+
<script src="angular.js" type="application/javascript"></script>
19+
<script src="http://localhost:1111/angular-gz.js" type="application/javascript"></script>
20+
<script src="angular-gz.js" type="application/javascript"></script>
21+
22+
<style media="screen">
23+
body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
24+
#message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px; border-radius: 3px; }
25+
#message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; }
26+
#message h1 { font-size: 22px; font-weight: 300; color: rgba(0,0,0,0.6); margin: 0 0 16px;}
27+
#message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
28+
#message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
29+
#message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
30+
#load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
31+
@media (max-width: 600px) {
32+
body, #message { margin-top: 0; background: white; box-shadow: none; }
33+
body { border-top: 16px solid #ffa100; }
34+
}
35+
</style>
36+
</head>
37+
<body>
38+
<div id="message">
39+
<h2>Welcome</h2>
40+
<h1>Firebase Hosting Setup Complete</h1>
41+
<p>You're seeing this because you've successfully setup Firebase Hosting. Now it's time to go build something extraordinary!</p>
42+
<a target="_blank" href="https://firebase.google.com/docs/hosting/">Open Hosting Documentation</a>
43+
</div>
44+
<p id="load">Firebase SDK Loading&hellip;</p>
45+
46+
<input type="file" id="file" name="file"/>
47+
<span id="linkbox"></span>
48+
49+
<script>
50+
let storageRef;
51+
52+
function handleFileSelect(evt) {
53+
evt.stopPropagation();
54+
evt.preventDefault();
55+
var file = evt.target.files[0];
56+
var metadata = {
57+
'contentType': file.type
58+
};
59+
// Push to child path.
60+
// [START oncomplete]
61+
storageRef.child('images/' + file.name).put(file, metadata).then(function(snapshot) {
62+
console.log('Uploaded', snapshot.totalBytes, 'bytes.');
63+
console.log(snapshot.metadata);
64+
var url = snapshot.downloadURL;
65+
console.log('File available at', url);
66+
// [START_EXCLUDE]
67+
document.getElementById('linkbox').innerHTML = '<a href="' + url + '">Click For File</a>';
68+
// [END_EXCLUDE]
69+
}).catch(function(error) {
70+
// [START onfailure]
71+
console.error('Upload failed:', error);
72+
// [END onfailure]
73+
});
74+
// [END oncomplete]
75+
}
76+
77+
78+
document.addEventListener('DOMContentLoaded', function() {
79+
// // πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯
80+
// // The Firebase SDK is initialized and available here!
81+
//
82+
// let auth = firebase.auth();
83+
// console.log(auth);
84+
// auth.onAuthStateChanged(user => {
85+
// if (user) {
86+
// console.log('Anonymous user signed-in.', user);
87+
// document.getElementById('file').disabled = false;
88+
// } else {
89+
// console.log('There was no anonymous session. Creating a new anonymous user.');
90+
// // Sign the user in anonymously since accessing Storage requires the user to be authorized.
91+
// auth.signInAnonymously();
92+
// }
93+
// });
94+
// firebase.database().ref('/path/to/ref').on('value', snapshot => { });
95+
// firebase.messaging().requestPermission().then(() => { });
96+
// firebase.storage().ref('/path/to/ref').getDownloadURL().then(() => { });
97+
//
98+
// // πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯
99+
100+
try {
101+
let app = firebase.app();
102+
103+
storageRef = firebase.storage().ref();
104+
let features = ['auth', 'database', 'messaging', 'storage'].filter(feature => typeof app[feature] === 'function');
105+
document.getElementById('load').innerHTML = `Firebase SDK loaded with ${features.join(', ')}`;
106+
document.getElementById('file').addEventListener('change', handleFileSelect, false);
107+
108+
} catch (e) {
109+
console.error(e);
110+
document.getElementById('load').innerHTML = 'Error loading the Firebase SDK, check the console.';
111+
}
112+
113+
114+
});
115+
</script>
116+
</body>
117+
</html>

0 commit comments

Comments
Β (0)