-
Notifications
You must be signed in to change notification settings - Fork 8
/
upload.js
157 lines (109 loc) · 4.08 KB
/
upload.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
/*you can have two buttons:
one to call getPictureFromGallery();
the other one to call getPictureFromCamera();
Depnding on which the user clicks, the appropriate action will take place.
Note that getPicturefromCamera() may crash the app in some phones due to memory issues.
This code does not fix that issue, do well to send a pull request.
*/
function getPictureFromGallery(){
navigator.camera.getPicture(onSuccess, onFail, {
destinationType: Camera.DestinationType.FILE_URI,
sourceType : Camera.PictureSourceType.PHOTOLIBRARY,
saveToPhotoAlbum: true,
allowEdit: true,
quality: 100,
correctOrientation: true,
});
}
function getPictureFromCamera(){
navigator.camera.getPicture(onSuccess, onFail, {
destinationType: Camera.DestinationType.FILE_URI,
sourceType : Camera.PictureSourceType.CAMERA,
saveToPhotoAlbum: true,
allowEdit: true,
quality: 100,
correctOrientation: true,
});
}
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src= imageData;
var file = imageData; //useless idiot like me :)
alert("File content: "+ file);
// Create the file metadata
var metadata = {
contentType: 'image/jpeg'
};
var fileName = file.substring(file.lastIndexOf('/')+1); //get name of file from file path
var storageRef = firebase.storage().ref();
var getFileBlob = function (url, cb) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.responseType = "blob";
xhr.addEventListener('load', function() {
cb(xhr.response);
});
xhr.send();
};
var blobToFile = function (blob, name) {
blob.lastModifiedDate = new Date();
blob.name = name;
return blob;
};
var getFileObject = function(filePathOrUrl, cb) {
getFileBlob(filePathOrUrl, function (blob) {
cb(blobToFile(blob, fileName));
});
};
getFileObject(imageData, function (fileObject) {
var timeStamp = Math.floor(Date.now() / 1000);
// Upload file and metadata to the object 'images/mountains.jpg'
var uploadTask = storageRef.child(localStorage.myFirebase_user_id+"/"+timeStamp+"/"+ fileName).put(fileObject);
// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
function(snapshot) {
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
}, function(error) {
switch (error.code) {
case 'storage/unauthorized':
// User doesn't have permission to access the object
alert("Storage unauthorized");
break;
case 'storage/canceled':
// User canceled the upload
alert("Error: storage/canceled");
break;
case 'storage/unknown':
// Unknown error occurred, inspect error.serverResponse
alert("Error: storage/unknown");
break;
}
}, function() {
// Upload completed successfully, now we can get the download URL
var downloadURL = uploadTask.snapshot.downloadURL;
console.log("The download URL: "+downloadURL);
//this is where you have to enter the code to update any desired firebase table with the image url
//eg this below code will add a 'profilePhoto' field and record to 'users' table for the user with the id of userId
//read more about saving data in firebase here: https://firebase.google.com/docs/database/web/save-data
firebase.database().ref('users/' + userId).update({
profilePhoto: downloadURL
});
});
});
}
function onFail(message) {
//alert('Failed because: ' + message);
setTimeout(function(){
navigator.notification.alert(message); //show a pop up message if something failed in the process eg.image upload canceled
}, 0);
}