|
| 1 | +(function() { |
| 2 | + "use strict"; |
| 3 | + |
| 4 | + /** |
| 5 | + * Take an UploadTask and create an interface for the user to monitor the |
| 6 | + * file's upload. The $progress, $error, and $complete methods are provided |
| 7 | + * to work with the $digest cycle. |
| 8 | + * |
| 9 | + * @param task |
| 10 | + * @param $firebaseUtils |
| 11 | + * @returns A converted task, which contains methods for monitoring the |
| 12 | + * upload progress. |
| 13 | + */ |
| 14 | + function _convertTask(task, $firebaseUtils) { |
| 15 | + return { |
| 16 | + $progress: function $progress(callback) { |
| 17 | + task.on('state_changed', function () { |
| 18 | + $firebaseUtils.compile(function () { |
| 19 | + callback(_unwrapStorageSnapshot(task.snapshot)); |
| 20 | + }); |
| 21 | + }); |
| 22 | + }, |
| 23 | + $error: function $error(callback) { |
| 24 | + task.on('state_changed', null, function (err) { |
| 25 | + $firebaseUtils.compile(function () { |
| 26 | + callback(err); |
| 27 | + }); |
| 28 | + }); |
| 29 | + }, |
| 30 | + $complete: function $complete(callback) { |
| 31 | + task.on('state_changed', null, null, function () { |
| 32 | + $firebaseUtils.compile(function () { |
| 33 | + callback(_unwrapStorageSnapshot(task.snapshot)); |
| 34 | + }); |
| 35 | + }); |
| 36 | + }, |
| 37 | + $cancel: task.cancel, |
| 38 | + $resume: task.resume, |
| 39 | + $pause: task.pause, |
| 40 | + then: task.then, |
| 41 | + catch: task.catch, |
| 42 | + $snapshot: task.snapshot |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Take an Firebase Storage snapshot and unwrap only the needed properties. |
| 48 | + * |
| 49 | + * @param snapshot |
| 50 | + * @returns An object containing the unwrapped values. |
| 51 | + */ |
| 52 | + function _unwrapStorageSnapshot(storageSnapshot) { |
| 53 | + return { |
| 54 | + bytesTransferred: storageSnapshot.bytesTransferred, |
| 55 | + downloadURL: storageSnapshot.downloadURL, |
| 56 | + metadata: storageSnapshot.metadata, |
| 57 | + ref: storageSnapshot.ref, |
| 58 | + state: storageSnapshot.state, |
| 59 | + task: storageSnapshot.task, |
| 60 | + totalBytes: storageSnapshot.totalBytes |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Determines if the value passed in is a Firebase Storage Reference. The |
| 66 | + * put method is used for the check. |
| 67 | + * |
| 68 | + * @param value |
| 69 | + * @returns A boolean that indicates if the value is a Firebase Storage |
| 70 | + * Reference. |
| 71 | + */ |
| 72 | + function _isStorageRef(value) { |
| 73 | + value = value || {}; |
| 74 | + return typeof value.put === 'function'; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Checks if the parameter is a Firebase Storage Reference, and throws an |
| 79 | + * error if it is not. |
| 80 | + * |
| 81 | + * @param storageRef |
| 82 | + */ |
| 83 | + function _assertStorageRef(storageRef) { |
| 84 | + if (!_isStorageRef(storageRef)) { |
| 85 | + throw new Error('$firebaseStorage expects a Storage reference'); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * This constructor should probably never be called manually. It is setup |
| 91 | + * for dependecy injection of the $firebaseUtils and $q service. |
| 92 | + * |
| 93 | + * @param {Object} $firebaseUtils |
| 94 | + * @param {Object} $q |
| 95 | + * @returns {Object} |
| 96 | + * @constructor |
| 97 | + */ |
| 98 | + function FirebaseStorage($firebaseUtils, $q) { |
| 99 | + |
| 100 | + /** |
| 101 | + * This inner constructor `Storage` allows for exporting of private methods |
| 102 | + * like _assertStorageRef, _isStorageRef, _convertTask, and _unwrapStorageSnapshot. |
| 103 | + */ |
| 104 | + var Storage = function Storage(storageRef) { |
| 105 | + _assertStorageRef(storageRef); |
| 106 | + return { |
| 107 | + $put: function $put(file, metadata) { |
| 108 | + var task = storageRef.put(file, metadata); |
| 109 | + return _convertTask(task, $firebaseUtils); |
| 110 | + }, |
| 111 | + $putString: function $putString(data, format, metadata) { |
| 112 | + var task = storageRef.putString(data, format, metadata); |
| 113 | + return _convertTask(task, $firebaseUtils); |
| 114 | + }, |
| 115 | + $getDownloadURL: function $getDownloadURL() { |
| 116 | + return $q.when(storageRef.getDownloadURL()); |
| 117 | + }, |
| 118 | + $delete: function $delete() { |
| 119 | + return $q.when(storageRef.delete()); |
| 120 | + }, |
| 121 | + $getMetadata: function $getMetadata() { |
| 122 | + return $q.when(storageRef.getMetadata()); |
| 123 | + }, |
| 124 | + $updateMetadata: function $updateMetadata(object) { |
| 125 | + return $q.when(storageRef.updateMetadata(object)); |
| 126 | + }, |
| 127 | + $toString: function $toString() { |
| 128 | + return storageRef.toString(); |
| 129 | + } |
| 130 | + }; |
| 131 | + }; |
| 132 | + |
| 133 | + Storage.utils = { |
| 134 | + _unwrapStorageSnapshot: _unwrapStorageSnapshot, |
| 135 | + _isStorageRef: _isStorageRef, |
| 136 | + _assertStorageRef: _assertStorageRef |
| 137 | + }; |
| 138 | + |
| 139 | + return Storage; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Creates a wrapper for the firebase.storage() object. This factory allows |
| 144 | + * you to upload files and monitor their progress and the callbacks are |
| 145 | + * wrapped in the $digest cycle. |
| 146 | + */ |
| 147 | + angular.module('firebase.storage') |
| 148 | + .factory('$firebaseStorage', ["$firebaseUtils", "$q", FirebaseStorage]); |
| 149 | + |
| 150 | +})(); |
0 commit comments