Skip to content

Commit 0921bd5

Browse files
image upload to firebase added, working code
1 parent 6f66997 commit 0921bd5

15 files changed

+1929
-48
lines changed

.firebaserc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "ionic-angular-project-a9d42"
4+
}
5+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private getGoogleMaps(): Promise<any> {
8686
* [Ionic datetime picker interface](https://ionicframework.com/docs/api/datetime) used to select booking dates. Alternative is a random dates option.
8787
* [RxJS](https://angular.io/guide/rx-library) reactive programming used to manage state.
8888
* Error handling added
89-
* [Firebase backend database](https://firebase.google.com) used to store place and booking data.
89+
* [Firebase backend database](https://firebase.google.com) used to store place and booking data. Images are stored in the same database.
9090
* Bookings can be cancelled from booking.page.
9191
* Place details can be edited (as long as user id matches) using a neat button that slides from the right.
9292
* [Google Maps Javascript API](https://developers.google.com/maps/documentation/javascript/tutorial) map-modal added to new-offer page. Clicking on 'SELECT LOCATION' will open Google Maps at a fixed location. Address of place extracted from Google Maps data and stored in Places database.

firebase.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

functions/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

functions/index.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const functions = require('firebase-functions');
2+
const cors = require('cors')({ origin: true });
3+
const Busboy = require('busboy');
4+
const os = require('os');
5+
const path = require('path');
6+
const fs = require('fs');
7+
const uuid = require('uuid/v4');
8+
9+
const { Storage } = require('@google-cloud/storage');
10+
11+
const storage = new Storage({
12+
projectId: 'ionic-angular-project-a9d42'
13+
});
14+
15+
exports.storeImage = functions.https.onRequest((req, res) => {
16+
return cors(req, res, () => {
17+
if (req.method !== 'POST') {
18+
return res.status(500).json({ message: 'Not allowed.' });
19+
}
20+
const busboy = new Busboy({ headers: req.headers });
21+
let uploadData;
22+
let oldImagePath;
23+
24+
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
25+
const filePath = path.join(os.tmpdir(), filename);
26+
uploadData = { filePath: filePath, type: mimetype, name: filename };
27+
file.pipe(fs.createWriteStream(filePath));
28+
});
29+
30+
busboy.on('field', (fieldname, value) => {
31+
oldImagePath = decodeURIComponent(value);
32+
});
33+
34+
busboy.on('finish', () => {
35+
const id = uuid();
36+
let imagePath = 'images/' + id + '-' + uploadData.name;
37+
if (oldImagePath) {
38+
imagePath = oldImagePath;
39+
}
40+
41+
console.log(uploadData.type);
42+
return storage
43+
.bucket('ionic-angular-project-a9d42.appspot.com')
44+
.upload(uploadData.filePath, {
45+
uploadType: 'media',
46+
destination: imagePath,
47+
metadata: {
48+
metadata: {
49+
contentType: uploadData.type,
50+
firebaseStorageDownloadTokens: id
51+
}
52+
}
53+
})
54+
55+
.then(() => {
56+
return res.status(201).json({
57+
imageUrl:
58+
'https://firebasestorage.googleapis.com/v0/b/' +
59+
storage.bucket('ionic-angular-project-a9d42.appspot.com').name +
60+
'/o/' +
61+
encodeURIComponent(imagePath) +
62+
'?alt=media&token=' +
63+
id,
64+
imagePath: imagePath
65+
});
66+
})
67+
.catch(error => {
68+
console.log(error);
69+
return res.status(401).json({ error: 'Unauthorized!' });
70+
});
71+
});
72+
return busboy.end(req.rawBody);
73+
});
74+
});

0 commit comments

Comments
 (0)