Last active
February 10, 2017 02:48
-
-
Save jdnichollsc/5ddc40c1c482e6209a8f4d634fd11d1e to your computer and use it in GitHub Desktop.
Firebase Angular Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(firebase) { | |
'use strict'; | |
angular | |
.module('App') | |
.factory('Media', Media); | |
Media.$inject = ['$q', 'Utilities']; | |
function Media($q, 'Utilities') { | |
return { | |
/** | |
* Upload base64 image. | |
* @param {string} imageData - The base64 data of the file. E.g: iVBORw0KGgoAAAANSUhEUgAAAAUAA... | |
* @param {string} folderName - The folder name of the storage. E.g: myFiles/ | |
* @param {string} uniqueFileName - The file name. E.g: myFile.jpg | |
*/ | |
uploadFileImageData: function(imageData, folderName, uniqueFileName){ | |
var deferred = $q.defer(); | |
var imageBlob = Utilities.b64toBlob(imageData); | |
var storageRef = firebase.storage().ref(folderName); | |
var uploadTask = storageRef.child(uniqueFileName).put(imageBlob); | |
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, function (snapshot) { | |
var progress = snapshot.bytesTransferred * 100 / snapshot.totalBytes; | |
deferred.notify(progress); | |
}, function (error) { | |
deferred.reject(error); | |
}, function () { | |
var downloadURL = uploadTask.snapshot.downloadURL; | |
deferred.resolve(downloadURL); | |
}); | |
return deferred.promise; | |
} | |
}; | |
} | |
})(firebase); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
angular | |
.module('App') | |
.factory('Utilities', Utilities); | |
function Utilities() { | |
return { | |
b64toBlob: function(b64Data, contentType, sliceSize){ | |
contentType = contentType || 'image/png'; | |
sliceSize = sliceSize || 512; | |
var byteCharacters = atob(b64Data); | |
var byteArrays = []; | |
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) { | |
var slice = byteCharacters.slice(offset, offset + sliceSize); | |
var byteNumbers = new Array(slice.length); | |
for (var i = 0; i < slice.length; i++) { | |
byteNumbers[i] = slice.charCodeAt(i); | |
} | |
var byteArray = new Uint8Array(byteNumbers); | |
byteArrays.push(byteArray); | |
} | |
var blob = new Blob(byteArrays, {type: contentType}); | |
return blob; | |
}, | |
dataURLtoBlob: function(dataURL, contentType){ | |
contentType = contentType || 'image/png'; | |
// Decode the dataURL | |
var binary = atob(dataURL.split(',')[1]); | |
// Create 8-bit unsigned array | |
var array = []; | |
for (var i = 0; i < binary.length; i++) { | |
array.push(binary.charCodeAt(i)); | |
} | |
// Return our Blob object | |
return new Blob([new Uint8Array(array)], { type: contentType }); | |
} | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment