-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebaseUpload.js
169 lines (142 loc) · 5.21 KB
/
firebaseUpload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const { rejects } = require("assert");
const firebase = require("firebase/app");
const fs = require('fs');
const { resolve } = require("path");
require("firebase/storage");
global.XMLHttpRequest = require("xhr2");
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyD-TAacbLdNlKw6uhXdQwEHKRIkHiC6i98",
authDomain: "encrypted-photo-gallery.firebaseapp.com",
projectId: "encrypted-photo-gallery",
storageBucket: "encrypted-photo-gallery.appspot.com",
messagingSenderId: "119774240975",
appId: "1:119774240975:web:8be61f51e71cc4dc6917ba",
measurementId: "G-YD1MJW7F6S",
};
firebase.initializeApp(firebaseConfig);
const storageRef = firebase.storage().ref();
// ** Downloading files
// storageRef
// .child("epg/2020-bmw-7-series-62.jpg.enc")
// .getDownloadURL()
// .then((url) => {
// // `url` is the download URL for 'images/stars.jpg'
// // This can be downloaded directly:
// var xhr = new XMLHttpRequest();
// xhr.responseType = "blob";
// xhr.onload = (event) => {
// var blob = xhr.response;
// };
// xhr.open("GET", url);
// xhr.send();
// console.log(url);
// process.exit(0);
// })
// .catch((error) => {
// // A full list of error codes is available at
// // https://firebase.google.com/docs/storage/web/handle-errors
// switch (error.code) {
// case "storage/object-not-found":
// // File doesn't exist
// console.log("File doesn't exist");
// break;
// case "storage/unauthorized":
// // User doesn't have permission to access the object
// console.log("User doesn't have permission to access the object");
// break;
// case "storage/canceled":
// // User canceled the upload
// console.log("User canceled the upload");
// break;
// // ...
// case "storage/unknown":
// // Unknown error occurred, inspect the server response
// console.log("Unknown error occurred, inspect the server response");
// break;
// }
// process.exit(1);
// });
//----------------------------------------------------------------------------------
// Uploading Files
// const imagesRef = storageRef.child("epg/2020-bmw-7-series-62.jpg.enc"); // child("file name to be created")
// var metadata = {
// contentType: "application/octet-stream",
// };
// fs.readFile("./2020-bmw-7-series-62.jpg.enc", (err, data) => {
// if (err) throw err;
// console.log(data);
// imagesRef.put(data, metadata).then((snapshot) => {
// console.log("Uploaded a file");
// console.log(snapshot);
// });
// });
async function uploadToFirebase(userId, filename) {
fileLocationInFirebase = userId + "/" + filename;
fileLocationInServer = __dirname + "/uploads/" + userId + filename;
console.log(fileLocationInServer);
// first upload to firebase
const imagesRef = storageRef.child(fileLocationInFirebase); // child("file name to be created")
var metadata = {
contentType: "application/octet-stream",
};
const fsPromise = await fs.promises.readFile(fileLocationInServer);
const snapshot = await imagesRef.put(fsPromise.buffer, metadata);
// console.log("Snapshot", snapshot);
// console.log("File uploaded");
const url = await storageRef.child(fileLocationInFirebase).getDownloadURL();
// console.log(url);
return new Promise((resolve, reject) => {
resolve(url);
});
// fs.readFile(fileLocationInServer, (err, data) => {
// if (err) throw err;
// console.log(data);
// imagesRef
// .put(data, metadata)
// .then((snapshot) => { console.log("Uploaded a file"); });
// // now, get the download url
// storageRef
// .child(fileLocationInFirebase)
// .getDownloadURL()
// .then((url) => {
// // `url` is the download URL for 'images/stars.jpg'
// // This can be downloaded directly:
// var xhr = new XMLHttpRequest();
// xhr.responseType = "blob";
// xhr.onload = (event) => {
// var blob = xhr.response;
// };
// xhr.open("GET", url);
// xhr.send();
// console.log(url);
// return url;
// // process.exit(0);
// })
// .catch((error) => {
// // A full list of error codes is available at
// // https://firebase.google.com/docs/storage/web/handle-errors
// switch (error.code) {
// case "storage/object-not-found":
// // File doesn't exist
// console.log("File doesn't exist");
// break;
// case "storage/unauthorized":
// // User doesn't have permission to access the object
// console.log("User doesn't have permission to access the object");
// break;
// case "storage/canceled":
// // User canceled the upload
// console.log("User canceled the upload");
// break;
// // ...
// case "storage/unknown":
// // Unknown error occurred, inspect the server response
// console.log("Unknown error occurred, inspect the server response");
// break;
// }
// // process.exit(1);
// });
// });
}
module.exports = uploadToFirebase;