|
1 | 1 | (function() { |
2 | 2 | "use strict"; |
3 | 3 |
|
4 | | - function unwrapStorageSnapshot(storageSnapshot) { |
| 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) { |
5 | 53 | return { |
6 | 54 | bytesTransferred: storageSnapshot.bytesTransferred, |
7 | 55 | downloadURL: storageSnapshot.downloadURL, |
|
13 | 61 | }; |
14 | 62 | } |
15 | 63 |
|
16 | | - function isStorageRef(value) { |
| 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) { |
17 | 73 | value = value || {}; |
18 | 74 | return typeof value.put === 'function'; |
19 | 75 | } |
20 | 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 | + */ |
21 | 83 | function _assertStorageRef(storageRef) { |
22 | | - if (!isStorageRef(storageRef)) { |
| 84 | + if (!_isStorageRef(storageRef)) { |
23 | 85 | throw new Error('$firebaseStorage expects a Storage reference'); |
24 | 86 | } |
25 | 87 | } |
26 | 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 | + */ |
27 | 98 | function FirebaseStorage($firebaseUtils, $q) { |
28 | 99 |
|
| 100 | + /** |
| 101 | + * This inner constructor `Storage` allows for exporting of private methods |
| 102 | + * like _assertStorageRef, _isStorageRef, _convertTask, and _unwrapStorageSnapshot. |
| 103 | + */ |
29 | 104 | var Storage = function Storage(storageRef) { |
30 | 105 | _assertStorageRef(storageRef); |
31 | 106 | return { |
32 | | - $put: function $put(file) { |
33 | | - var task = storageRef.put(file); |
34 | | - |
35 | | - return { |
36 | | - $progress: function $progress(callback) { |
37 | | - task.on('state_changed', function () { |
38 | | - $firebaseUtils.compile(function () { |
39 | | - callback(unwrapStorageSnapshot(task.snapshot)); |
40 | | - }); |
41 | | - }); |
42 | | - }, |
43 | | - $error: function $error(callback) { |
44 | | - task.on('state_changed', null, function (err) { |
45 | | - $firebaseUtils.compile(function () { |
46 | | - callback(err); |
47 | | - }); |
48 | | - }); |
49 | | - }, |
50 | | - $complete: function $complete(callback) { |
51 | | - task.on('state_changed', null, null, function () { |
52 | | - $firebaseUtils.compile(function () { |
53 | | - callback(unwrapStorageSnapshot(task.snapshot)); |
54 | | - }); |
55 | | - }); |
56 | | - }, |
57 | | - $cancel: task.cancel, |
58 | | - $resume: task.resume, |
59 | | - $pause: task.pause, |
60 | | - then: task.then, |
61 | | - catch: task.catch, |
62 | | - _task: task |
63 | | - }; |
| 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); |
64 | 114 | }, |
65 | 115 | $getDownloadURL: function $getDownloadURL() { |
66 | 116 | return $q.when(storageRef.getDownloadURL()); |
|
73 | 123 | }, |
74 | 124 | $updateMetadata: function $updateMetadata(object) { |
75 | 125 | return $q.when(storageRef.updateMetadata(object)); |
| 126 | + }, |
| 127 | + $toString: function $toString() { |
| 128 | + return storageRef.toString(); |
76 | 129 | } |
77 | 130 | }; |
78 | 131 | }; |
79 | 132 |
|
80 | 133 | Storage.utils = { |
81 | | - _unwrapStorageSnapshot: unwrapStorageSnapshot, |
82 | | - _isStorageRef: isStorageRef, |
| 134 | + _unwrapStorageSnapshot: _unwrapStorageSnapshot, |
| 135 | + _isStorageRef: _isStorageRef, |
83 | 136 | _assertStorageRef: _assertStorageRef |
84 | 137 | }; |
85 | 138 |
|
86 | 139 | return Storage; |
87 | 140 | } |
88 | 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 | + */ |
89 | 147 | angular.module('firebase.storage') |
90 | 148 | .factory('$firebaseStorage', ["$firebaseUtils", "$q", FirebaseStorage]); |
91 | 149 |
|
|
0 commit comments